V148
Fork of RadioHead-148 by
RH_NRF905.h@1:b7641da2b203, 2017-10-25 (annotated)
- Committer:
- ilkaykozak
- Date:
- Wed Oct 25 05:14:09 2017 +0000
- Revision:
- 1:b7641da2b203
- Parent:
- 0:ab4e012489ef
V148
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
davidr99 | 0:ab4e012489ef | 1 | // RH_NRF905.h |
davidr99 | 0:ab4e012489ef | 2 | // Author: Mike McCauley (mikem@airspayce.com) |
davidr99 | 0:ab4e012489ef | 3 | // Copyright (C) 2014 Mike McCauley |
davidr99 | 0:ab4e012489ef | 4 | // $Id: RH_NRF905.h,v 1.7 2015/03/09 06:04:26 mikem Exp $ |
davidr99 | 0:ab4e012489ef | 5 | // |
davidr99 | 0:ab4e012489ef | 6 | |
davidr99 | 0:ab4e012489ef | 7 | #ifndef RH_NRF905_h |
davidr99 | 0:ab4e012489ef | 8 | #define RH_NRF905_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 (and only) number of bytes that can be carried by the nRF905. |
davidr99 | 0:ab4e012489ef | 14 | // We use some for headers, leaving fewer for RadioHead messages |
davidr99 | 0:ab4e012489ef | 15 | #define RH_NRF905_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 nRF905 payload |
davidr99 | 0:ab4e012489ef | 19 | // As well as the usual TO, FROM, ID, FLAGS, we also need LEN, since |
davidr99 | 0:ab4e012489ef | 20 | // nRF905 only has fixed width messages. |
davidr99 | 0:ab4e012489ef | 21 | // REVISIT: could we have put the LEN into the FLAGS field? |
davidr99 | 0:ab4e012489ef | 22 | #define RH_NRF905_HEADER_LEN 5 |
davidr99 | 0:ab4e012489ef | 23 | |
davidr99 | 0:ab4e012489ef | 24 | // This is the maximum RadioHead user message length that can be supported by this library. Limited by |
davidr99 | 0:ab4e012489ef | 25 | // the supported message lengths in the nRF905 |
davidr99 | 0:ab4e012489ef | 26 | #define RH_NRF905_MAX_MESSAGE_LEN (RH_NRF905_MAX_PAYLOAD_LEN-RH_NRF905_HEADER_LEN) |
davidr99 | 0:ab4e012489ef | 27 | |
davidr99 | 0:ab4e012489ef | 28 | // Register names |
davidr99 | 0:ab4e012489ef | 29 | #define RH_NRF905_REG_MASK 0x0f |
davidr99 | 0:ab4e012489ef | 30 | #define RH_NRF905_REG_W_CONFIG 0x00 |
davidr99 | 0:ab4e012489ef | 31 | #define RH_NRF905_REG_R_CONFIG 0x10 |
davidr99 | 0:ab4e012489ef | 32 | #define RH_NRF905_REG_W_TX_PAYLOAD 0x20 |
davidr99 | 0:ab4e012489ef | 33 | #define RH_NRF905_REG_R_TX_PAYLOAD 0x21 |
davidr99 | 0:ab4e012489ef | 34 | #define RH_NRF905_REG_W_TX_ADDRESS 0x22 |
davidr99 | 0:ab4e012489ef | 35 | #define RH_NRF905_REG_R_TX_ADDRESS 0x23 |
davidr99 | 0:ab4e012489ef | 36 | #define RH_NRF905_REG_R_RX_PAYLOAD 0x24 |
davidr99 | 0:ab4e012489ef | 37 | #define RH_NRF905_REG_CHANNEL_CONFIG 0x80 |
davidr99 | 0:ab4e012489ef | 38 | |
davidr99 | 0:ab4e012489ef | 39 | // Configuration register |
davidr99 | 0:ab4e012489ef | 40 | #define RH_NRF905_CONFIG_0 0x00 |
davidr99 | 0:ab4e012489ef | 41 | #define RH_NRF905_CONFIG_0_CH_NO 0xff |
davidr99 | 0:ab4e012489ef | 42 | |
davidr99 | 0:ab4e012489ef | 43 | #define RH_NRF905_CONFIG_1 0x01 |
davidr99 | 0:ab4e012489ef | 44 | #define RH_NRF905_CONFIG_1_AUTO_RETRAN 0x20 |
davidr99 | 0:ab4e012489ef | 45 | #define RH_NRF905_CONFIG_1_RX_RED_PWR 0x10 |
davidr99 | 0:ab4e012489ef | 46 | #define RH_NRF905_CONFIG_1_PA_PWR 0x0c |
davidr99 | 0:ab4e012489ef | 47 | #define RH_NRF905_CONFIG_1_PA_PWR_N10DBM 0x00 |
davidr99 | 0:ab4e012489ef | 48 | #define RH_NRF905_CONFIG_1_PA_PWR_N2DBM 0x04 |
davidr99 | 0:ab4e012489ef | 49 | #define RH_NRF905_CONFIG_1_PA_PWR_6DBM 0x08 |
davidr99 | 0:ab4e012489ef | 50 | #define RH_NRF905_CONFIG_1_PA_PWR_10DBM 0x0c |
davidr99 | 0:ab4e012489ef | 51 | #define RH_NRF905_CONFIG_1_HFREQ_PLL 0x02 |
davidr99 | 0:ab4e012489ef | 52 | #define RH_NRF905_CONFIG_1_CH_NO 0x01 |
davidr99 | 0:ab4e012489ef | 53 | |
davidr99 | 0:ab4e012489ef | 54 | #define RH_NRF905_CONFIG_2 0x02 |
davidr99 | 0:ab4e012489ef | 55 | #define RH_NRF905_CONFIG_2_TX_AFW 0x70 |
davidr99 | 0:ab4e012489ef | 56 | #define RH_NRF905_CONFIG_2_RX_AFW 0x07 |
davidr99 | 0:ab4e012489ef | 57 | |
davidr99 | 0:ab4e012489ef | 58 | #define RH_NRF905_CONFIG_3 0x03 |
davidr99 | 0:ab4e012489ef | 59 | #define RH_NRF905_CONFIG_3_RX_PW 0x3f |
davidr99 | 0:ab4e012489ef | 60 | |
davidr99 | 0:ab4e012489ef | 61 | #define RH_NRF905_CONFIG_4 0x04 |
davidr99 | 0:ab4e012489ef | 62 | #define RH_NRF905_CONFIG_4_TX_PW 0x3f |
davidr99 | 0:ab4e012489ef | 63 | |
davidr99 | 0:ab4e012489ef | 64 | #define RH_NRF905_CONFIG_5 0x05 |
davidr99 | 0:ab4e012489ef | 65 | #define RH_NRF905_CONFIG_5_RX_ADDRESS 0xff |
davidr99 | 0:ab4e012489ef | 66 | |
davidr99 | 0:ab4e012489ef | 67 | #define RH_NRF905_CONFIG_6 0x06 |
davidr99 | 0:ab4e012489ef | 68 | #define RH_NRF905_CONFIG_6_RX_ADDRESS 0xff |
davidr99 | 0:ab4e012489ef | 69 | |
davidr99 | 0:ab4e012489ef | 70 | #define RH_NRF905_CONFIG_7 0x07 |
davidr99 | 0:ab4e012489ef | 71 | #define RH_NRF905_CONFIG_7_RX_ADDRESS 0xff |
davidr99 | 0:ab4e012489ef | 72 | |
davidr99 | 0:ab4e012489ef | 73 | #define RH_NRF905_CONFIG_8 0x08 |
davidr99 | 0:ab4e012489ef | 74 | #define RH_NRF905_CONFIG_8_RX_ADDRESS 0xff |
davidr99 | 0:ab4e012489ef | 75 | |
davidr99 | 0:ab4e012489ef | 76 | #define RH_NRF905_CONFIG_9 0x09 |
davidr99 | 0:ab4e012489ef | 77 | #define RH_NRF905_CONFIG_9_CRC_MODE_16BIT 0x80 |
davidr99 | 0:ab4e012489ef | 78 | #define RH_NRF905_CONFIG_9_CRC_EN 0x40 |
davidr99 | 0:ab4e012489ef | 79 | #define RH_NRF905_CONFIG_9_XOF 0x38 |
davidr99 | 0:ab4e012489ef | 80 | #define RH_NRF905_CONFIG_9_XOF_4MHZ 0x00 |
davidr99 | 0:ab4e012489ef | 81 | #define RH_NRF905_CONFIG_9_XOF_8MHZ 0x08 |
davidr99 | 0:ab4e012489ef | 82 | #define RH_NRF905_CONFIG_9_XOF_12MHZ 0x10 |
davidr99 | 0:ab4e012489ef | 83 | #define RH_NRF905_CONFIG_9_XOF_16MHZ 0x18 |
davidr99 | 0:ab4e012489ef | 84 | #define RH_NRF905_CONFIG_9_XOF_20MHZ 0x20 |
davidr99 | 0:ab4e012489ef | 85 | #define RH_NRF905_CONFIG_9_UP_CLK_EN 0x04 |
davidr99 | 0:ab4e012489ef | 86 | #define RH_NRF905_CONFIG_9_UP_CLK_FREQ 0x03 |
davidr99 | 0:ab4e012489ef | 87 | #define RH_NRF905_CONFIG_9_UP_CLK_FREQ_4MHZ 0x00 |
davidr99 | 0:ab4e012489ef | 88 | #define RH_NRF905_CONFIG_9_UP_CLK_FREQ_2MHZ 0x01 |
davidr99 | 0:ab4e012489ef | 89 | #define RH_NRF905_CONFIG_9_UP_CLK_FREQ_1MHZ 0x02 |
davidr99 | 0:ab4e012489ef | 90 | #define RH_NRF905_CONFIG_9_UP_CLK_FREQ_500KHZ 0x03 |
davidr99 | 0:ab4e012489ef | 91 | |
davidr99 | 0:ab4e012489ef | 92 | // Status register is always read as first byte |
davidr99 | 0:ab4e012489ef | 93 | #define RH_NRF905_STATUS_AM 0x80 |
davidr99 | 0:ab4e012489ef | 94 | #define RH_NRF905_STATUS_DR 0x20 |
davidr99 | 0:ab4e012489ef | 95 | |
davidr99 | 0:ab4e012489ef | 96 | ///////////////////////////////////////////////////////////////////// |
davidr99 | 0:ab4e012489ef | 97 | /// \class RH_NRF905 RH_NRF905.h <RH_NRF905.h> |
davidr99 | 0:ab4e012489ef | 98 | /// \brief Send and receive addressed, reliable, acknowledged datagrams by nRF905 and compatible transceivers. |
davidr99 | 0:ab4e012489ef | 99 | /// |
davidr99 | 0:ab4e012489ef | 100 | /// This base class provides basic functions for sending and receiving unaddressed, unreliable datagrams |
davidr99 | 0:ab4e012489ef | 101 | /// of arbitrary length to 28 octets per packet. Use one of the Manager classes to get addressing and |
davidr99 | 0:ab4e012489ef | 102 | /// acknowledgement reliability, routing, meshes etc. |
davidr99 | 0:ab4e012489ef | 103 | /// |
davidr99 | 0:ab4e012489ef | 104 | /// The nRF905 transceiver is configured to use Enhanced Shockburst with 16 Bit CRC, and 32 octet packets. |
davidr99 | 0:ab4e012489ef | 105 | /// |
davidr99 | 0:ab4e012489ef | 106 | /// Naturally, for any 2 radios to communicate that must be configured to use the same frequency |
davidr99 | 0:ab4e012489ef | 107 | /// and with identical network addresses. |
davidr99 | 0:ab4e012489ef | 108 | /// |
davidr99 | 0:ab4e012489ef | 109 | /// The nRF905 from Nordic Semiconductor http://www.nordicsemi.com/eng/Products/Sub-1-GHz-RF/nRF905 |
davidr99 | 0:ab4e012489ef | 110 | /// (http://www.nordicsemi.com/jpn/nordic/content_download/2452/29528/file/Product_Specification_nRF905_v1.5.pdf) |
davidr99 | 0:ab4e012489ef | 111 | /// is a low-cost 433/868/915 MHz ISM transceiver module. It supports a number of channel frequencies at |
davidr99 | 0:ab4e012489ef | 112 | /// 100kHz deviation and 50kHz bandwidth with Manchester encoding. |
davidr99 | 0:ab4e012489ef | 113 | /// |
davidr99 | 0:ab4e012489ef | 114 | /// We tested with inexpensive nRF905 modules from eBay, similar to: |
davidr99 | 0:ab4e012489ef | 115 | /// http://www.aliexpress.com/store/product/Free-ship-NRF905-433MHz-Wireless-Transmission-Module-Transceiver-Module-with-Antenna-for-the-433MHz-ISM-band/513046_607163305.html |
davidr99 | 0:ab4e012489ef | 116 | /// |
davidr99 | 0:ab4e012489ef | 117 | /// This library provides functions for sending and receiving messages of up to 27 octets on any |
davidr99 | 0:ab4e012489ef | 118 | /// frequency supported by the nRF905. |
davidr99 | 0:ab4e012489ef | 119 | /// |
davidr99 | 0:ab4e012489ef | 120 | /// Several nRF905 modules can be connected to an Arduino, permitting the construction of translators |
davidr99 | 0:ab4e012489ef | 121 | /// and frequency changers, etc. |
davidr99 | 0:ab4e012489ef | 122 | /// |
davidr99 | 0:ab4e012489ef | 123 | /// Example Arduino programs are included to show the main modes of use. |
davidr99 | 0:ab4e012489ef | 124 | /// |
davidr99 | 0:ab4e012489ef | 125 | /// \par Packet Format |
davidr99 | 0:ab4e012489ef | 126 | /// |
davidr99 | 0:ab4e012489ef | 127 | /// All messages sent and received by this class conform to this fixed length packet format |
davidr99 | 0:ab4e012489ef | 128 | /// |
davidr99 | 0:ab4e012489ef | 129 | /// - 4 octets NETWORK ADDRESS |
davidr99 | 0:ab4e012489ef | 130 | /// - 32 octets PAYLOAD, consisting of: |
davidr99 | 0:ab4e012489ef | 131 | /// - 1 octet TO header |
davidr99 | 0:ab4e012489ef | 132 | /// - 1 octet FROM header |
davidr99 | 0:ab4e012489ef | 133 | /// - 1 octet ID header |
davidr99 | 0:ab4e012489ef | 134 | /// - 1 octet FLAGS header |
davidr99 | 0:ab4e012489ef | 135 | /// - 1 octet user message length header |
davidr99 | 0:ab4e012489ef | 136 | /// - 0 to 27 octets of user message, trailing octets after the user message length are ignored |
davidr99 | 0:ab4e012489ef | 137 | /// - 2 octets CRC |
davidr99 | 0:ab4e012489ef | 138 | /// |
davidr99 | 0:ab4e012489ef | 139 | /// All messages sent and received by this driver are 32 octets. The user message length is embedded in the message. |
davidr99 | 0:ab4e012489ef | 140 | /// |
davidr99 | 0:ab4e012489ef | 141 | /// \par Connecting nRF905 |
davidr99 | 0:ab4e012489ef | 142 | /// |
davidr99 | 0:ab4e012489ef | 143 | /// The nRF905 is a 3.3V part is is *NOT* 5V tolerant. So you MUST use a 3.3V CPU such as Teensy, Arduino Due etc |
davidr99 | 0:ab4e012489ef | 144 | /// or else provide for level shifters between the CPU and the nRF905. Failure to consider this will probbaly |
davidr99 | 0:ab4e012489ef | 145 | /// break your nRF905. |
davidr99 | 0:ab4e012489ef | 146 | /// |
davidr99 | 0:ab4e012489ef | 147 | /// The electrical connection between the nRF905 and the CPU require 3.3V, the 3 x SPI pins (SCK, SDI, SDO), |
davidr99 | 0:ab4e012489ef | 148 | /// a Chip Enable pin, a Transmit Enable pin and a Slave Select pin. |
davidr99 | 0:ab4e012489ef | 149 | /// |
davidr99 | 0:ab4e012489ef | 150 | /// The examples below assume the commonly found cheap Chinese nRF905 modules. The RH_RF905 driver assumes the |
davidr99 | 0:ab4e012489ef | 151 | /// the nRF905 has a 16MHz crystal. |
davidr99 | 0:ab4e012489ef | 152 | /// |
davidr99 | 0:ab4e012489ef | 153 | /// Connect the nRF905 to Teensy like this |
davidr99 | 0:ab4e012489ef | 154 | /// \code |
davidr99 | 0:ab4e012489ef | 155 | /// CPU nRF905 module |
davidr99 | 0:ab4e012489ef | 156 | /// 3V3----------VCC (3.3V) |
davidr99 | 0:ab4e012489ef | 157 | /// pin D8-----------CE (chip enable in) |
davidr99 | 0:ab4e012489ef | 158 | /// pin D9-----------TX_EN (transmit enable in) |
davidr99 | 0:ab4e012489ef | 159 | /// SS pin D10----------CSN (chip select in) |
davidr99 | 0:ab4e012489ef | 160 | /// SCK pin D13----------SCK (SPI clock in) |
davidr99 | 0:ab4e012489ef | 161 | /// MOSI pin D11----------MOSI (SPI Data in) |
davidr99 | 0:ab4e012489ef | 162 | /// MISO pin D12----------MISO (SPI data out) |
davidr99 | 0:ab4e012489ef | 163 | /// GND----------GND (ground in) |
davidr99 | 0:ab4e012489ef | 164 | /// \endcode |
davidr99 | 0:ab4e012489ef | 165 | /// |
davidr99 | 0:ab4e012489ef | 166 | /// Caution: Arduino Due is a 3.3V part and is not 5V tolerant (so too is the nRF905 module |
davidr99 | 0:ab4e012489ef | 167 | /// so they can be connected directly together. Unlike other Arduinos the Due has it default SPI |
davidr99 | 0:ab4e012489ef | 168 | /// connections on a dedicated 6 pin SPI header in the center of the board, which is |
davidr99 | 0:ab4e012489ef | 169 | /// physically compatible with Uno, Leonardo and Mega2560. A little dot marks pin 1 on the header. |
davidr99 | 0:ab4e012489ef | 170 | /// You must connect to these |
davidr99 | 0:ab4e012489ef | 171 | /// and *not* to the usual Arduino SPI pins 11, 12 and 13. |
davidr99 | 0:ab4e012489ef | 172 | /// See http://21stdigitalhome.blogspot.com.au/2013/02/arduino-due-hardware-spi.html |
davidr99 | 0:ab4e012489ef | 173 | /// |
davidr99 | 0:ab4e012489ef | 174 | /// Connect the nRF905 to Arduino Due like this |
davidr99 | 0:ab4e012489ef | 175 | /// \code |
davidr99 | 0:ab4e012489ef | 176 | /// CPU nRF905 module |
davidr99 | 0:ab4e012489ef | 177 | /// 3V3----------VCC (3.3V) |
davidr99 | 0:ab4e012489ef | 178 | /// pin D8-----------CE (chip enable in) |
davidr99 | 0:ab4e012489ef | 179 | /// pin D9-----------TX_EN (transmit enable in) |
davidr99 | 0:ab4e012489ef | 180 | /// SS pin D10----------CSN (chip select in) |
davidr99 | 0:ab4e012489ef | 181 | /// SCK on SPI header pin 3----------SCK (SPI clock in) |
davidr99 | 0:ab4e012489ef | 182 | /// MOSI on SPI header pin 4----------MOSI (SPI Data in) |
davidr99 | 0:ab4e012489ef | 183 | /// MISO on SPI header pin 1----------MISO (SPI data out) |
davidr99 | 0:ab4e012489ef | 184 | /// GND----------GND (ground in) |
davidr99 | 0:ab4e012489ef | 185 | /// \endcode |
davidr99 | 0:ab4e012489ef | 186 | /// |
davidr99 | 0:ab4e012489ef | 187 | /// and you can then use the default constructor RH_NRF905(). |
davidr99 | 0:ab4e012489ef | 188 | /// You can override the default settings for the CE, TX_EN and CSN pins |
davidr99 | 0:ab4e012489ef | 189 | /// in the NRF905() constructor if you wish to connect the slave select CSN to other than the normal one for your |
davidr99 | 0:ab4e012489ef | 190 | /// CPU. |
davidr99 | 0:ab4e012489ef | 191 | /// |
davidr99 | 0:ab4e012489ef | 192 | /// It is possible to have 2 radios conected to one CPU, provided each radio has its own |
davidr99 | 0:ab4e012489ef | 193 | /// CSN, TX_EN and CE line (SCK, MOSI and MISO are common to both radios) |
davidr99 | 0:ab4e012489ef | 194 | /// |
davidr99 | 0:ab4e012489ef | 195 | /// \par Example programs |
davidr99 | 0:ab4e012489ef | 196 | /// |
davidr99 | 0:ab4e012489ef | 197 | /// Several example programs are provided. They work out of the box with Teensy 3.1 and Arduino Due |
davidr99 | 0:ab4e012489ef | 198 | /// connected as show above. |
davidr99 | 0:ab4e012489ef | 199 | /// |
davidr99 | 0:ab4e012489ef | 200 | /// \par Radio Performance |
davidr99 | 0:ab4e012489ef | 201 | /// |
davidr99 | 0:ab4e012489ef | 202 | /// Frequency accuracy may be debatable. |
davidr99 | 0:ab4e012489ef | 203 | /// |
davidr99 | 0:ab4e012489ef | 204 | /// \par Memory |
davidr99 | 0:ab4e012489ef | 205 | /// |
davidr99 | 0:ab4e012489ef | 206 | /// Memory usage of this class is minimal. The compiled client and server sketches are about 16000 bytes on Teensy. |
davidr99 | 0:ab4e012489ef | 207 | /// |
davidr99 | 0:ab4e012489ef | 208 | class RH_NRF905 : public RHNRFSPIDriver |
davidr99 | 0:ab4e012489ef | 209 | { |
davidr99 | 0:ab4e012489ef | 210 | public: |
davidr99 | 0:ab4e012489ef | 211 | /// \brief Convenient values for setting transmitter power in setRF() |
davidr99 | 0:ab4e012489ef | 212 | /// These are designed to agree with the values for PA_PWR |
davidr99 | 0:ab4e012489ef | 213 | /// To be passed to setRF(); |
davidr99 | 0:ab4e012489ef | 214 | typedef enum |
davidr99 | 0:ab4e012489ef | 215 | { |
davidr99 | 0:ab4e012489ef | 216 | TransmitPowerm10dBm = 0, ///< -10 dBm |
davidr99 | 0:ab4e012489ef | 217 | TransmitPowerm2dBm, ///< -2 dBm |
davidr99 | 0:ab4e012489ef | 218 | TransmitPower6dBm, ///< 6 dBm |
davidr99 | 0:ab4e012489ef | 219 | TransmitPower10dBm ///< 10 dBm |
davidr99 | 0:ab4e012489ef | 220 | } TransmitPower; |
davidr99 | 0:ab4e012489ef | 221 | |
davidr99 | 0:ab4e012489ef | 222 | /// Constructor. You can have multiple instances, but each instance must have its own |
davidr99 | 0:ab4e012489ef | 223 | /// chip enable and slave select pin. |
davidr99 | 0:ab4e012489ef | 224 | /// After constructing, you must call init() to initialise the interface |
davidr99 | 0:ab4e012489ef | 225 | /// and the radio module |
davidr99 | 0:ab4e012489ef | 226 | /// \param[in] chipEnablePin the Arduino pin to use to enable the chip for transmit/receive |
davidr99 | 0:ab4e012489ef | 227 | /// \param[in] txEnablePin the Arduino pin cponnected to the txEn pin on the radio that enable transmit mode |
davidr99 | 0:ab4e012489ef | 228 | /// \param[in] slaveSelectPin the Arduino pin number of the output to use to select the NRF905 before |
davidr99 | 0:ab4e012489ef | 229 | /// accessing it. Defaults to the normal SS pin for your Arduino (D10 for Diecimila, Uno etc, D53 for Mega, |
davidr99 | 0:ab4e012489ef | 230 | /// D10 for Maple, Teensy) |
davidr99 | 0:ab4e012489ef | 231 | /// \param[in] spi Pointer to the SPI interface object to use. |
davidr99 | 0:ab4e012489ef | 232 | /// Defaults to the standard Arduino hardware SPI interface |
davidr99 | 0:ab4e012489ef | 233 | RH_NRF905(PINS chipEnablePin, PINS txEnablePin, PINS slaveSelectPin, RHGenericSPI& spi = hardware_spi); |
davidr99 | 0:ab4e012489ef | 234 | |
davidr99 | 0:ab4e012489ef | 235 | /// Initialises this instance and the radio module connected to it. |
davidr99 | 0:ab4e012489ef | 236 | /// The following steps are taken:g |
davidr99 | 0:ab4e012489ef | 237 | /// - Set the chip enable and chip select pins to output LOW, HIGH respectively. |
davidr99 | 0:ab4e012489ef | 238 | /// - Initialise the SPI output pins |
davidr99 | 0:ab4e012489ef | 239 | /// - Initialise the SPI interface library to 8MHz (Hint, if you want to lower |
davidr99 | 0:ab4e012489ef | 240 | /// the SPI frequency (perhaps where you have other SPI shields, low voltages etc), |
davidr99 | 0:ab4e012489ef | 241 | /// call SPI.setClockDivider() after init()). |
davidr99 | 0:ab4e012489ef | 242 | /// -Flush the receiver and transmitter buffers |
davidr99 | 0:ab4e012489ef | 243 | /// - Set the radio to receive with powerUpRx(); |
davidr99 | 0:ab4e012489ef | 244 | /// \return true if everything was successful |
davidr99 | 0:ab4e012489ef | 245 | bool init(); |
davidr99 | 0:ab4e012489ef | 246 | |
davidr99 | 0:ab4e012489ef | 247 | /// Reads a single register from the NRF905 |
davidr99 | 0:ab4e012489ef | 248 | /// \param[in] reg Register number, one of NR905_REG_* |
davidr99 | 0:ab4e012489ef | 249 | /// \return The value of the register |
davidr99 | 0:ab4e012489ef | 250 | uint8_t spiReadRegister(uint8_t reg); |
davidr99 | 0:ab4e012489ef | 251 | |
davidr99 | 0:ab4e012489ef | 252 | /// Writes a single byte to the NRF905, and at the ame time reads the current STATUS register |
davidr99 | 0:ab4e012489ef | 253 | /// \param[in] reg Register number, one of NRF905_REG_* |
davidr99 | 0:ab4e012489ef | 254 | /// \param[in] val The value to write |
davidr99 | 0:ab4e012489ef | 255 | /// \return the current STATUS (read while the command is sent) |
davidr99 | 0:ab4e012489ef | 256 | uint8_t spiWriteRegister(uint8_t reg, uint8_t val); |
davidr99 | 0:ab4e012489ef | 257 | |
davidr99 | 0:ab4e012489ef | 258 | /// Reads a number of consecutive registers from the NRF905 using burst read mode |
davidr99 | 0:ab4e012489ef | 259 | /// \param[in] reg Register number of the first register, one of NRF905_REG_* |
davidr99 | 0:ab4e012489ef | 260 | /// \param[in] dest Array to write the register values to. Must be at least len bytes |
davidr99 | 0:ab4e012489ef | 261 | /// \param[in] len Number of bytes to read |
davidr99 | 0:ab4e012489ef | 262 | /// \return the current STATUS (read while the command is sent) |
davidr99 | 0:ab4e012489ef | 263 | uint8_t spiBurstReadRegister(uint8_t reg, uint8_t* dest, uint8_t len); |
davidr99 | 0:ab4e012489ef | 264 | |
davidr99 | 0:ab4e012489ef | 265 | /// Write a number of consecutive registers using burst write mode |
davidr99 | 0:ab4e012489ef | 266 | /// \param[in] reg Register number of the first register, one of NRF905_REG_* |
davidr99 | 0:ab4e012489ef | 267 | /// \param[in] src Array of new register values to write. Must be at least len bytes |
davidr99 | 0:ab4e012489ef | 268 | /// \param[in] len Number of bytes to write |
davidr99 | 0:ab4e012489ef | 269 | /// \return the current STATUS (read while the command is sent) |
davidr99 | 0:ab4e012489ef | 270 | uint8_t spiBurstWriteRegister(uint8_t reg, uint8_t* src, uint8_t len); |
davidr99 | 0:ab4e012489ef | 271 | |
davidr99 | 0:ab4e012489ef | 272 | /// Reads and returns the device status register NRF905_REG_02_DEVICE_STATUS |
davidr99 | 0:ab4e012489ef | 273 | /// \return The value of the device status register |
davidr99 | 0:ab4e012489ef | 274 | uint8_t statusRead(); |
davidr99 | 0:ab4e012489ef | 275 | |
davidr99 | 0:ab4e012489ef | 276 | /// Sets the transmit and receive channel number. |
davidr99 | 0:ab4e012489ef | 277 | /// The RF frequency used is (422.4 + channel/10) * (1+hiFrequency) MHz |
davidr99 | 0:ab4e012489ef | 278 | /// \param[in] channel The channel number. |
davidr99 | 0:ab4e012489ef | 279 | /// \param[in] hiFrequency false for low frequency band (422.4MHz and up), true for high frequency band (845MHz and up) |
davidr99 | 0:ab4e012489ef | 280 | /// \return true on success |
davidr99 | 0:ab4e012489ef | 281 | bool setChannel(uint16_t channel, bool hiFrequency = false); |
davidr99 | 0:ab4e012489ef | 282 | |
davidr99 | 0:ab4e012489ef | 283 | /// Sets the Network address. |
davidr99 | 0:ab4e012489ef | 284 | /// Only nodes with the same network address can communicate with each other. You |
davidr99 | 0:ab4e012489ef | 285 | /// can set different network addresses in different sets of nodes to isolate them from each other. |
davidr99 | 0:ab4e012489ef | 286 | /// The default network address is 0xE7E7E7E7 |
davidr99 | 0:ab4e012489ef | 287 | /// \param[in] address The new network address. Must match the network address of any receiving node(s). |
davidr99 | 0:ab4e012489ef | 288 | /// \param[in] len Number of bytes of address to set (1 to 4). |
davidr99 | 0:ab4e012489ef | 289 | /// \return true on success, false if len is not in the range 1-4 inclusive. |
davidr99 | 0:ab4e012489ef | 290 | bool setNetworkAddress(uint8_t* address, uint8_t len); |
davidr99 | 0:ab4e012489ef | 291 | |
davidr99 | 0:ab4e012489ef | 292 | /// Sets the transmitter power to use |
davidr99 | 0:ab4e012489ef | 293 | /// \param [in] power Transmitter power. One of NRF905::TransmitPower. |
davidr99 | 0:ab4e012489ef | 294 | /// \return true on success |
davidr99 | 0:ab4e012489ef | 295 | bool setRF(TransmitPower power); |
davidr99 | 0:ab4e012489ef | 296 | |
davidr99 | 0:ab4e012489ef | 297 | /// Sets the radio in power down mode. |
davidr99 | 0:ab4e012489ef | 298 | /// Sets chip enable to LOW. |
davidr99 | 0:ab4e012489ef | 299 | /// \return true on success |
davidr99 | 0:ab4e012489ef | 300 | void setModeIdle(); |
davidr99 | 0:ab4e012489ef | 301 | |
davidr99 | 0:ab4e012489ef | 302 | /// Sets the radio in RX mode. |
davidr99 | 0:ab4e012489ef | 303 | /// Sets chip enable to HIGH to enable the chip in RX mode. |
davidr99 | 0:ab4e012489ef | 304 | /// \return true on success |
davidr99 | 0:ab4e012489ef | 305 | void setModeRx(); |
davidr99 | 0:ab4e012489ef | 306 | |
davidr99 | 0:ab4e012489ef | 307 | /// Sets the radio in TX mode. |
davidr99 | 0:ab4e012489ef | 308 | /// Pulses the chip enable LOW then HIGH to enable the chip in TX mode. |
davidr99 | 0:ab4e012489ef | 309 | /// \return true on success |
davidr99 | 0:ab4e012489ef | 310 | void setModeTx(); |
davidr99 | 0:ab4e012489ef | 311 | |
davidr99 | 0:ab4e012489ef | 312 | /// Sends data to the address set by setTransmitAddress() |
davidr99 | 0:ab4e012489ef | 313 | /// Sets the radio to TX mode |
davidr99 | 0:ab4e012489ef | 314 | /// \param [in] data Data bytes to send. |
davidr99 | 0:ab4e012489ef | 315 | /// \param [in] len Number of data bytes to set in teh TX buffer. The actual size of the |
davidr99 | 0:ab4e012489ef | 316 | /// transmitted data payload is set by setPayloadSize |
davidr99 | 0:ab4e012489ef | 317 | /// \return true on success (which does not necessarily mean the receiver got the message, only that the message was |
davidr99 | 0:ab4e012489ef | 318 | /// successfully transmitted). |
davidr99 | 0:ab4e012489ef | 319 | bool send(const uint8_t* data, uint8_t len); |
davidr99 | 0:ab4e012489ef | 320 | |
davidr99 | 0:ab4e012489ef | 321 | /// Blocks until the current message (if any) |
davidr99 | 0:ab4e012489ef | 322 | /// has been transmitted |
davidr99 | 0:ab4e012489ef | 323 | /// \return true on success, false if the chip is not in transmit mode |
davidr99 | 0:ab4e012489ef | 324 | virtual bool waitPacketSent(); |
davidr99 | 0:ab4e012489ef | 325 | |
davidr99 | 0:ab4e012489ef | 326 | /// Indicates if the chip is in transmit mode and |
davidr99 | 0:ab4e012489ef | 327 | /// there is a packet currently being transmitted |
davidr99 | 0:ab4e012489ef | 328 | /// \return true if the chip is in transmit mode and there is a transmission in progress |
davidr99 | 0:ab4e012489ef | 329 | bool isSending(); |
davidr99 | 0:ab4e012489ef | 330 | |
davidr99 | 0:ab4e012489ef | 331 | /// Prints the value of a single chip register |
davidr99 | 0:ab4e012489ef | 332 | /// to the Serial device if RH_HAVE_SERIAL is defined for the current platform |
davidr99 | 0:ab4e012489ef | 333 | /// For debugging purposes only. |
davidr99 | 0:ab4e012489ef | 334 | /// \return true on success |
davidr99 | 0:ab4e012489ef | 335 | bool printRegister(uint8_t reg); |
davidr99 | 0:ab4e012489ef | 336 | |
davidr99 | 0:ab4e012489ef | 337 | /// Prints the value of all chip registers |
davidr99 | 0:ab4e012489ef | 338 | /// to the Serial device if RH_HAVE_SERIAL is defined for the current platform |
davidr99 | 0:ab4e012489ef | 339 | /// For debugging purposes only. |
davidr99 | 0:ab4e012489ef | 340 | /// \return true on success |
davidr99 | 0:ab4e012489ef | 341 | bool printRegisters(); |
davidr99 | 0:ab4e012489ef | 342 | |
davidr99 | 0:ab4e012489ef | 343 | /// Checks whether a received message is available. |
davidr99 | 0:ab4e012489ef | 344 | /// This can be called multiple times in a timeout loop |
davidr99 | 0:ab4e012489ef | 345 | /// \return true if a complete, valid message has been received and is able to be retrieved by |
davidr99 | 0:ab4e012489ef | 346 | /// recv() |
davidr99 | 0:ab4e012489ef | 347 | bool available(); |
davidr99 | 0:ab4e012489ef | 348 | |
davidr99 | 0:ab4e012489ef | 349 | /// Turns the receiver on if it not already on. |
davidr99 | 0:ab4e012489ef | 350 | /// If there is a valid message available, copy it to buf and return true |
davidr99 | 0:ab4e012489ef | 351 | /// else return false. |
davidr99 | 0:ab4e012489ef | 352 | /// If a message is copied, *len is set to the length (Caution, 0 length messages are permitted). |
davidr99 | 0:ab4e012489ef | 353 | /// You should be sure to call this function frequently enough to not miss any messages |
davidr99 | 0:ab4e012489ef | 354 | /// It is recommended that you call it in your main loop. |
davidr99 | 0:ab4e012489ef | 355 | /// \param[in] buf Location to copy the received message |
davidr99 | 0:ab4e012489ef | 356 | /// \param[in,out] len Pointer to available space in buf. Set to the actual number of octets copied. |
davidr99 | 0:ab4e012489ef | 357 | /// \return true if a valid message was copied to buf |
davidr99 | 0:ab4e012489ef | 358 | bool recv(uint8_t* buf, uint8_t* len); |
davidr99 | 0:ab4e012489ef | 359 | |
davidr99 | 0:ab4e012489ef | 360 | /// The maximum message length supported by this driver |
davidr99 | 0:ab4e012489ef | 361 | /// \return The maximum message length supported by this driver |
davidr99 | 0:ab4e012489ef | 362 | uint8_t maxMessageLength(); |
davidr99 | 0:ab4e012489ef | 363 | |
davidr99 | 0:ab4e012489ef | 364 | protected: |
davidr99 | 0:ab4e012489ef | 365 | /// Examine the revceive buffer to determine whether the message is for this node |
davidr99 | 0:ab4e012489ef | 366 | void validateRxBuf(); |
davidr99 | 0:ab4e012489ef | 367 | |
davidr99 | 0:ab4e012489ef | 368 | /// Clear our local receive buffer |
davidr99 | 0:ab4e012489ef | 369 | void clearRxBuf(); |
davidr99 | 0:ab4e012489ef | 370 | |
davidr99 | 0:ab4e012489ef | 371 | private: |
davidr99 | 0:ab4e012489ef | 372 | /// This idle mode chip configuration |
davidr99 | 0:ab4e012489ef | 373 | uint8_t _configuration; |
davidr99 | 0:ab4e012489ef | 374 | |
davidr99 | 0:ab4e012489ef | 375 | /// the number of the chip enable pin |
davidr99 | 0:ab4e012489ef | 376 | uint8_t _chipEnablePin; |
davidr99 | 0:ab4e012489ef | 377 | |
davidr99 | 0:ab4e012489ef | 378 | /// The number of the transmit enable pin |
davidr99 | 0:ab4e012489ef | 379 | uint8_t _txEnablePin; |
davidr99 | 0:ab4e012489ef | 380 | |
davidr99 | 0:ab4e012489ef | 381 | /// Number of octets in the buffer |
davidr99 | 0:ab4e012489ef | 382 | uint8_t _bufLen; |
davidr99 | 0:ab4e012489ef | 383 | |
davidr99 | 0:ab4e012489ef | 384 | /// The receiver/transmitter buffer |
davidr99 | 0:ab4e012489ef | 385 | uint8_t _buf[RH_NRF905_MAX_PAYLOAD_LEN]; |
davidr99 | 0:ab4e012489ef | 386 | |
davidr99 | 0:ab4e012489ef | 387 | /// True when there is a valid message in the buffer |
davidr99 | 0:ab4e012489ef | 388 | bool _rxBufValid; |
davidr99 | 0:ab4e012489ef | 389 | }; |
davidr99 | 0:ab4e012489ef | 390 | |
davidr99 | 0:ab4e012489ef | 391 | /// @example nrf905_client.pde |
davidr99 | 0:ab4e012489ef | 392 | /// @example nrf905_server.pde |
davidr99 | 0:ab4e012489ef | 393 | /// @example nrf905_reliable_datagram_client.pde |
davidr99 | 0:ab4e012489ef | 394 | /// @example nrf905_reliable_datagram_server.pde |
davidr99 | 0:ab4e012489ef | 395 | |
davidr99 | 0:ab4e012489ef | 396 | #endif |