Maniacbug's nRF24L01 arduino library ported to mbed. Functional with minor issues.

Fork of nRF24L01P_Maniacbug by Christiaan M

Committer:
jotaemesousa
Date:
Sat Oct 25 17:09:50 2014 +0000
Revision:
7:5ddd7a2fcb70
Parent:
6:56b693b59e16
FIX: redefenition of #define STATUS  0x07 (using a mkl05); ADD: configuration where the macros are defined (file included only in nRF24L01P_Maniacbug)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Christilut 0:eb5b89f49c35 1 /*
Christilut 0:eb5b89f49c35 2 Copyright (c) 2007 Stefan Engelke <mbox@stefanengelke.de>
Christilut 0:eb5b89f49c35 3
Christilut 2:a483f426d380 4 Permission is hereby granted, free of charge, to any person
Christilut 2:a483f426d380 5 obtaining a copy of this software and associated documentation
Christilut 2:a483f426d380 6 files (the "Software"), to deal in the Software without
Christilut 2:a483f426d380 7 restriction, including without limitation the rights to use, copy,
Christilut 2:a483f426d380 8 modify, merge, publish, distribute, sublicense, and/or sell copies
Christilut 2:a483f426d380 9 of the Software, and to permit persons to whom the Software is
Christilut 0:eb5b89f49c35 10 furnished to do so, subject to the following conditions:
Christilut 0:eb5b89f49c35 11
Christilut 2:a483f426d380 12 The above copyright notice and this permission notice shall be
Christilut 0:eb5b89f49c35 13 included in all copies or substantial portions of the Software.
Christilut 0:eb5b89f49c35 14
Christilut 2:a483f426d380 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Christilut 2:a483f426d380 16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Christilut 2:a483f426d380 17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Christilut 2:a483f426d380 18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
Christilut 2:a483f426d380 19 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
Christilut 2:a483f426d380 20 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Christilut 2:a483f426d380 21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Christilut 0:eb5b89f49c35 22 DEALINGS IN THE SOFTWARE.
Christilut 0:eb5b89f49c35 23 */
Christilut 0:eb5b89f49c35 24
Christilut 0:eb5b89f49c35 25 /*
Christilut 0:eb5b89f49c35 26 Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
Christilut 0:eb5b89f49c35 27
Christilut 0:eb5b89f49c35 28 This program is free software; you can redistribute it and/or
Christilut 0:eb5b89f49c35 29 modify it under the terms of the GNU General Public License
Christilut 0:eb5b89f49c35 30 version 2 as published by the Free Software Foundation.
Christilut 0:eb5b89f49c35 31 */
Christilut 0:eb5b89f49c35 32
Christilut 0:eb5b89f49c35 33 /**
Christilut 0:eb5b89f49c35 34 * @file RF24.h
Christilut 0:eb5b89f49c35 35 *
Christilut 0:eb5b89f49c35 36 * Class declaration for RF24 and helper enums
Christilut 0:eb5b89f49c35 37 */
Christilut 0:eb5b89f49c35 38
Christilut 0:eb5b89f49c35 39 #ifndef __RF24_H__
Christilut 0:eb5b89f49c35 40 #define __RF24_H__
Christilut 0:eb5b89f49c35 41
Christilut 2:a483f426d380 42 #include <mbed.h>
Christilut 0:eb5b89f49c35 43
Christilut 0:eb5b89f49c35 44 /**
Christilut 0:eb5b89f49c35 45 * Power Amplifier level.
Christilut 0:eb5b89f49c35 46 *
Christilut 0:eb5b89f49c35 47 * For use with setPALevel()
Christilut 0:eb5b89f49c35 48 */
Christilut 0:eb5b89f49c35 49 typedef enum { RF24_PA_MIN = 0,RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX, RF24_PA_ERROR } rf24_pa_dbm_e ;
Christilut 0:eb5b89f49c35 50
Christilut 0:eb5b89f49c35 51 /**
Christilut 0:eb5b89f49c35 52 * Data rate. How fast data moves through the air.
Christilut 0:eb5b89f49c35 53 *
Christilut 0:eb5b89f49c35 54 * For use with setDataRate()
Christilut 0:eb5b89f49c35 55 */
Christilut 0:eb5b89f49c35 56 typedef enum { RF24_1MBPS = 0, RF24_2MBPS, RF24_250KBPS } rf24_datarate_e;
Christilut 0:eb5b89f49c35 57
Christilut 0:eb5b89f49c35 58 /**
Christilut 0:eb5b89f49c35 59 * CRC Length. How big (if any) of a CRC is included.
Christilut 0:eb5b89f49c35 60 *
Christilut 0:eb5b89f49c35 61 * For use with setCRCLength()
Christilut 0:eb5b89f49c35 62 */
Christilut 0:eb5b89f49c35 63 typedef enum { RF24_CRC_DISABLED = 0, RF24_CRC_8, RF24_CRC_16 } rf24_crclength_e;
Christilut 0:eb5b89f49c35 64
Christilut 0:eb5b89f49c35 65 /**
Christilut 0:eb5b89f49c35 66 * Driver for nRF24L01(+) 2.4GHz Wireless Transceiver
Christilut 0:eb5b89f49c35 67 */
Christilut 0:eb5b89f49c35 68
Christilut 0:eb5b89f49c35 69 class RF24
Christilut 0:eb5b89f49c35 70 {
Christilut 0:eb5b89f49c35 71 private:
Christilut 0:eb5b89f49c35 72 DigitalOut ce_pin; /**< "Chip Enable" pin, activates the RX or TX role */
Christilut 0:eb5b89f49c35 73 DigitalOut csn_pin; /**< SPI Chip select */
Christilut 0:eb5b89f49c35 74 bool wide_band; /* 2Mbs data rate in use? */
Christilut 0:eb5b89f49c35 75 bool p_variant; /* False for RF24L01 and true for RF24L01P */
Christilut 0:eb5b89f49c35 76 uint8_t payload_size; /**< Fixed size of payloads */
Christilut 0:eb5b89f49c35 77 bool ack_payload_available; /**< Whether there is an ack payload waiting */
Christilut 0:eb5b89f49c35 78 bool dynamic_payloads_enabled; /**< Whether dynamic payloads are enabled. */
Christilut 0:eb5b89f49c35 79 uint8_t ack_payload_length; /**< Dynamic size of pending ack payload. */
Christilut 0:eb5b89f49c35 80 uint64_t pipe0_reading_address; /**< Last address set on pipe 0 for reading. */
Christilut 0:eb5b89f49c35 81 SPI spi;
Christilut 0:eb5b89f49c35 82 Timer mainTimer;
Christilut 0:eb5b89f49c35 83
Christilut 0:eb5b89f49c35 84 protected:
Christilut 0:eb5b89f49c35 85 /**
Christilut 0:eb5b89f49c35 86 * @name Low-level internal interface.
Christilut 0:eb5b89f49c35 87 *
Christilut 0:eb5b89f49c35 88 * Protected methods that address the chip directly. Regular users cannot
Christilut 0:eb5b89f49c35 89 * ever call these. They are documented for completeness and for developers who
Christilut 0:eb5b89f49c35 90 * may want to extend this class.
Christilut 0:eb5b89f49c35 91 */
Christilut 0:eb5b89f49c35 92 /**@{*/
Christilut 0:eb5b89f49c35 93
Christilut 0:eb5b89f49c35 94 /**
Christilut 0:eb5b89f49c35 95 * Set chip select pin
Christilut 0:eb5b89f49c35 96 *
Christilut 0:eb5b89f49c35 97 * Running SPI bus at PI_CLOCK_DIV2 so we don't waste time transferring data
Christilut 0:eb5b89f49c35 98 * and best of all, we make use of the radio's FIFO buffers. A lower speed
Christilut 0:eb5b89f49c35 99 * means we're less likely to effectively leverage our FIFOs and pay a higher
Christilut 0:eb5b89f49c35 100 * AVR runtime cost as toll.
Christilut 0:eb5b89f49c35 101 *
Christilut 0:eb5b89f49c35 102 * @param mode HIGH to take this unit off the SPI bus, LOW to put it on
Christilut 0:eb5b89f49c35 103 */
Christilut 0:eb5b89f49c35 104 void csn(int mode);
Christilut 0:eb5b89f49c35 105
Christilut 0:eb5b89f49c35 106 /**
Christilut 0:eb5b89f49c35 107 * Set chip enable
Christilut 0:eb5b89f49c35 108 *
Christilut 0:eb5b89f49c35 109 * @param level HIGH to actively begin transmission or LOW to put in standby. Please see data sheet
Christilut 0:eb5b89f49c35 110 * for a much more detailed description of this pin.
Christilut 0:eb5b89f49c35 111 */
Christilut 4:491267614a10 112 void ce(int level);
Christilut 4:491267614a10 113
Christilut 0:eb5b89f49c35 114
Christilut 0:eb5b89f49c35 115 /**
Christilut 0:eb5b89f49c35 116 * Read a chunk of data in from a register
Christilut 0:eb5b89f49c35 117 *
Christilut 0:eb5b89f49c35 118 * @param reg Which register. Use constants from nRF24L01.h
Christilut 0:eb5b89f49c35 119 * @param buf Where to put the data
Christilut 0:eb5b89f49c35 120 * @param len How many bytes of data to transfer
Christilut 0:eb5b89f49c35 121 * @return Current value of status register
Christilut 0:eb5b89f49c35 122 */
Christilut 0:eb5b89f49c35 123 uint8_t read_register(uint8_t reg, uint8_t* buf, uint8_t len);
Christilut 0:eb5b89f49c35 124
Christilut 0:eb5b89f49c35 125 /**
Christilut 0:eb5b89f49c35 126 * Read single byte from a register
Christilut 0:eb5b89f49c35 127 *
Christilut 0:eb5b89f49c35 128 * @param reg Which register. Use constants from nRF24L01.h
Christilut 0:eb5b89f49c35 129 * @return Current value of register @p reg
Christilut 0:eb5b89f49c35 130 */
Christilut 0:eb5b89f49c35 131 uint8_t read_register(uint8_t reg);
Christilut 0:eb5b89f49c35 132
Christilut 0:eb5b89f49c35 133 /**
Christilut 0:eb5b89f49c35 134 * Write a chunk of data to a register
Christilut 0:eb5b89f49c35 135 *
Christilut 0:eb5b89f49c35 136 * @param reg Which register. Use constants from nRF24L01.h
Christilut 0:eb5b89f49c35 137 * @param buf Where to get the data
Christilut 0:eb5b89f49c35 138 * @param len How many bytes of data to transfer
Christilut 0:eb5b89f49c35 139 * @return Current value of status register
Christilut 0:eb5b89f49c35 140 */
Christilut 0:eb5b89f49c35 141 uint8_t write_register(uint8_t reg, const uint8_t* buf, uint8_t len);
Christilut 0:eb5b89f49c35 142
Christilut 0:eb5b89f49c35 143 /**
Christilut 0:eb5b89f49c35 144 * Write a single byte to a register
Christilut 0:eb5b89f49c35 145 *
Christilut 0:eb5b89f49c35 146 * @param reg Which register. Use constants from nRF24L01.h
Christilut 0:eb5b89f49c35 147 * @param value The new value to write
Christilut 0:eb5b89f49c35 148 * @return Current value of status register
Christilut 0:eb5b89f49c35 149 */
Christilut 0:eb5b89f49c35 150 uint8_t write_register(uint8_t reg, uint8_t value);
Christilut 0:eb5b89f49c35 151
Christilut 0:eb5b89f49c35 152 /**
Christilut 0:eb5b89f49c35 153 * Write the transmit payload
Christilut 0:eb5b89f49c35 154 *
Christilut 0:eb5b89f49c35 155 * The size of data written is the fixed payload size, see getPayloadSize()
Christilut 0:eb5b89f49c35 156 *
Christilut 0:eb5b89f49c35 157 * @param buf Where to get the data
Christilut 0:eb5b89f49c35 158 * @param len Number of bytes to be sent
Christilut 0:eb5b89f49c35 159 * @return Current value of status register
Christilut 0:eb5b89f49c35 160 */
Christilut 0:eb5b89f49c35 161 uint8_t write_payload(const void* buf, uint8_t len);
Christilut 0:eb5b89f49c35 162
Christilut 0:eb5b89f49c35 163 /**
Christilut 0:eb5b89f49c35 164 * Read the receive payload
Christilut 0:eb5b89f49c35 165 *
Christilut 0:eb5b89f49c35 166 * The size of data read is the fixed payload size, see getPayloadSize()
Christilut 0:eb5b89f49c35 167 *
Christilut 0:eb5b89f49c35 168 * @param buf Where to put the data
Christilut 0:eb5b89f49c35 169 * @param len Maximum number of bytes to read
Christilut 0:eb5b89f49c35 170 * @return Current value of status register
Christilut 0:eb5b89f49c35 171 */
Christilut 0:eb5b89f49c35 172 uint8_t read_payload(void* buf, uint8_t len);
Christilut 0:eb5b89f49c35 173
Christilut 0:eb5b89f49c35 174
Christilut 0:eb5b89f49c35 175 /**
Christilut 0:eb5b89f49c35 176 * Decode and print the given status to stdout
Christilut 0:eb5b89f49c35 177 *
Christilut 0:eb5b89f49c35 178 * @param status Status value to print
Christilut 0:eb5b89f49c35 179 *
Christilut 0:eb5b89f49c35 180 * @warning Does nothing if stdout is not defined. See fdevopen in stdio.h
Christilut 0:eb5b89f49c35 181 */
Christilut 0:eb5b89f49c35 182 void print_status(uint8_t status);
Christilut 0:eb5b89f49c35 183
Christilut 0:eb5b89f49c35 184 /**
Christilut 0:eb5b89f49c35 185 * Decode and print the given 'observe_tx' value to stdout
Christilut 0:eb5b89f49c35 186 *
Christilut 0:eb5b89f49c35 187 * @param value The observe_tx value to print
Christilut 0:eb5b89f49c35 188 *
Christilut 0:eb5b89f49c35 189 * @warning Does nothing if stdout is not defined. See fdevopen in stdio.h
Christilut 0:eb5b89f49c35 190 */
Christilut 0:eb5b89f49c35 191 void print_observe_tx(uint8_t value);
Christilut 0:eb5b89f49c35 192
Christilut 0:eb5b89f49c35 193 /**
Christilut 0:eb5b89f49c35 194 * Print the name and value of an 8-bit register to stdout
Christilut 0:eb5b89f49c35 195 *
Christilut 0:eb5b89f49c35 196 * Optionally it can print some quantity of successive
Christilut 0:eb5b89f49c35 197 * registers on the same line. This is useful for printing a group
Christilut 0:eb5b89f49c35 198 * of related registers on one line.
Christilut 0:eb5b89f49c35 199 *
Christilut 0:eb5b89f49c35 200 * @param name Name of the register
Christilut 0:eb5b89f49c35 201 * @param reg Which register. Use constants from nRF24L01.h
Christilut 0:eb5b89f49c35 202 * @param qty How many successive registers to print
Christilut 0:eb5b89f49c35 203 */
Christilut 0:eb5b89f49c35 204 void print_byte_register(const char* name, uint8_t reg, uint8_t qty = 1);
Christilut 0:eb5b89f49c35 205
Christilut 0:eb5b89f49c35 206 /**
Christilut 0:eb5b89f49c35 207 * Print the name and value of a 40-bit address register to stdout
Christilut 0:eb5b89f49c35 208 *
Christilut 0:eb5b89f49c35 209 * Optionally it can print some quantity of successive
Christilut 0:eb5b89f49c35 210 * registers on the same line. This is useful for printing a group
Christilut 0:eb5b89f49c35 211 * of related registers on one line.
Christilut 0:eb5b89f49c35 212 *
Christilut 0:eb5b89f49c35 213 * @param name Name of the register
Christilut 0:eb5b89f49c35 214 * @param reg Which register. Use constants from nRF24L01.h
Christilut 0:eb5b89f49c35 215 * @param qty How many successive registers to print
Christilut 0:eb5b89f49c35 216 */
Christilut 0:eb5b89f49c35 217 void print_address_register(const char* name, uint8_t reg, uint8_t qty = 1);
Christilut 0:eb5b89f49c35 218
Christilut 0:eb5b89f49c35 219 /**
Christilut 0:eb5b89f49c35 220 * Turn on or off the special features of the chip
Christilut 0:eb5b89f49c35 221 *
Christilut 0:eb5b89f49c35 222 * The chip has certain 'features' which are only available when the 'features'
Christilut 0:eb5b89f49c35 223 * are enabled. See the datasheet for details.
Christilut 0:eb5b89f49c35 224 */
Christilut 0:eb5b89f49c35 225 void toggle_features(void);
Christilut 0:eb5b89f49c35 226 /**@}*/
Christilut 0:eb5b89f49c35 227
Christilut 0:eb5b89f49c35 228 public:
Christilut 0:eb5b89f49c35 229 /**
Christilut 0:eb5b89f49c35 230 * @name Primary public interface
Christilut 0:eb5b89f49c35 231 *
Christilut 0:eb5b89f49c35 232 * These are the main methods you need to operate the chip
Christilut 0:eb5b89f49c35 233 */
Christilut 0:eb5b89f49c35 234 /**@{*/
Christilut 0:eb5b89f49c35 235
Christilut 0:eb5b89f49c35 236 /**
Christilut 0:eb5b89f49c35 237 * Constructor
Christilut 0:eb5b89f49c35 238 *
Christilut 0:eb5b89f49c35 239 * Creates a new instance of this driver. Before using, you create an instance
Christilut 0:eb5b89f49c35 240 * and send in the unique pins that this chip is connected to.
Christilut 0:eb5b89f49c35 241 *
Christilut 0:eb5b89f49c35 242 * @param _cepin The pin attached to Chip Enable on the RF module
Christilut 0:eb5b89f49c35 243 * @param _cspin The pin attached to Chip Select
Christilut 0:eb5b89f49c35 244 */
Christilut 2:a483f426d380 245 RF24(PinName mosi, PinName miso, PinName sck, PinName _csnpin, PinName _cepin);
Christilut 0:eb5b89f49c35 246
Christilut 4:491267614a10 247 /**
Christilut 4:491267614a10 248 * Begin operation of the chip
Christilut 4:491267614a10 249 *
Christilut 4:491267614a10 250 * Call this in setup(), before calling any other methods.
Christilut 4:491267614a10 251 */
Christilut 4:491267614a10 252 void begin(void);
Christilut 4:491267614a10 253
Christilut 6:56b693b59e16 254 /**
Christilut 6:56b693b59e16 255 * Retrieve the current status of the chip
Christilut 6:56b693b59e16 256 *
Christilut 6:56b693b59e16 257 * @return Current value of status register
Christilut 6:56b693b59e16 258 */
Christilut 6:56b693b59e16 259 uint8_t get_status(void);
Christilut 4:491267614a10 260
Christilut 3:b13cafed7ee5 261 /**
Christilut 3:b13cafed7ee5 262 * Empty the receive buffer
Christilut 3:b13cafed7ee5 263 *
Christilut 3:b13cafed7ee5 264 * @return Current value of status register
Christilut 3:b13cafed7ee5 265 */
Christilut 3:b13cafed7ee5 266 uint8_t flush_rx(void);
Christilut 3:b13cafed7ee5 267
Christilut 3:b13cafed7ee5 268 /**
Christilut 3:b13cafed7ee5 269 * Empty the transmit buffer
Christilut 3:b13cafed7ee5 270 *
Christilut 3:b13cafed7ee5 271 * @return Current value of status register
Christilut 3:b13cafed7ee5 272 */
Christilut 3:b13cafed7ee5 273 uint8_t flush_tx(void);
Christilut 0:eb5b89f49c35 274
Christilut 0:eb5b89f49c35 275 /**
Christilut 0:eb5b89f49c35 276 * Start listening on the pipes opened for reading.
Christilut 0:eb5b89f49c35 277 *
Christilut 0:eb5b89f49c35 278 * Be sure to call openReadingPipe() first. Do not call write() while
Christilut 0:eb5b89f49c35 279 * in this mode, without first calling stopListening(). Call
Christilut 0:eb5b89f49c35 280 * isAvailable() to check for incoming traffic, and read() to get it.
Christilut 0:eb5b89f49c35 281 */
Christilut 0:eb5b89f49c35 282 void startListening(void);
Christilut 0:eb5b89f49c35 283
Christilut 0:eb5b89f49c35 284 /**
Christilut 0:eb5b89f49c35 285 * Stop listening for incoming messages
Christilut 0:eb5b89f49c35 286 *
Christilut 0:eb5b89f49c35 287 * Do this before calling write().
Christilut 0:eb5b89f49c35 288 */
Christilut 0:eb5b89f49c35 289 void stopListening(void);
Christilut 0:eb5b89f49c35 290
Christilut 0:eb5b89f49c35 291 /**
Christilut 0:eb5b89f49c35 292 * Write to the open writing pipe
Christilut 0:eb5b89f49c35 293 *
Christilut 0:eb5b89f49c35 294 * Be sure to call openWritingPipe() first to set the destination
Christilut 0:eb5b89f49c35 295 * of where to write to.
Christilut 0:eb5b89f49c35 296 *
Christilut 0:eb5b89f49c35 297 * This blocks until the message is successfully acknowledged by
Christilut 0:eb5b89f49c35 298 * the receiver or the timeout/retransmit maxima are reached. In
Christilut 0:eb5b89f49c35 299 * the current configuration, the max delay here is 60ms.
Christilut 0:eb5b89f49c35 300 *
Christilut 0:eb5b89f49c35 301 * The maximum size of data written is the fixed payload size, see
Christilut 0:eb5b89f49c35 302 * getPayloadSize(). However, you can write less, and the remainder
Christilut 0:eb5b89f49c35 303 * will just be filled with zeroes.
Christilut 0:eb5b89f49c35 304 *
Christilut 0:eb5b89f49c35 305 * @param buf Pointer to the data to be sent
Christilut 0:eb5b89f49c35 306 * @param len Number of bytes to be sent
Christilut 0:eb5b89f49c35 307 * @return True if the payload was delivered successfully false if not
Christilut 0:eb5b89f49c35 308 */
Christilut 0:eb5b89f49c35 309 bool write( const void* buf, uint8_t len );
Christilut 0:eb5b89f49c35 310
Christilut 0:eb5b89f49c35 311 /**
Christilut 0:eb5b89f49c35 312 * Test whether there are bytes available to be read
Christilut 0:eb5b89f49c35 313 *
Christilut 0:eb5b89f49c35 314 * @return True if there is a payload available, false if none is
Christilut 0:eb5b89f49c35 315 */
Christilut 0:eb5b89f49c35 316 bool available(void);
Christilut 0:eb5b89f49c35 317
Christilut 0:eb5b89f49c35 318 /**
Christilut 0:eb5b89f49c35 319 * Read the payload
Christilut 0:eb5b89f49c35 320 *
Christilut 0:eb5b89f49c35 321 * Return the last payload received
Christilut 0:eb5b89f49c35 322 *
Christilut 0:eb5b89f49c35 323 * The size of data read is the fixed payload size, see getPayloadSize()
Christilut 0:eb5b89f49c35 324 *
Christilut 0:eb5b89f49c35 325 * @note I specifically chose 'void*' as a data type to make it easier
Christilut 0:eb5b89f49c35 326 * for beginners to use. No casting needed.
Christilut 0:eb5b89f49c35 327 *
Christilut 0:eb5b89f49c35 328 * @param buf Pointer to a buffer where the data should be written
Christilut 0:eb5b89f49c35 329 * @param len Maximum number of bytes to read into the buffer
Christilut 0:eb5b89f49c35 330 * @return True if the payload was delivered successfully false if not
Christilut 0:eb5b89f49c35 331 */
Christilut 0:eb5b89f49c35 332 bool read( void* buf, uint8_t len );
Christilut 0:eb5b89f49c35 333
Christilut 0:eb5b89f49c35 334 /**
Christilut 0:eb5b89f49c35 335 * Open a pipe for writing
Christilut 0:eb5b89f49c35 336 *
Christilut 0:eb5b89f49c35 337 * Only one pipe can be open at once, but you can change the pipe
Christilut 0:eb5b89f49c35 338 * you'll listen to. Do not call this while actively listening.
Christilut 0:eb5b89f49c35 339 * Remember to stopListening() first.
Christilut 0:eb5b89f49c35 340 *
Christilut 0:eb5b89f49c35 341 * Addresses are 40-bit hex values, e.g.:
Christilut 0:eb5b89f49c35 342 *
Christilut 0:eb5b89f49c35 343 * @code
Christilut 0:eb5b89f49c35 344 * openWritingPipe(0xF0F0F0F0F0);
Christilut 0:eb5b89f49c35 345 * @endcode
Christilut 0:eb5b89f49c35 346 *
Christilut 0:eb5b89f49c35 347 * @param address The 40-bit address of the pipe to open. This can be
Christilut 0:eb5b89f49c35 348 * any value whatsoever, as long as you are the only one writing to it
Christilut 0:eb5b89f49c35 349 * and only one other radio is listening to it. Coordinate these pipe
Christilut 0:eb5b89f49c35 350 * addresses amongst nodes on the network.
Christilut 0:eb5b89f49c35 351 */
Christilut 0:eb5b89f49c35 352 void openWritingPipe(uint64_t address);
Christilut 0:eb5b89f49c35 353
Christilut 0:eb5b89f49c35 354 /**
Christilut 0:eb5b89f49c35 355 * Open a pipe for reading
Christilut 0:eb5b89f49c35 356 *
Christilut 0:eb5b89f49c35 357 * Up to 6 pipes can be open for reading at once. Open all the
Christilut 0:eb5b89f49c35 358 * reading pipes, and then call startListening().
Christilut 0:eb5b89f49c35 359 *
Christilut 0:eb5b89f49c35 360 * @see openWritingPipe
Christilut 0:eb5b89f49c35 361 *
Christilut 0:eb5b89f49c35 362 * @warning Pipes 1-5 should share the first 32 bits.
Christilut 0:eb5b89f49c35 363 * Only the least significant byte should be unique, e.g.
Christilut 0:eb5b89f49c35 364 * @code
Christilut 0:eb5b89f49c35 365 * openReadingPipe(1,0xF0F0F0F0AA);
Christilut 0:eb5b89f49c35 366 * openReadingPipe(2,0xF0F0F0F066);
Christilut 0:eb5b89f49c35 367 * @endcode
Christilut 0:eb5b89f49c35 368 *
Christilut 0:eb5b89f49c35 369 * @warning Pipe 0 is also used by the writing pipe. So if you open
Christilut 0:eb5b89f49c35 370 * pipe 0 for reading, and then startListening(), it will overwrite the
Christilut 0:eb5b89f49c35 371 * writing pipe. Ergo, do an openWritingPipe() again before write().
Christilut 0:eb5b89f49c35 372 *
Christilut 0:eb5b89f49c35 373 * @todo Enforce the restriction that pipes 1-5 must share the top 32 bits
Christilut 0:eb5b89f49c35 374 *
Christilut 0:eb5b89f49c35 375 * @param number Which pipe# to open, 0-5.
Christilut 0:eb5b89f49c35 376 * @param address The 40-bit address of the pipe to open.
Christilut 0:eb5b89f49c35 377 */
Christilut 0:eb5b89f49c35 378 void openReadingPipe(uint8_t number, uint64_t address);
Christilut 0:eb5b89f49c35 379
Christilut 0:eb5b89f49c35 380 /**@}*/
Christilut 0:eb5b89f49c35 381 /**
Christilut 0:eb5b89f49c35 382 * @name Optional Configurators
Christilut 0:eb5b89f49c35 383 *
Christilut 0:eb5b89f49c35 384 * Methods you can use to get or set the configuration of the chip.
Christilut 0:eb5b89f49c35 385 * None are required. Calling begin() sets up a reasonable set of
Christilut 0:eb5b89f49c35 386 * defaults.
Christilut 0:eb5b89f49c35 387 */
Christilut 0:eb5b89f49c35 388 /**@{*/
Christilut 0:eb5b89f49c35 389 /**
Christilut 0:eb5b89f49c35 390 * Set the number and delay of retries upon failed submit
Christilut 0:eb5b89f49c35 391 *
Christilut 0:eb5b89f49c35 392 * @param delay How long to wait between each retry, in multiples of 250us,
Christilut 0:eb5b89f49c35 393 * max is 15. 0 means 250us, 15 means 4000us.
Christilut 0:eb5b89f49c35 394 * @param count How many retries before giving up, max 15
Christilut 0:eb5b89f49c35 395 */
Christilut 0:eb5b89f49c35 396 void setRetries(uint8_t delay, uint8_t count);
Christilut 0:eb5b89f49c35 397
Christilut 0:eb5b89f49c35 398 /**
Christilut 0:eb5b89f49c35 399 * Set RF communication channel
Christilut 0:eb5b89f49c35 400 *
Christilut 0:eb5b89f49c35 401 * @param channel Which RF channel to communicate on, 0-127
Christilut 0:eb5b89f49c35 402 */
Christilut 0:eb5b89f49c35 403 void setChannel(uint8_t channel);
Christilut 0:eb5b89f49c35 404
Christilut 0:eb5b89f49c35 405 /**
Christilut 0:eb5b89f49c35 406 * Set Static Payload Size
Christilut 0:eb5b89f49c35 407 *
Christilut 0:eb5b89f49c35 408 * This implementation uses a pre-stablished fixed payload size for all
Christilut 0:eb5b89f49c35 409 * transmissions. If this method is never called, the driver will always
Christilut 0:eb5b89f49c35 410 * transmit the maximum payload size (32 bytes), no matter how much
Christilut 0:eb5b89f49c35 411 * was sent to write().
Christilut 0:eb5b89f49c35 412 *
Christilut 0:eb5b89f49c35 413 * @todo Implement variable-sized payloads feature
Christilut 0:eb5b89f49c35 414 *
Christilut 0:eb5b89f49c35 415 * @param size The number of bytes in the payload
Christilut 0:eb5b89f49c35 416 */
Christilut 0:eb5b89f49c35 417 void setPayloadSize(uint8_t size);
Christilut 0:eb5b89f49c35 418
Christilut 0:eb5b89f49c35 419 /**
Christilut 0:eb5b89f49c35 420 * Get Static Payload Size
Christilut 0:eb5b89f49c35 421 *
Christilut 0:eb5b89f49c35 422 * @see setPayloadSize()
Christilut 0:eb5b89f49c35 423 *
Christilut 0:eb5b89f49c35 424 * @return The number of bytes in the payload
Christilut 0:eb5b89f49c35 425 */
Christilut 0:eb5b89f49c35 426 uint8_t getPayloadSize(void);
Christilut 0:eb5b89f49c35 427
Christilut 0:eb5b89f49c35 428 /**
Christilut 0:eb5b89f49c35 429 * Get Dynamic Payload Size
Christilut 0:eb5b89f49c35 430 *
Christilut 0:eb5b89f49c35 431 * For dynamic payloads, this pulls the size of the payload off
Christilut 0:eb5b89f49c35 432 * the chip
Christilut 0:eb5b89f49c35 433 *
Christilut 0:eb5b89f49c35 434 * @return Payload length of last-received dynamic payload
Christilut 0:eb5b89f49c35 435 */
Christilut 0:eb5b89f49c35 436 uint8_t getDynamicPayloadSize(void);
Christilut 0:eb5b89f49c35 437
Christilut 0:eb5b89f49c35 438 /**
Christilut 0:eb5b89f49c35 439 * Enable custom payloads on the acknowledge packets
Christilut 0:eb5b89f49c35 440 *
Christilut 0:eb5b89f49c35 441 * Ack payloads are a handy way to return data back to senders without
Christilut 0:eb5b89f49c35 442 * manually changing the radio modes on both units.
Christilut 0:eb5b89f49c35 443 *
Christilut 0:eb5b89f49c35 444 * @see examples/pingpair_pl/pingpair_pl.pde
Christilut 0:eb5b89f49c35 445 */
Christilut 0:eb5b89f49c35 446 void enableAckPayload(void);
Christilut 0:eb5b89f49c35 447
Christilut 0:eb5b89f49c35 448 /**
Christilut 0:eb5b89f49c35 449 * Enable dynamically-sized payloads
Christilut 0:eb5b89f49c35 450 *
Christilut 0:eb5b89f49c35 451 * This way you don't always have to send large packets just to send them
Christilut 0:eb5b89f49c35 452 * once in a while. This enables dynamic payloads on ALL pipes.
Christilut 0:eb5b89f49c35 453 *
Christilut 0:eb5b89f49c35 454 * @see examples/pingpair_pl/pingpair_dyn.pde
Christilut 0:eb5b89f49c35 455 */
Christilut 0:eb5b89f49c35 456 void enableDynamicPayloads(void);
Christilut 0:eb5b89f49c35 457
Christilut 0:eb5b89f49c35 458 /**
Christilut 0:eb5b89f49c35 459 * Determine whether the hardware is an nRF24L01+ or not.
Christilut 0:eb5b89f49c35 460 *
Christilut 0:eb5b89f49c35 461 * @return true if the hardware is nRF24L01+ (or compatible) and false
Christilut 0:eb5b89f49c35 462 * if its not.
Christilut 0:eb5b89f49c35 463 */
Christilut 0:eb5b89f49c35 464 bool isPVariant(void) ;
Christilut 0:eb5b89f49c35 465
Christilut 0:eb5b89f49c35 466 /**
Christilut 0:eb5b89f49c35 467 * Enable or disable auto-acknowlede packets
Christilut 0:eb5b89f49c35 468 *
Christilut 0:eb5b89f49c35 469 * This is enabled by default, so it's only needed if you want to turn
Christilut 0:eb5b89f49c35 470 * it off for some reason.
Christilut 0:eb5b89f49c35 471 *
Christilut 0:eb5b89f49c35 472 * @param enable Whether to enable (true) or disable (false) auto-acks
Christilut 0:eb5b89f49c35 473 */
Christilut 0:eb5b89f49c35 474 void setAutoAck(bool enable);
Christilut 0:eb5b89f49c35 475
Christilut 0:eb5b89f49c35 476 /**
Christilut 0:eb5b89f49c35 477 * Enable or disable auto-acknowlede packets on a per pipeline basis.
Christilut 0:eb5b89f49c35 478 *
Christilut 0:eb5b89f49c35 479 * AA is enabled by default, so it's only needed if you want to turn
Christilut 0:eb5b89f49c35 480 * it off/on for some reason on a per pipeline basis.
Christilut 0:eb5b89f49c35 481 *
Christilut 0:eb5b89f49c35 482 * @param pipe Which pipeline to modify
Christilut 0:eb5b89f49c35 483 * @param enable Whether to enable (true) or disable (false) auto-acks
Christilut 0:eb5b89f49c35 484 */
Christilut 0:eb5b89f49c35 485 void setAutoAck( uint8_t pipe, bool enable ) ;
Christilut 0:eb5b89f49c35 486
Christilut 0:eb5b89f49c35 487 /**
Christilut 0:eb5b89f49c35 488 * Set Power Amplifier (PA) level to one of four levels.
Christilut 0:eb5b89f49c35 489 * Relative mnemonics have been used to allow for future PA level
Christilut 0:eb5b89f49c35 490 * changes. According to 6.5 of the nRF24L01+ specification sheet,
Christilut 0:eb5b89f49c35 491 * they translate to: RF24_PA_MIN=-18dBm, RF24_PA_LOW=-12dBm,
Christilut 0:eb5b89f49c35 492 * RF24_PA_MED=-6dBM, and RF24_PA_HIGH=0dBm.
Christilut 0:eb5b89f49c35 493 *
Christilut 0:eb5b89f49c35 494 * @param level Desired PA level.
Christilut 0:eb5b89f49c35 495 */
Christilut 0:eb5b89f49c35 496 void setPALevel( rf24_pa_dbm_e level ) ;
Christilut 0:eb5b89f49c35 497
Christilut 0:eb5b89f49c35 498 /**
Christilut 0:eb5b89f49c35 499 * Fetches the current PA level.
Christilut 0:eb5b89f49c35 500 *
Christilut 0:eb5b89f49c35 501 * @return Returns a value from the rf24_pa_dbm_e enum describing
Christilut 0:eb5b89f49c35 502 * the current PA setting. Please remember, all values represented
Christilut 0:eb5b89f49c35 503 * by the enum mnemonics are negative dBm. See setPALevel for
Christilut 0:eb5b89f49c35 504 * return value descriptions.
Christilut 0:eb5b89f49c35 505 */
Christilut 0:eb5b89f49c35 506 rf24_pa_dbm_e getPALevel( void ) ;
Christilut 0:eb5b89f49c35 507
Christilut 0:eb5b89f49c35 508 /**
Christilut 0:eb5b89f49c35 509 * Set the transmission data rate
Christilut 0:eb5b89f49c35 510 *
Christilut 0:eb5b89f49c35 511 * @warning setting RF24_250KBPS will fail for non-plus units
Christilut 0:eb5b89f49c35 512 *
Christilut 0:eb5b89f49c35 513 * @param speed RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
Christilut 0:eb5b89f49c35 514 * @return true if the change was successful
Christilut 0:eb5b89f49c35 515 */
Christilut 0:eb5b89f49c35 516 bool setDataRate(rf24_datarate_e speed);
Christilut 0:eb5b89f49c35 517
Christilut 0:eb5b89f49c35 518 /**
Christilut 0:eb5b89f49c35 519 * Fetches the transmission data rate
Christilut 0:eb5b89f49c35 520 *
Christilut 0:eb5b89f49c35 521 * @return Returns the hardware's currently configured datarate. The value
Christilut 0:eb5b89f49c35 522 * is one of 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS, as defined in the
Christilut 0:eb5b89f49c35 523 * rf24_datarate_e enum.
Christilut 0:eb5b89f49c35 524 */
Christilut 0:eb5b89f49c35 525 rf24_datarate_e getDataRate( void ) ;
Christilut 0:eb5b89f49c35 526
Christilut 0:eb5b89f49c35 527 /**
Christilut 0:eb5b89f49c35 528 * Set the CRC length
Christilut 0:eb5b89f49c35 529 *
Christilut 0:eb5b89f49c35 530 * @param length RF24_CRC_8 for 8-bit or RF24_CRC_16 for 16-bit
Christilut 0:eb5b89f49c35 531 */
Christilut 0:eb5b89f49c35 532 void setCRCLength(rf24_crclength_e length);
Christilut 0:eb5b89f49c35 533
Christilut 0:eb5b89f49c35 534 /**
Christilut 0:eb5b89f49c35 535 * Get the CRC length
Christilut 0:eb5b89f49c35 536 *
Christilut 0:eb5b89f49c35 537 * @return RF24_DISABLED if disabled or RF24_CRC_8 for 8-bit or RF24_CRC_16 for 16-bit
Christilut 0:eb5b89f49c35 538 */
Christilut 0:eb5b89f49c35 539 rf24_crclength_e getCRCLength(void);
Christilut 0:eb5b89f49c35 540
Christilut 0:eb5b89f49c35 541 /**
Christilut 0:eb5b89f49c35 542 * Disable CRC validation
Christilut 0:eb5b89f49c35 543 *
Christilut 0:eb5b89f49c35 544 */
Christilut 0:eb5b89f49c35 545 void disableCRC( void ) ;
Christilut 0:eb5b89f49c35 546
Christilut 0:eb5b89f49c35 547 /**@}*/
Christilut 0:eb5b89f49c35 548 /**
Christilut 0:eb5b89f49c35 549 * @name Advanced Operation
Christilut 0:eb5b89f49c35 550 *
Christilut 0:eb5b89f49c35 551 * Methods you can use to drive the chip in more advanced ways
Christilut 0:eb5b89f49c35 552 */
Christilut 0:eb5b89f49c35 553 /**@{*/
Christilut 0:eb5b89f49c35 554
Christilut 0:eb5b89f49c35 555 /**
Christilut 0:eb5b89f49c35 556 * Print a giant block of debugging information to stdout
Christilut 0:eb5b89f49c35 557 *
Christilut 0:eb5b89f49c35 558 * @warning Does nothing if stdout is not defined. See fdevopen in stdio.h
Christilut 0:eb5b89f49c35 559 */
Christilut 0:eb5b89f49c35 560 void printDetails(void);
Christilut 0:eb5b89f49c35 561
Christilut 0:eb5b89f49c35 562 /**
Christilut 0:eb5b89f49c35 563 * Enter low-power mode
Christilut 0:eb5b89f49c35 564 *
Christilut 0:eb5b89f49c35 565 * To return to normal power mode, either write() some data or
Christilut 0:eb5b89f49c35 566 * startListening, or powerUp().
Christilut 0:eb5b89f49c35 567 */
Christilut 0:eb5b89f49c35 568 void powerDown(void);
Christilut 0:eb5b89f49c35 569
Christilut 0:eb5b89f49c35 570 /**
Christilut 0:eb5b89f49c35 571 * Leave low-power mode - making radio more responsive
Christilut 0:eb5b89f49c35 572 *
Christilut 0:eb5b89f49c35 573 * To return to low power mode, call powerDown().
Christilut 0:eb5b89f49c35 574 */
Christilut 0:eb5b89f49c35 575 void powerUp(void) ;
Christilut 0:eb5b89f49c35 576
Christilut 0:eb5b89f49c35 577 /**
Christilut 0:eb5b89f49c35 578 * Test whether there are bytes available to be read
Christilut 0:eb5b89f49c35 579 *
Christilut 0:eb5b89f49c35 580 * Use this version to discover on which pipe the message
Christilut 0:eb5b89f49c35 581 * arrived.
Christilut 0:eb5b89f49c35 582 *
Christilut 0:eb5b89f49c35 583 * @param[out] pipe_num Which pipe has the payload available
Christilut 0:eb5b89f49c35 584 * @return True if there is a payload available, false if none is
Christilut 0:eb5b89f49c35 585 */
Christilut 0:eb5b89f49c35 586 bool available(uint8_t* pipe_num);
Christilut 0:eb5b89f49c35 587
Christilut 0:eb5b89f49c35 588 /**
Christilut 0:eb5b89f49c35 589 * Non-blocking write to the open writing pipe
Christilut 0:eb5b89f49c35 590 *
Christilut 0:eb5b89f49c35 591 * Just like write(), but it returns immediately. To find out what happened
Christilut 0:eb5b89f49c35 592 * to the send, catch the IRQ and then call whatHappened().
Christilut 0:eb5b89f49c35 593 *
Christilut 0:eb5b89f49c35 594 * @see write()
Christilut 0:eb5b89f49c35 595 * @see whatHappened()
Christilut 0:eb5b89f49c35 596 *
Christilut 0:eb5b89f49c35 597 * @param buf Pointer to the data to be sent
Christilut 0:eb5b89f49c35 598 * @param len Number of bytes to be sent
Christilut 0:eb5b89f49c35 599 * @return True if the payload was delivered successfully false if not
Christilut 0:eb5b89f49c35 600 */
Christilut 0:eb5b89f49c35 601 void startWrite( const void* buf, uint8_t len );
Christilut 0:eb5b89f49c35 602
Christilut 0:eb5b89f49c35 603 /**
Christilut 0:eb5b89f49c35 604 * Write an ack payload for the specified pipe
Christilut 0:eb5b89f49c35 605 *
Christilut 0:eb5b89f49c35 606 * The next time a message is received on @p pipe, the data in @p buf will
Christilut 0:eb5b89f49c35 607 * be sent back in the acknowledgement.
Christilut 0:eb5b89f49c35 608 *
Christilut 0:eb5b89f49c35 609 * @warning According to the data sheet, only three of these can be pending
Christilut 0:eb5b89f49c35 610 * at any time. I have not tested this.
Christilut 0:eb5b89f49c35 611 *
Christilut 0:eb5b89f49c35 612 * @param pipe Which pipe# (typically 1-5) will get this response.
Christilut 0:eb5b89f49c35 613 * @param buf Pointer to data that is sent
Christilut 0:eb5b89f49c35 614 * @param len Length of the data to send, up to 32 bytes max. Not affected
Christilut 0:eb5b89f49c35 615 * by the static payload set by setPayloadSize().
Christilut 0:eb5b89f49c35 616 */
Christilut 0:eb5b89f49c35 617 void writeAckPayload(uint8_t pipe, const void* buf, uint8_t len);
Christilut 0:eb5b89f49c35 618
Christilut 0:eb5b89f49c35 619 /**
Christilut 0:eb5b89f49c35 620 * Determine if an ack payload was received in the most recent call to
Christilut 0:eb5b89f49c35 621 * write().
Christilut 0:eb5b89f49c35 622 *
Christilut 0:eb5b89f49c35 623 * Call read() to retrieve the ack payload.
Christilut 0:eb5b89f49c35 624 *
Christilut 0:eb5b89f49c35 625 * @warning Calling this function clears the internal flag which indicates
Christilut 0:eb5b89f49c35 626 * a payload is available. If it returns true, you must read the packet
Christilut 0:eb5b89f49c35 627 * out as the very next interaction with the radio, or the results are
Christilut 0:eb5b89f49c35 628 * undefined.
Christilut 0:eb5b89f49c35 629 *
Christilut 0:eb5b89f49c35 630 * @return True if an ack payload is available.
Christilut 0:eb5b89f49c35 631 */
Christilut 0:eb5b89f49c35 632 bool isAckPayloadAvailable(void);
Christilut 0:eb5b89f49c35 633
Christilut 0:eb5b89f49c35 634 /**
Christilut 0:eb5b89f49c35 635 * Call this when you get an interrupt to find out why
Christilut 0:eb5b89f49c35 636 *
Christilut 0:eb5b89f49c35 637 * Tells you what caused the interrupt, and clears the state of
Christilut 0:eb5b89f49c35 638 * interrupts.
Christilut 0:eb5b89f49c35 639 *
Christilut 0:eb5b89f49c35 640 * @param[out] tx_ok The send was successful (TX_DS)
Christilut 0:eb5b89f49c35 641 * @param[out] tx_fail The send failed, too many retries (MAX_RT)
Christilut 0:eb5b89f49c35 642 * @param[out] rx_ready There is a message waiting to be read (RX_DS)
Christilut 0:eb5b89f49c35 643 */
Christilut 0:eb5b89f49c35 644 void whatHappened(bool& tx_ok,bool& tx_fail,bool& rx_ready);
Christilut 0:eb5b89f49c35 645
Christilut 0:eb5b89f49c35 646 /**
Christilut 0:eb5b89f49c35 647 * Test whether there was a carrier on the line for the
Christilut 0:eb5b89f49c35 648 * previous listening period.
Christilut 0:eb5b89f49c35 649 *
Christilut 0:eb5b89f49c35 650 * Useful to check for interference on the current channel.
Christilut 0:eb5b89f49c35 651 *
Christilut 0:eb5b89f49c35 652 * @return true if was carrier, false if not
Christilut 0:eb5b89f49c35 653 */
Christilut 0:eb5b89f49c35 654 bool testCarrier(void);
Christilut 0:eb5b89f49c35 655
Christilut 0:eb5b89f49c35 656 /**
Christilut 0:eb5b89f49c35 657 * Test whether a signal (carrier or otherwise) greater than
Christilut 0:eb5b89f49c35 658 * or equal to -64dBm is present on the channel. Valid only
Christilut 0:eb5b89f49c35 659 * on nRF24L01P (+) hardware. On nRF24L01, use testCarrier().
Christilut 0:eb5b89f49c35 660 *
Christilut 0:eb5b89f49c35 661 * Useful to check for interference on the current channel and
Christilut 0:eb5b89f49c35 662 * channel hopping strategies.
Christilut 0:eb5b89f49c35 663 *
Christilut 0:eb5b89f49c35 664 * @return true if signal => -64dBm, false if not
Christilut 0:eb5b89f49c35 665 */
Christilut 0:eb5b89f49c35 666 bool testRPD(void) ;
Christilut 0:eb5b89f49c35 667
Christilut 2:a483f426d380 668 uint8_t min(uint8_t, uint8_t);
Christilut 0:eb5b89f49c35 669 };
Christilut 0:eb5b89f49c35 670
Christilut 0:eb5b89f49c35 671
Christilut 2:a483f426d380 672 #endif // __RF24_H__
Christilut 2:a483f426d380 673 // vim:ai:cin:sts=2 sw=2 ft=cpp