TMRh20 ported to MBED

Fork of TMRh20 by BME SmartLab

Committer:
gume
Date:
Thu Mar 17 07:19:37 2016 +0000
Revision:
3:13e43d3101a5
Parent:
1:8f889354678f
Child:
6:15a3bbf90fe9
- Fixed SPI problems. The problems was the initilaztion order. SPI has to be initialized after its CE/CS pins

Who changed what in which revision?

UserRevisionLine numberNew contents of line
gume 0:163155b607df 1 /*
gume 0:163155b607df 2 Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
gume 0:163155b607df 3
gume 0:163155b607df 4 This program is free software; you can redistribute it and/or
gume 0:163155b607df 5 modify it under the terms of the GNU General Public License
gume 0:163155b607df 6 version 2 as published by the Free Software Foundation.
gume 0:163155b607df 7 */
gume 0:163155b607df 8
gume 0:163155b607df 9 /**
gume 0:163155b607df 10 * @file RF24.h
gume 0:163155b607df 11 *
gume 0:163155b607df 12 * Class declaration for RF24 and helper enums
gume 0:163155b607df 13 */
gume 0:163155b607df 14
gume 0:163155b607df 15 #ifndef __RF24_H__
gume 0:163155b607df 16 #define __RF24_H__
gume 0:163155b607df 17
gume 0:163155b607df 18 #include "mbed.h"
gume 0:163155b607df 19 #include "RF24_config.h"
gume 0:163155b607df 20
gume 0:163155b607df 21 /**
gume 0:163155b607df 22 * Power Amplifier level.
gume 0:163155b607df 23 *
gume 0:163155b607df 24 * For use with setPALevel()
gume 0:163155b607df 25 */
gume 0:163155b607df 26 typedef enum { RF24_PA_MIN = 0,RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX, RF24_PA_ERROR } rf24_pa_dbm_e ;
gume 0:163155b607df 27
gume 0:163155b607df 28 /**
gume 0:163155b607df 29 * Data rate. How fast data moves through the air.
gume 0:163155b607df 30 *
gume 0:163155b607df 31 * For use with setDataRate()
gume 0:163155b607df 32 */
gume 0:163155b607df 33 typedef enum { RF24_1MBPS = 0, RF24_2MBPS, RF24_250KBPS } rf24_datarate_e;
gume 0:163155b607df 34
gume 0:163155b607df 35 /**
gume 0:163155b607df 36 * CRC Length. How big (if any) of a CRC is included.
gume 0:163155b607df 37 *
gume 0:163155b607df 38 * For use with setCRCLength()
gume 0:163155b607df 39 */
gume 0:163155b607df 40 typedef enum { RF24_CRC_DISABLED = 0, RF24_CRC_8, RF24_CRC_16 } rf24_crclength_e;
gume 0:163155b607df 41
gume 0:163155b607df 42 /**
gume 0:163155b607df 43 * Driver for nRF24L01(+) 2.4GHz Wireless Transceiver
gume 0:163155b607df 44 */
gume 0:163155b607df 45
gume 0:163155b607df 46 class RF24
gume 0:163155b607df 47 {
gume 0:163155b607df 48 private:
gume 0:163155b607df 49
gume 0:163155b607df 50 DigitalOut ce_pin; /**< "Chip Enable" pin, activates the RX or TX role */
gume 0:163155b607df 51 DigitalOut csn_pin; /**< SPI Chip select */
gume 3:13e43d3101a5 52 //uint16_t spi_speed; /**< SPI Bus Speed */
gume 0:163155b607df 53
gume 0:163155b607df 54 uint8_t spi_rxbuff[32+1] ; //SPI receive buffer (payload max 32 bytes)
gume 0:163155b607df 55 uint8_t spi_txbuff[32+1] ; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command)
gume 0:163155b607df 56
gume 0:163155b607df 57 bool p_variant; /* False for RF24L01 and true for RF24L01P */
gume 0:163155b607df 58 uint8_t payload_size; /**< Fixed size of payloads */
gume 0:163155b607df 59 bool dynamic_payloads_enabled; /**< Whether dynamic payloads are enabled. */
gume 0:163155b607df 60 uint8_t pipe0_reading_address[5]; /**< Last address set on pipe 0 for reading. */
gume 0:163155b607df 61 uint8_t addr_width; /**< The address width to use - 3,4 or 5 bytes. */
gume 0:163155b607df 62 uint32_t txRxDelay; /**< Var for adjusting delays depending on datarate */
gume 0:163155b607df 63
gume 3:13e43d3101a5 64 SPI spi;
gume 3:13e43d3101a5 65
gume 0:163155b607df 66 Timer mainTimer;
gume 0:163155b607df 67
gume 0:163155b607df 68
gume 0:163155b607df 69 protected:
gume 0:163155b607df 70 /**
gume 0:163155b607df 71 * SPI transactions
gume 0:163155b607df 72 *
gume 0:163155b607df 73 * Common code for SPI transactions including CSN toggle
gume 0:163155b607df 74 *
gume 0:163155b607df 75 */
gume 3:13e43d3101a5 76 void beginTransaction(); // inline removed
gume 0:163155b607df 77
gume 3:13e43d3101a5 78 void endTransaction(); // inline removed
gume 0:163155b607df 79
gume 0:163155b607df 80 public:
gume 0:163155b607df 81
gume 0:163155b607df 82 /**
gume 0:163155b607df 83 * @name Primary public interface
gume 0:163155b607df 84 *
gume 0:163155b607df 85 * These are the main methods you need to operate the chip
gume 0:163155b607df 86 */
gume 0:163155b607df 87 /**@{*/
gume 0:163155b607df 88
gume 0:163155b607df 89 /**
gume 0:163155b607df 90 * Arduino Constructor
gume 0:163155b607df 91 *
gume 0:163155b607df 92 * Creates a new instance of this driver. Before using, you create an instance
gume 0:163155b607df 93 * and send in the unique pins that this chip is connected to.
gume 0:163155b607df 94 *
gume 0:163155b607df 95 * @param _cepin The pin attached to Chip Enable on the RF module
gume 0:163155b607df 96 * @param _cspin The pin attached to Chip Select
gume 0:163155b607df 97 */
gume 1:8f889354678f 98 RF24(PinName mosi, PinName miso, PinName sck, PinName _cspin, PinName _cepin);
gume 0:163155b607df 99
gume 0:163155b607df 100 virtual ~RF24() {};
gume 0:163155b607df 101
gume 0:163155b607df 102 /**
gume 0:163155b607df 103 * Begin operation of the chip
gume 0:163155b607df 104 *
gume 0:163155b607df 105 * Call this in setup(), before calling any other methods.
gume 0:163155b607df 106 * @code radio.begin() @endcode
gume 0:163155b607df 107 */
gume 0:163155b607df 108 bool begin(void);
gume 3:13e43d3101a5 109 void begin_MB(void);
gume 0:163155b607df 110
gume 0:163155b607df 111 /**
gume 0:163155b607df 112 * Start listening on the pipes opened for reading.
gume 0:163155b607df 113 *
gume 0:163155b607df 114 * 1. Be sure to call openReadingPipe() first.
gume 0:163155b607df 115 * 2. Do not call write() while in this mode, without first calling stopListening().
gume 0:163155b607df 116 * 3. Call available() to check for incoming traffic, and read() to get it.
gume 0:163155b607df 117 *
gume 0:163155b607df 118 * @code
gume 0:163155b607df 119 * Open reading pipe 1 using address CCCECCCECC
gume 0:163155b607df 120 *
gume 0:163155b607df 121 * byte address[] = { 0xCC,0xCE,0xCC,0xCE,0xCC };
gume 0:163155b607df 122 * radio.openReadingPipe(1,address);
gume 0:163155b607df 123 * radio.startListening();
gume 0:163155b607df 124 * @endcode
gume 0:163155b607df 125 */
gume 0:163155b607df 126 void startListening(void);
gume 0:163155b607df 127
gume 0:163155b607df 128 /**
gume 0:163155b607df 129 * Stop listening for incoming messages, and switch to transmit mode.
gume 0:163155b607df 130 *
gume 0:163155b607df 131 * Do this before calling write().
gume 0:163155b607df 132 * @code
gume 0:163155b607df 133 * radio.stopListening();
gume 0:163155b607df 134 * radio.write(&data,sizeof(data));
gume 0:163155b607df 135 * @endcode
gume 0:163155b607df 136 */
gume 0:163155b607df 137 void stopListening(void);
gume 0:163155b607df 138
gume 0:163155b607df 139 /**
gume 0:163155b607df 140 * Check whether there are bytes available to be read
gume 0:163155b607df 141 * @code
gume 0:163155b607df 142 * if(radio.available()){
gume 0:163155b607df 143 * radio.read(&data,sizeof(data));
gume 0:163155b607df 144 * }
gume 0:163155b607df 145 * @endcode
gume 0:163155b607df 146 * @return True if there is a payload available, false if none is
gume 0:163155b607df 147 */
gume 0:163155b607df 148 bool available(void);
gume 0:163155b607df 149
gume 0:163155b607df 150 /**
gume 0:163155b607df 151 * Read the available payload
gume 0:163155b607df 152 *
gume 0:163155b607df 153 * The size of data read is the fixed payload size, see getPayloadSize()
gume 0:163155b607df 154 *
gume 0:163155b607df 155 * @note I specifically chose 'void*' as a data type to make it easier
gume 0:163155b607df 156 * for beginners to use. No casting needed.
gume 0:163155b607df 157 *
gume 0:163155b607df 158 * @note No longer boolean. Use available to determine if packets are
gume 0:163155b607df 159 * available. Interrupt flags are now cleared during reads instead of
gume 0:163155b607df 160 * when calling available().
gume 0:163155b607df 161 *
gume 0:163155b607df 162 * @param buf Pointer to a buffer where the data should be written
gume 0:163155b607df 163 * @param len Maximum number of bytes to read into the buffer
gume 0:163155b607df 164 *
gume 0:163155b607df 165 * @code
gume 0:163155b607df 166 * if(radio.available()){
gume 0:163155b607df 167 * radio.read(&data,sizeof(data));
gume 0:163155b607df 168 * }
gume 0:163155b607df 169 * @endcode
gume 0:163155b607df 170 * @return No return value. Use available().
gume 0:163155b607df 171 */
gume 0:163155b607df 172 void read( void* buf, uint8_t len );
gume 0:163155b607df 173
gume 0:163155b607df 174 /**
gume 0:163155b607df 175 * Be sure to call openWritingPipe() first to set the destination
gume 0:163155b607df 176 * of where to write to.
gume 0:163155b607df 177 *
gume 0:163155b607df 178 * This blocks until the message is successfully acknowledged by
gume 0:163155b607df 179 * the receiver or the timeout/retransmit maxima are reached. In
gume 0:163155b607df 180 * the current configuration, the max delay here is 60-70ms.
gume 0:163155b607df 181 *
gume 0:163155b607df 182 * The maximum size of data written is the fixed payload size, see
gume 0:163155b607df 183 * getPayloadSize(). However, you can write less, and the remainder
gume 0:163155b607df 184 * will just be filled with zeroes.
gume 0:163155b607df 185 *
gume 0:163155b607df 186 * TX/RX/RT interrupt flags will be cleared every time write is called
gume 0:163155b607df 187 *
gume 0:163155b607df 188 * @param buf Pointer to the data to be sent
gume 0:163155b607df 189 * @param len Number of bytes to be sent
gume 0:163155b607df 190 *
gume 0:163155b607df 191 * @code
gume 0:163155b607df 192 * radio.stopListening();
gume 0:163155b607df 193 * radio.write(&data,sizeof(data));
gume 0:163155b607df 194 * @endcode
gume 0:163155b607df 195 * @return True if the payload was delivered successfully false if not
gume 0:163155b607df 196 */
gume 0:163155b607df 197 bool write( const void* buf, uint8_t len );
gume 0:163155b607df 198
gume 0:163155b607df 199 /**
gume 0:163155b607df 200 * New: Open a pipe for writing via byte array. Old addressing format retained
gume 0:163155b607df 201 * for compatibility.
gume 0:163155b607df 202 *
gume 0:163155b607df 203 * Only one writing pipe can be open at once, but you can change the address
gume 0:163155b607df 204 * you'll write to. Call stopListening() first.
gume 0:163155b607df 205 *
gume 0:163155b607df 206 * Addresses are assigned via a byte array, default is 5 byte address length
gume 0:163155b607df 207 s *
gume 0:163155b607df 208 * @code
gume 0:163155b607df 209 * uint8_t addresses[][6] = {"1Node","2Node"};
gume 0:163155b607df 210 * radio.openWritingPipe(addresses[0]);
gume 0:163155b607df 211 * @endcode
gume 0:163155b607df 212 * @code
gume 0:163155b607df 213 * uint8_t address[] = { 0xCC,0xCE,0xCC,0xCE,0xCC };
gume 0:163155b607df 214 * radio.openWritingPipe(address);
gume 0:163155b607df 215 * address[0] = 0x33;
gume 0:163155b607df 216 * radio.openReadingPipe(1,address);
gume 0:163155b607df 217 * @endcode
gume 0:163155b607df 218 * @see setAddressWidth
gume 0:163155b607df 219 *
gume 0:163155b607df 220 * @param address The address of the pipe to open. Coordinate these pipe
gume 0:163155b607df 221 * addresses amongst nodes on the network.
gume 0:163155b607df 222 */
gume 0:163155b607df 223
gume 0:163155b607df 224 void openWritingPipe(const uint8_t *address);
gume 0:163155b607df 225
gume 0:163155b607df 226 /**
gume 0:163155b607df 227 * Open a pipe for reading
gume 0:163155b607df 228 *
gume 0:163155b607df 229 * Up to 6 pipes can be open for reading at once. Open all the required
gume 0:163155b607df 230 * reading pipes, and then call startListening().
gume 0:163155b607df 231 *
gume 0:163155b607df 232 * @see openWritingPipe
gume 0:163155b607df 233 * @see setAddressWidth
gume 0:163155b607df 234 *
gume 0:163155b607df 235 * @note Pipes 0 and 1 will store a full 5-byte address. Pipes 2-5 will technically
gume 0:163155b607df 236 * only store a single byte, borrowing up to 4 additional bytes from pipe #1 per the
gume 0:163155b607df 237 * assigned address width.
gume 0:163155b607df 238 * @warning Pipes 1-5 should share the same address, except the first byte.
gume 0:163155b607df 239 * Only the first byte in the array should be unique, e.g.
gume 0:163155b607df 240 * @code
gume 0:163155b607df 241 * uint8_t addresses[][6] = {"1Node","2Node"};
gume 0:163155b607df 242 * openReadingPipe(1,addresses[0]);
gume 0:163155b607df 243 * openReadingPipe(2,addresses[1]);
gume 0:163155b607df 244 * @endcode
gume 0:163155b607df 245 *
gume 0:163155b607df 246 * @warning Pipe 0 is also used by the writing pipe. So if you open
gume 0:163155b607df 247 * pipe 0 for reading, and then startListening(), it will overwrite the
gume 0:163155b607df 248 * writing pipe. Ergo, do an openWritingPipe() again before write().
gume 0:163155b607df 249 *
gume 0:163155b607df 250 * @param number Which pipe# to open, 0-5.
gume 0:163155b607df 251 * @param address The 24, 32 or 40 bit address of the pipe to open.
gume 0:163155b607df 252 */
gume 0:163155b607df 253
gume 0:163155b607df 254 void openReadingPipe(uint8_t number, const uint8_t *address);
gume 0:163155b607df 255
gume 0:163155b607df 256 /**@}*/
gume 0:163155b607df 257 /**
gume 0:163155b607df 258 * @name Advanced Operation
gume 0:163155b607df 259 *
gume 0:163155b607df 260 * Methods you can use to drive the chip in more advanced ways
gume 0:163155b607df 261 */
gume 0:163155b607df 262 /**@{*/
gume 0:163155b607df 263
gume 0:163155b607df 264 /**
gume 0:163155b607df 265 * Print a giant block of debugging information to stdout
gume 0:163155b607df 266 *
gume 0:163155b607df 267 * @warning Does nothing if stdout is not defined. See fdevopen in stdio.h
gume 0:163155b607df 268 * The printf.h file is included with the library for Arduino.
gume 0:163155b607df 269 * @code
gume 0:163155b607df 270 * #include <printf.h>
gume 0:163155b607df 271 * setup(){
gume 0:163155b607df 272 * Serial.begin(115200);
gume 0:163155b607df 273 * printf_begin();
gume 0:163155b607df 274 * ...
gume 0:163155b607df 275 * }
gume 0:163155b607df 276 * @endcode
gume 0:163155b607df 277 */
gume 0:163155b607df 278 void printDetails(void);
gume 0:163155b607df 279
gume 0:163155b607df 280 /**
gume 0:163155b607df 281 * Test whether there are bytes available to be read in the
gume 0:163155b607df 282 * FIFO buffers.
gume 0:163155b607df 283 *
gume 0:163155b607df 284 * @param[out] pipe_num Which pipe has the payload available
gume 0:163155b607df 285 *
gume 0:163155b607df 286 * @code
gume 0:163155b607df 287 * uint8_t pipeNum;
gume 0:163155b607df 288 * if(radio.available(&pipeNum)){
gume 0:163155b607df 289 * radio.read(&data,sizeof(data));
gume 0:163155b607df 290 * Serial.print("Got data on pipe");
gume 0:163155b607df 291 * Serial.println(pipeNum);
gume 0:163155b607df 292 * }
gume 0:163155b607df 293 * @endcode
gume 0:163155b607df 294 * @return True if there is a payload available, false if none is
gume 0:163155b607df 295 */
gume 0:163155b607df 296 bool available(uint8_t* pipe_num);
gume 0:163155b607df 297
gume 0:163155b607df 298 /**
gume 0:163155b607df 299 * Check if the radio needs to be read. Can be used to prevent data loss
gume 0:163155b607df 300 * @return True if all three 32-byte radio buffers are full
gume 0:163155b607df 301 */
gume 0:163155b607df 302 bool rxFifoFull();
gume 0:163155b607df 303
gume 0:163155b607df 304 /**
gume 0:163155b607df 305 * Enter low-power mode
gume 0:163155b607df 306 *
gume 0:163155b607df 307 * To return to normal power mode, call powerUp().
gume 0:163155b607df 308 *
gume 0:163155b607df 309 * @note After calling startListening(), a basic radio will consume about 13.5mA
gume 0:163155b607df 310 * at max PA level.
gume 0:163155b607df 311 * During active transmission, the radio will consume about 11.5mA, but this will
gume 0:163155b607df 312 * be reduced to 26uA (.026mA) between sending.
gume 0:163155b607df 313 * In full powerDown mode, the radio will consume approximately 900nA (.0009mA)
gume 0:163155b607df 314 *
gume 0:163155b607df 315 * @code
gume 0:163155b607df 316 * radio.powerDown();
gume 0:163155b607df 317 * avr_enter_sleep_mode(); // Custom function to sleep the device
gume 0:163155b607df 318 * radio.powerUp();
gume 0:163155b607df 319 * @endcode
gume 0:163155b607df 320 */
gume 0:163155b607df 321 void powerDown(void);
gume 0:163155b607df 322
gume 0:163155b607df 323 /**
gume 0:163155b607df 324 * Leave low-power mode - required for normal radio operation after calling powerDown()
gume 0:163155b607df 325 *
gume 0:163155b607df 326 * To return to low power mode, call powerDown().
gume 0:163155b607df 327 * @note This will take up to 5ms for maximum compatibility
gume 0:163155b607df 328 */
gume 0:163155b607df 329 void powerUp(void) ;
gume 0:163155b607df 330
gume 0:163155b607df 331 /**
gume 0:163155b607df 332 * Write for single NOACK writes. Optionally disables acknowledgements/autoretries for a single write.
gume 0:163155b607df 333 *
gume 0:163155b607df 334 * @note enableDynamicAck() must be called to enable this feature
gume 0:163155b607df 335 *
gume 0:163155b607df 336 * Can be used with enableAckPayload() to request a response
gume 0:163155b607df 337 * @see enableDynamicAck()
gume 0:163155b607df 338 * @see setAutoAck()
gume 0:163155b607df 339 * @see write()
gume 0:163155b607df 340 *
gume 0:163155b607df 341 * @param buf Pointer to the data to be sent
gume 0:163155b607df 342 * @param len Number of bytes to be sent
gume 0:163155b607df 343 * @param multicast Request ACK (0), NOACK (1)
gume 0:163155b607df 344 */
gume 0:163155b607df 345 bool write( const void* buf, uint8_t len, const bool multicast );
gume 0:163155b607df 346
gume 0:163155b607df 347 /**
gume 0:163155b607df 348 * This will not block until the 3 FIFO buffers are filled with data.
gume 0:163155b607df 349 * Once the FIFOs are full, writeFast will simply wait for success or
gume 0:163155b607df 350 * timeout, and return 1 or 0 respectively. From a user perspective, just
gume 0:163155b607df 351 * keep trying to send the same data. The library will keep auto retrying
gume 0:163155b607df 352 * the current payload using the built in functionality.
gume 0:163155b607df 353 * @warning It is important to never keep the nRF24L01 in TX mode and FIFO full for more than 4ms at a time. If the auto
gume 0:163155b607df 354 * retransmit is enabled, the nRF24L01 is never in TX mode long enough to disobey this rule. Allow the FIFO
gume 0:163155b607df 355 * to clear by issuing txStandBy() or ensure appropriate time between transmissions.
gume 0:163155b607df 356 *
gume 0:163155b607df 357 * @code
gume 0:163155b607df 358 * Example (Partial blocking):
gume 0:163155b607df 359 *
gume 0:163155b607df 360 * radio.writeFast(&buf,32); // Writes 1 payload to the buffers
gume 0:163155b607df 361 * txStandBy(); // Returns 0 if failed. 1 if success. Blocks only until MAX_RT timeout or success. Data flushed on fail.
gume 0:163155b607df 362 *
gume 0:163155b607df 363 * radio.writeFast(&buf,32); // Writes 1 payload to the buffers
gume 0:163155b607df 364 * txStandBy(1000); // Using extended timeouts, returns 1 if success. Retries failed payloads for 1 seconds before returning 0.
gume 0:163155b607df 365 * @endcode
gume 0:163155b607df 366 *
gume 0:163155b607df 367 * @see txStandBy()
gume 0:163155b607df 368 * @see write()
gume 0:163155b607df 369 * @see writeBlocking()
gume 0:163155b607df 370 *
gume 0:163155b607df 371 * @param buf Pointer to the data to be sent
gume 0:163155b607df 372 * @param len Number of bytes to be sent
gume 0:163155b607df 373 * @return True if the payload was delivered successfully false if not
gume 0:163155b607df 374 */
gume 0:163155b607df 375 bool writeFast( const void* buf, uint8_t len );
gume 0:163155b607df 376
gume 0:163155b607df 377 /**
gume 0:163155b607df 378 * WriteFast for single NOACK writes. Disables acknowledgements/autoretries for a single write.
gume 0:163155b607df 379 *
gume 0:163155b607df 380 * @note enableDynamicAck() must be called to enable this feature
gume 0:163155b607df 381 * @see enableDynamicAck()
gume 0:163155b607df 382 * @see setAutoAck()
gume 0:163155b607df 383 *
gume 0:163155b607df 384 * @param buf Pointer to the data to be sent
gume 0:163155b607df 385 * @param len Number of bytes to be sent
gume 0:163155b607df 386 * @param multicast Request ACK (0) or NOACK (1)
gume 0:163155b607df 387 */
gume 0:163155b607df 388 bool writeFast( const void* buf, uint8_t len, const bool multicast );
gume 0:163155b607df 389
gume 0:163155b607df 390 /**
gume 0:163155b607df 391 * This function extends the auto-retry mechanism to any specified duration.
gume 0:163155b607df 392 * It will not block until the 3 FIFO buffers are filled with data.
gume 0:163155b607df 393 * If so the library will auto retry until a new payload is written
gume 0:163155b607df 394 * or the user specified timeout period is reached.
gume 0:163155b607df 395 * @warning It is important to never keep the nRF24L01 in TX mode and FIFO full for more than 4ms at a time. If the auto
gume 0:163155b607df 396 * retransmit is enabled, the nRF24L01 is never in TX mode long enough to disobey this rule. Allow the FIFO
gume 0:163155b607df 397 * to clear by issuing txStandBy() or ensure appropriate time between transmissions.
gume 0:163155b607df 398 *
gume 0:163155b607df 399 * @code
gume 0:163155b607df 400 * Example (Full blocking):
gume 0:163155b607df 401 *
gume 0:163155b607df 402 * radio.writeBlocking(&buf,32,1000); //Wait up to 1 second to write 1 payload to the buffers
gume 0:163155b607df 403 * txStandBy(1000); //Wait up to 1 second for the payload to send. Return 1 if ok, 0 if failed.
gume 0:163155b607df 404 * //Blocks only until user timeout or success. Data flushed on fail.
gume 0:163155b607df 405 * @endcode
gume 0:163155b607df 406 * @note If used from within an interrupt, the interrupt should be disabled until completion, and sei(); called to enable millis().
gume 0:163155b607df 407 * @see txStandBy()
gume 0:163155b607df 408 * @see write()
gume 0:163155b607df 409 * @see writeFast()
gume 0:163155b607df 410 *
gume 0:163155b607df 411 * @param buf Pointer to the data to be sent
gume 0:163155b607df 412 * @param len Number of bytes to be sent
gume 0:163155b607df 413 * @param timeout User defined timeout in milliseconds.
gume 0:163155b607df 414 * @return True if the payload was loaded into the buffer successfully false if not
gume 0:163155b607df 415 */
gume 0:163155b607df 416 bool writeBlocking( const void* buf, uint8_t len, uint32_t timeout );
gume 0:163155b607df 417
gume 0:163155b607df 418 /**
gume 0:163155b607df 419 * This function should be called as soon as transmission is finished to
gume 0:163155b607df 420 * drop the radio back to STANDBY-I mode. If not issued, the radio will
gume 0:163155b607df 421 * remain in STANDBY-II mode which, per the data sheet, is not a recommended
gume 0:163155b607df 422 * operating mode.
gume 0:163155b607df 423 *
gume 0:163155b607df 424 * @note When transmitting data in rapid succession, it is still recommended by
gume 0:163155b607df 425 * the manufacturer to drop the radio out of TX or STANDBY-II mode if there is
gume 0:163155b607df 426 * time enough between sends for the FIFOs to empty. This is not required if auto-ack
gume 0:163155b607df 427 * is enabled.
gume 0:163155b607df 428 *
gume 0:163155b607df 429 * Relies on built-in auto retry functionality.
gume 0:163155b607df 430 *
gume 0:163155b607df 431 * @code
gume 0:163155b607df 432 * Example (Partial blocking):
gume 0:163155b607df 433 *
gume 0:163155b607df 434 * radio.writeFast(&buf,32);
gume 0:163155b607df 435 * radio.writeFast(&buf,32);
gume 0:163155b607df 436 * radio.writeFast(&buf,32); //Fills the FIFO buffers up
gume 0:163155b607df 437 * bool ok = txStandBy(); //Returns 0 if failed. 1 if success.
gume 0:163155b607df 438 * //Blocks only until MAX_RT timeout or success. Data flushed on fail.
gume 0:163155b607df 439 * @endcode
gume 0:163155b607df 440 * @see txStandBy(unsigned long timeout)
gume 0:163155b607df 441 * @return True if transmission is successful
gume 0:163155b607df 442 *
gume 0:163155b607df 443 */
gume 0:163155b607df 444 bool txStandBy();
gume 0:163155b607df 445
gume 0:163155b607df 446 /**
gume 0:163155b607df 447 * This function allows extended blocking and auto-retries per a user defined timeout
gume 0:163155b607df 448 * @code
gume 0:163155b607df 449 * Fully Blocking Example:
gume 0:163155b607df 450 *
gume 0:163155b607df 451 * radio.writeFast(&buf,32);
gume 0:163155b607df 452 * radio.writeFast(&buf,32);
gume 0:163155b607df 453 * radio.writeFast(&buf,32); //Fills the FIFO buffers up
gume 0:163155b607df 454 * bool ok = txStandBy(1000); //Returns 0 if failed after 1 second of retries. 1 if success.
gume 0:163155b607df 455 * //Blocks only until user defined timeout or success. Data flushed on fail.
gume 0:163155b607df 456 * @endcode
gume 0:163155b607df 457 * @note If used from within an interrupt, the interrupt should be disabled until completion, and sei(); called to enable millis().
gume 0:163155b607df 458 * @param timeout Number of milliseconds to retry failed payloads
gume 0:163155b607df 459 * @return True if transmission is successful
gume 0:163155b607df 460 *
gume 0:163155b607df 461 */
gume 0:163155b607df 462 bool txStandBy(uint32_t timeout, bool startTx = 0);
gume 0:163155b607df 463
gume 0:163155b607df 464 /**
gume 0:163155b607df 465 * Write an ack payload for the specified pipe
gume 0:163155b607df 466 *
gume 0:163155b607df 467 * The next time a message is received on @p pipe, the data in @p buf will
gume 0:163155b607df 468 * be sent back in the acknowledgement.
gume 0:163155b607df 469 * @see enableAckPayload()
gume 0:163155b607df 470 * @see enableDynamicPayloads()
gume 0:163155b607df 471 * @warning Only three of these can be pending at any time as there are only 3 FIFO buffers.<br> Dynamic payloads must be enabled.
gume 0:163155b607df 472 * @note Ack payloads are handled automatically by the radio chip when a payload is received. Users should generally
gume 0:163155b607df 473 * write an ack payload as soon as startListening() is called, so one is available when a regular payload is received.
gume 0:163155b607df 474 * @note Ack payloads are dynamic payloads. This only works on pipes 0&1 by default. Call
gume 0:163155b607df 475 * enableDynamicPayloads() to enable on all pipes.
gume 0:163155b607df 476 *
gume 0:163155b607df 477 * @param pipe Which pipe# (typically 1-5) will get this response.
gume 0:163155b607df 478 * @param buf Pointer to data that is sent
gume 0:163155b607df 479 * @param len Length of the data to send, up to 32 bytes max. Not affected
gume 0:163155b607df 480 * by the static payload set by setPayloadSize().
gume 0:163155b607df 481 */
gume 0:163155b607df 482 void writeAckPayload(uint8_t pipe, const void* buf, uint8_t len);
gume 0:163155b607df 483
gume 0:163155b607df 484 /**
gume 0:163155b607df 485 * Determine if an ack payload was received in the most recent call to
gume 0:163155b607df 486 * write(). The regular available() can also be used.
gume 0:163155b607df 487 *
gume 0:163155b607df 488 * Call read() to retrieve the ack payload.
gume 0:163155b607df 489 *
gume 0:163155b607df 490 * @return True if an ack payload is available.
gume 0:163155b607df 491 */
gume 0:163155b607df 492 bool isAckPayloadAvailable(void);
gume 0:163155b607df 493
gume 0:163155b607df 494 /**
gume 0:163155b607df 495 * Call this when you get an interrupt to find out why
gume 0:163155b607df 496 *
gume 0:163155b607df 497 * Tells you what caused the interrupt, and clears the state of
gume 0:163155b607df 498 * interrupts.
gume 0:163155b607df 499 *
gume 0:163155b607df 500 * @param[out] tx_ok The send was successful (TX_DS)
gume 0:163155b607df 501 * @param[out] tx_fail The send failed, too many retries (MAX_RT)
gume 0:163155b607df 502 * @param[out] rx_ready There is a message waiting to be read (RX_DS)
gume 0:163155b607df 503 */
gume 0:163155b607df 504 void whatHappened(bool& tx_ok,bool& tx_fail,bool& rx_ready);
gume 0:163155b607df 505
gume 0:163155b607df 506 /**
gume 0:163155b607df 507 * Non-blocking write to the open writing pipe used for buffered writes
gume 0:163155b607df 508 *
gume 0:163155b607df 509 * @note Optimization: This function now leaves the CE pin high, so the radio
gume 0:163155b607df 510 * will remain in TX or STANDBY-II Mode until a txStandBy() command is issued. Can be used as an alternative to startWrite()
gume 0:163155b607df 511 * if writing multiple payloads at once.
gume 0:163155b607df 512 * @warning It is important to never keep the nRF24L01 in TX mode with FIFO full for more than 4ms at a time. If the auto
gume 0:163155b607df 513 * retransmit/autoAck is enabled, the nRF24L01 is never in TX mode long enough to disobey this rule. Allow the FIFO
gume 0:163155b607df 514 * to clear by issuing txStandBy() or ensure appropriate time between transmissions.
gume 0:163155b607df 515 *
gume 0:163155b607df 516 * @see write()
gume 0:163155b607df 517 * @see writeFast()
gume 0:163155b607df 518 * @see startWrite()
gume 0:163155b607df 519 * @see writeBlocking()
gume 0:163155b607df 520 *
gume 0:163155b607df 521 * For single noAck writes see:
gume 0:163155b607df 522 * @see enableDynamicAck()
gume 0:163155b607df 523 * @see setAutoAck()
gume 0:163155b607df 524 *
gume 0:163155b607df 525 * @param buf Pointer to the data to be sent
gume 0:163155b607df 526 * @param len Number of bytes to be sent
gume 0:163155b607df 527 * @param multicast Request ACK (0) or NOACK (1)
gume 0:163155b607df 528 * @return True if the payload was delivered successfully false if not
gume 0:163155b607df 529 */
gume 0:163155b607df 530 void startFastWrite( const void* buf, uint8_t len, const bool multicast, bool startTx = 1 );
gume 0:163155b607df 531
gume 0:163155b607df 532 /**
gume 0:163155b607df 533 * Non-blocking write to the open writing pipe
gume 0:163155b607df 534 *
gume 0:163155b607df 535 * Just like write(), but it returns immediately. To find out what happened
gume 0:163155b607df 536 * to the send, catch the IRQ and then call whatHappened().
gume 0:163155b607df 537 *
gume 0:163155b607df 538 * @see write()
gume 0:163155b607df 539 * @see writeFast()
gume 0:163155b607df 540 * @see startFastWrite()
gume 0:163155b607df 541 * @see whatHappened()
gume 0:163155b607df 542 *
gume 0:163155b607df 543 * For single noAck writes see:
gume 0:163155b607df 544 * @see enableDynamicAck()
gume 0:163155b607df 545 * @see setAutoAck()
gume 0:163155b607df 546 *
gume 0:163155b607df 547 * @param buf Pointer to the data to be sent
gume 0:163155b607df 548 * @param len Number of bytes to be sent
gume 0:163155b607df 549 * @param multicast Request ACK (0) or NOACK (1)
gume 0:163155b607df 550 *
gume 0:163155b607df 551 */
gume 0:163155b607df 552 void startWrite( const void* buf, uint8_t len, const bool multicast );
gume 0:163155b607df 553
gume 0:163155b607df 554 /**
gume 0:163155b607df 555 * This function is mainly used internally to take advantage of the auto payload
gume 0:163155b607df 556 * re-use functionality of the chip, but can be beneficial to users as well.
gume 0:163155b607df 557 *
gume 0:163155b607df 558 * The function will instruct the radio to re-use the data in the FIFO buffers,
gume 0:163155b607df 559 * and instructs the radio to re-send once the timeout limit has been reached.
gume 0:163155b607df 560 * Used by writeFast and writeBlocking to initiate retries when a TX failure
gume 0:163155b607df 561 * occurs. Retries are automatically initiated except with the standard write().
gume 0:163155b607df 562 * This way, data is not flushed from the buffer until switching between modes.
gume 0:163155b607df 563 *
gume 0:163155b607df 564 * @note This is to be used AFTER auto-retry fails if wanting to resend
gume 0:163155b607df 565 * using the built-in payload reuse features.
gume 0:163155b607df 566 * After issuing reUseTX(), it will keep reending the same payload forever or until
gume 0:163155b607df 567 * a payload is written to the FIFO, or a flush_tx command is given.
gume 0:163155b607df 568 */
gume 0:163155b607df 569 void reUseTX();
gume 0:163155b607df 570
gume 0:163155b607df 571 /**
gume 0:163155b607df 572 * Empty the transmit buffer. This is generally not required in standard operation.
gume 0:163155b607df 573 * May be required in specific cases after stopListening() , if operating at 250KBPS data rate.
gume 0:163155b607df 574 *
gume 0:163155b607df 575 * @return Current value of status register
gume 0:163155b607df 576 */
gume 0:163155b607df 577 uint8_t flush_tx(void);
gume 0:163155b607df 578
gume 0:163155b607df 579 /**
gume 0:163155b607df 580 * Test whether there was a carrier on the line for the
gume 0:163155b607df 581 * previous listening period.
gume 0:163155b607df 582 *
gume 0:163155b607df 583 * Useful to check for interference on the current channel.
gume 0:163155b607df 584 *
gume 0:163155b607df 585 * @return true if was carrier, false if not
gume 0:163155b607df 586 */
gume 0:163155b607df 587 bool testCarrier(void);
gume 0:163155b607df 588
gume 0:163155b607df 589 /**
gume 0:163155b607df 590 * Test whether a signal (carrier or otherwise) greater than
gume 0:163155b607df 591 * or equal to -64dBm is present on the channel. Valid only
gume 0:163155b607df 592 * on nRF24L01P (+) hardware. On nRF24L01, use testCarrier().
gume 0:163155b607df 593 *
gume 0:163155b607df 594 * Useful to check for interference on the current channel and
gume 0:163155b607df 595 * channel hopping strategies.
gume 0:163155b607df 596 *
gume 0:163155b607df 597 * @code
gume 0:163155b607df 598 * bool goodSignal = radio.testRPD();
gume 0:163155b607df 599 * if(radio.available()){
gume 0:163155b607df 600 * Serial.println(goodSignal ? "Strong signal > 64dBm" : "Weak signal < 64dBm" );
gume 0:163155b607df 601 * radio.read(0,0);
gume 0:163155b607df 602 * }
gume 0:163155b607df 603 * @endcode
gume 0:163155b607df 604 * @return true if signal => -64dBm, false if not
gume 0:163155b607df 605 */
gume 0:163155b607df 606 bool testRPD(void) ;
gume 0:163155b607df 607
gume 0:163155b607df 608 /**
gume 0:163155b607df 609 * Test whether this is a real radio, or a mock shim for
gume 0:163155b607df 610 * debugging. Setting either pin to 0xff is the way to
gume 0:163155b607df 611 * indicate that this is not a real radio.
gume 0:163155b607df 612 *
gume 0:163155b607df 613 * @return true if this is a legitimate radio
gume 0:163155b607df 614 */
gume 0:163155b607df 615 bool isValid() {
gume 0:163155b607df 616 return ce_pin != 0xff && csn_pin != 0xff;
gume 0:163155b607df 617 }
gume 0:163155b607df 618
gume 0:163155b607df 619 /**
gume 0:163155b607df 620 * Close a pipe after it has been previously opened.
gume 0:163155b607df 621 * Can be safely called without having previously opened a pipe.
gume 0:163155b607df 622 * @param pipe Which pipe # to close, 0-5.
gume 0:163155b607df 623 */
gume 0:163155b607df 624 void closeReadingPipe( uint8_t pipe ) ;
gume 0:163155b607df 625
gume 0:163155b607df 626 /**
gume 0:163155b607df 627 * Enable error detection by un-commenting #define FAILURE_HANDLING in RF24_config.h
gume 0:163155b607df 628 * If a failure has been detected, it usually indicates a hardware issue. By default the library
gume 0:163155b607df 629 * will cease operation when a failure is detected.
gume 0:163155b607df 630 * This should allow advanced users to detect and resolve intermittent hardware issues.
gume 0:163155b607df 631 *
gume 0:163155b607df 632 * In most cases, the radio must be re-enabled via radio.begin(); and the appropriate settings
gume 0:163155b607df 633 * applied after a failure occurs, if wanting to re-enable the device immediately.
gume 0:163155b607df 634 *
gume 0:163155b607df 635 * Usage: (Failure handling must be enabled per above)
gume 0:163155b607df 636 * @code
gume 0:163155b607df 637 * if(radio.failureDetected){
gume 0:163155b607df 638 * radio.begin(); // Attempt to re-configure the radio with defaults
gume 0:163155b607df 639 * radio.failureDetected = 0; // Reset the detection value
gume 0:163155b607df 640 * radio.openWritingPipe(addresses[1]); // Re-configure pipe addresses
gume 0:163155b607df 641 * radio.openReadingPipe(1,addresses[0]);
gume 0:163155b607df 642 * report_failure(); // Blink leds, send a message, etc. to indicate failure
gume 0:163155b607df 643 * }
gume 0:163155b607df 644 * @endcode
gume 0:163155b607df 645 */
gume 0:163155b607df 646 //#if defined (FAILURE_HANDLING)
gume 0:163155b607df 647 bool failureDetected;
gume 0:163155b607df 648 //#endif
gume 0:163155b607df 649
gume 0:163155b607df 650 /**@}*/
gume 0:163155b607df 651
gume 0:163155b607df 652 /**@}*/
gume 0:163155b607df 653 /**
gume 0:163155b607df 654 * @name Optional Configurators
gume 0:163155b607df 655 *
gume 0:163155b607df 656 * Methods you can use to get or set the configuration of the chip.
gume 0:163155b607df 657 * None are required. Calling begin() sets up a reasonable set of
gume 0:163155b607df 658 * defaults.
gume 0:163155b607df 659 */
gume 0:163155b607df 660 /**@{*/
gume 0:163155b607df 661
gume 0:163155b607df 662 /**
gume 0:163155b607df 663 * Set the address width from 3 to 5 bytes (24, 32 or 40 bit)
gume 0:163155b607df 664 *
gume 0:163155b607df 665 * @param a_width The address width to use: 3,4 or 5
gume 0:163155b607df 666 */
gume 0:163155b607df 667
gume 0:163155b607df 668 void setAddressWidth(uint8_t a_width);
gume 0:163155b607df 669
gume 0:163155b607df 670 /**
gume 0:163155b607df 671 * Set the number and delay of retries upon failed submit
gume 0:163155b607df 672 *
gume 0:163155b607df 673 * @param delay How long to wait between each retry, in multiples of 250us,
gume 0:163155b607df 674 * max is 15. 0 means 250us, 15 means 4000us.
gume 0:163155b607df 675 * @param count How many retries before giving up, max 15
gume 0:163155b607df 676 */
gume 0:163155b607df 677 void setRetries(uint8_t delay, uint8_t count);
gume 0:163155b607df 678
gume 0:163155b607df 679 /**
gume 0:163155b607df 680 * Set RF communication channel
gume 0:163155b607df 681 *
gume 0:163155b607df 682 * @param channel Which RF channel to communicate on, 0-125
gume 0:163155b607df 683 */
gume 0:163155b607df 684 void setChannel(uint8_t channel);
gume 0:163155b607df 685
gume 0:163155b607df 686 /**
gume 0:163155b607df 687 * Get RF communication channel
gume 0:163155b607df 688 *
gume 0:163155b607df 689 * @return The currently configured RF Channel
gume 0:163155b607df 690 */
gume 0:163155b607df 691 uint8_t getChannel(void);
gume 0:163155b607df 692
gume 0:163155b607df 693 /**
gume 0:163155b607df 694 * Set Static Payload Size
gume 0:163155b607df 695 *
gume 0:163155b607df 696 * This implementation uses a pre-stablished fixed payload size for all
gume 0:163155b607df 697 * transmissions. If this method is never called, the driver will always
gume 0:163155b607df 698 * transmit the maximum payload size (32 bytes), no matter how much
gume 0:163155b607df 699 * was sent to write().
gume 0:163155b607df 700 *
gume 0:163155b607df 701 * @todo Implement variable-sized payloads feature
gume 0:163155b607df 702 *
gume 0:163155b607df 703 * @param size The number of bytes in the payload
gume 0:163155b607df 704 */
gume 0:163155b607df 705 void setPayloadSize(uint8_t size);
gume 0:163155b607df 706
gume 0:163155b607df 707 /**
gume 0:163155b607df 708 * Get Static Payload Size
gume 0:163155b607df 709 *
gume 0:163155b607df 710 * @see setPayloadSize()
gume 0:163155b607df 711 *
gume 0:163155b607df 712 * @return The number of bytes in the payload
gume 0:163155b607df 713 */
gume 0:163155b607df 714 uint8_t getPayloadSize(void);
gume 0:163155b607df 715
gume 0:163155b607df 716 /**
gume 0:163155b607df 717 * Get Dynamic Payload Size
gume 0:163155b607df 718 *
gume 0:163155b607df 719 * For dynamic payloads, this pulls the size of the payload off
gume 0:163155b607df 720 * the chip
gume 0:163155b607df 721 *
gume 0:163155b607df 722 * @note Corrupt packets are now detected and flushed per the
gume 0:163155b607df 723 * manufacturer.
gume 0:163155b607df 724 * @code
gume 0:163155b607df 725 * if(radio.available()){
gume 0:163155b607df 726 * if(radio.getDynamicPayloadSize() < 1){
gume 0:163155b607df 727 * // Corrupt payload has been flushed
gume 0:163155b607df 728 * return;
gume 0:163155b607df 729 * }
gume 0:163155b607df 730 * radio.read(&data,sizeof(data));
gume 0:163155b607df 731 * }
gume 0:163155b607df 732 * @endcode
gume 0:163155b607df 733 *
gume 0:163155b607df 734 * @return Payload length of last-received dynamic payload
gume 0:163155b607df 735 */
gume 0:163155b607df 736 uint8_t getDynamicPayloadSize(void);
gume 0:163155b607df 737
gume 0:163155b607df 738 /**
gume 0:163155b607df 739 * Enable custom payloads on the acknowledge packets
gume 0:163155b607df 740 *
gume 0:163155b607df 741 * Ack payloads are a handy way to return data back to senders without
gume 0:163155b607df 742 * manually changing the radio modes on both units.
gume 0:163155b607df 743 *
gume 0:163155b607df 744 * @note Ack payloads are dynamic payloads. This only works on pipes 0&1 by default. Call
gume 0:163155b607df 745 * enableDynamicPayloads() to enable on all pipes.
gume 0:163155b607df 746 */
gume 0:163155b607df 747 void enableAckPayload(void);
gume 0:163155b607df 748
gume 0:163155b607df 749 /**
gume 0:163155b607df 750 * Enable dynamically-sized payloads
gume 0:163155b607df 751 *
gume 0:163155b607df 752 * This way you don't always have to send large packets just to send them
gume 0:163155b607df 753 * once in a while. This enables dynamic payloads on ALL pipes.
gume 0:163155b607df 754 *
gume 0:163155b607df 755 */
gume 0:163155b607df 756 void enableDynamicPayloads(void);
gume 0:163155b607df 757
gume 0:163155b607df 758 /**
gume 0:163155b607df 759 * Enable dynamic ACKs (single write multicast or unicast) for chosen messages
gume 0:163155b607df 760 *
gume 0:163155b607df 761 * @note To enable full multicast or per-pipe multicast, use setAutoAck()
gume 0:163155b607df 762 *
gume 0:163155b607df 763 * @warning This MUST be called prior to attempting single write NOACK calls
gume 0:163155b607df 764 * @code
gume 0:163155b607df 765 * radio.enableDynamicAck();
gume 0:163155b607df 766 * radio.write(&data,32,1); // Sends a payload with no acknowledgement requested
gume 0:163155b607df 767 * radio.write(&data,32,0); // Sends a payload using auto-retry/autoACK
gume 0:163155b607df 768 * @endcode
gume 0:163155b607df 769 */
gume 0:163155b607df 770 void enableDynamicAck();
gume 0:163155b607df 771
gume 0:163155b607df 772 /**
gume 0:163155b607df 773 * Determine whether the hardware is an nRF24L01+ or not.
gume 0:163155b607df 774 *
gume 0:163155b607df 775 * @return true if the hardware is nRF24L01+ (or compatible) and false
gume 0:163155b607df 776 * if its not.
gume 0:163155b607df 777 */
gume 0:163155b607df 778 bool isPVariant(void) ;
gume 0:163155b607df 779
gume 0:163155b607df 780 /**
gume 0:163155b607df 781 * Enable or disable auto-acknowlede packets
gume 0:163155b607df 782 *
gume 0:163155b607df 783 * This is enabled by default, so it's only needed if you want to turn
gume 0:163155b607df 784 * it off for some reason.
gume 0:163155b607df 785 *
gume 0:163155b607df 786 * @param enable Whether to enable (true) or disable (false) auto-acks
gume 0:163155b607df 787 */
gume 0:163155b607df 788 void setAutoAck(bool enable);
gume 0:163155b607df 789
gume 0:163155b607df 790 /**
gume 0:163155b607df 791 * Enable or disable auto-acknowlede packets on a per pipeline basis.
gume 0:163155b607df 792 *
gume 0:163155b607df 793 * AA is enabled by default, so it's only needed if you want to turn
gume 0:163155b607df 794 * it off/on for some reason on a per pipeline basis.
gume 0:163155b607df 795 *
gume 0:163155b607df 796 * @param pipe Which pipeline to modify
gume 0:163155b607df 797 * @param enable Whether to enable (true) or disable (false) auto-acks
gume 0:163155b607df 798 */
gume 0:163155b607df 799 void setAutoAck( uint8_t pipe, bool enable ) ;
gume 0:163155b607df 800
gume 0:163155b607df 801 /**
gume 0:163155b607df 802 * Set Power Amplifier (PA) level to one of four levels:
gume 0:163155b607df 803 * RF24_PA_MIN, RF24_PA_LOW, RF24_PA_HIGH and RF24_PA_MAX
gume 0:163155b607df 804 *
gume 0:163155b607df 805 * The power levels correspond to the following output levels respectively:
gume 0:163155b607df 806 * NRF24L01: -18dBm, -12dBm,-6dBM, and 0dBm
gume 0:163155b607df 807 *
gume 0:163155b607df 808 * SI24R1: -6dBm, 0dBm, 3dBM, and 7dBm.
gume 0:163155b607df 809 *
gume 0:163155b607df 810 * @param level Desired PA level.
gume 0:163155b607df 811 */
gume 0:163155b607df 812 void setPALevel ( uint8_t level );
gume 0:163155b607df 813
gume 0:163155b607df 814 /**
gume 0:163155b607df 815 * Fetches the current PA level.
gume 0:163155b607df 816 *
gume 0:163155b607df 817 * NRF24L01: -18dBm, -12dBm, -6dBm and 0dBm
gume 0:163155b607df 818 * SI24R1: -6dBm, 0dBm, 3dBm, 7dBm
gume 0:163155b607df 819 *
gume 0:163155b607df 820 * @return Returns values 0 to 3 representing the PA Level.
gume 0:163155b607df 821 */
gume 0:163155b607df 822 uint8_t getPALevel( void );
gume 0:163155b607df 823
gume 0:163155b607df 824 /**
gume 0:163155b607df 825 * Set the transmission data rate
gume 0:163155b607df 826 *
gume 0:163155b607df 827 * @warning setting RF24_250KBPS will fail for non-plus units
gume 0:163155b607df 828 *
gume 0:163155b607df 829 * @param speed RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
gume 0:163155b607df 830 * @return true if the change was successful
gume 0:163155b607df 831 */
gume 0:163155b607df 832 bool setDataRate(rf24_datarate_e speed);
gume 0:163155b607df 833
gume 0:163155b607df 834 /**
gume 0:163155b607df 835 * Fetches the transmission data rate
gume 0:163155b607df 836 *
gume 0:163155b607df 837 * @return Returns the hardware's currently configured datarate. The value
gume 0:163155b607df 838 * is one of 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS, as defined in the
gume 0:163155b607df 839 * rf24_datarate_e enum.
gume 0:163155b607df 840 */
gume 0:163155b607df 841 rf24_datarate_e getDataRate( void ) ;
gume 0:163155b607df 842
gume 0:163155b607df 843 /**
gume 0:163155b607df 844 * Set the CRC length
gume 0:163155b607df 845 * <br>CRC checking cannot be disabled if auto-ack is enabled
gume 0:163155b607df 846 * @param length RF24_CRC_8 for 8-bit or RF24_CRC_16 for 16-bit
gume 0:163155b607df 847 */
gume 0:163155b607df 848 void setCRCLength(rf24_crclength_e length);
gume 0:163155b607df 849
gume 0:163155b607df 850 /**
gume 0:163155b607df 851 * Get the CRC length
gume 0:163155b607df 852 * <br>CRC checking cannot be disabled if auto-ack is enabled
gume 0:163155b607df 853 * @return RF24_DISABLED if disabled or RF24_CRC_8 for 8-bit or RF24_CRC_16 for 16-bit
gume 0:163155b607df 854 */
gume 0:163155b607df 855 rf24_crclength_e getCRCLength(void);
gume 0:163155b607df 856
gume 0:163155b607df 857 /**
gume 0:163155b607df 858 * Disable CRC validation
gume 0:163155b607df 859 *
gume 0:163155b607df 860 * @warning CRC cannot be disabled if auto-ack/ESB is enabled.
gume 0:163155b607df 861 */
gume 0:163155b607df 862 void disableCRC( void ) ;
gume 0:163155b607df 863
gume 0:163155b607df 864 /**
gume 0:163155b607df 865 * The radio will generate interrupt signals when a transmission is complete,
gume 0:163155b607df 866 * a transmission fails, or a payload is received. This allows users to mask
gume 0:163155b607df 867 * those interrupts to prevent them from generating a signal on the interrupt
gume 0:163155b607df 868 * pin. Interrupts are enabled on the radio chip by default.
gume 0:163155b607df 869 *
gume 0:163155b607df 870 * @code
gume 0:163155b607df 871 * Mask all interrupts except the receive interrupt:
gume 0:163155b607df 872 *
gume 0:163155b607df 873 * radio.maskIRQ(1,1,0);
gume 0:163155b607df 874 * @endcode
gume 0:163155b607df 875 *
gume 0:163155b607df 876 * @param tx_ok Mask transmission complete interrupts
gume 0:163155b607df 877 * @param tx_fail Mask transmit failure interrupts
gume 0:163155b607df 878 * @param rx_ready Mask payload received interrupts
gume 0:163155b607df 879 */
gume 0:163155b607df 880 void maskIRQ(bool tx_ok,bool tx_fail,bool rx_ready);
gume 0:163155b607df 881
gume 0:163155b607df 882 /**@}*/
gume 0:163155b607df 883 /**
gume 0:163155b607df 884 * @name Deprecated
gume 0:163155b607df 885 *
gume 0:163155b607df 886 * Methods provided for backwards compabibility.
gume 0:163155b607df 887 */
gume 0:163155b607df 888 /**@{*/
gume 0:163155b607df 889
gume 0:163155b607df 890
gume 0:163155b607df 891 /**
gume 0:163155b607df 892 * Open a pipe for reading
gume 0:163155b607df 893 * @note For compatibility with old code only, see new function
gume 0:163155b607df 894 *
gume 0:163155b607df 895 * @warning Pipes 1-5 should share the first 32 bits.
gume 0:163155b607df 896 * Only the least significant byte should be unique, e.g.
gume 0:163155b607df 897 * @code
gume 0:163155b607df 898 * openReadingPipe(1,0xF0F0F0F0AA);
gume 0:163155b607df 899 * openReadingPipe(2,0xF0F0F0F066);
gume 0:163155b607df 900 * @endcode
gume 0:163155b607df 901 *
gume 0:163155b607df 902 * @warning Pipe 0 is also used by the writing pipe. So if you open
gume 0:163155b607df 903 * pipe 0 for reading, and then startListening(), it will overwrite the
gume 0:163155b607df 904 * writing pipe. Ergo, do an openWritingPipe() again before write().
gume 0:163155b607df 905 *
gume 0:163155b607df 906 * @param number Which pipe# to open, 0-5.
gume 0:163155b607df 907 * @param address The 40-bit address of the pipe to open.
gume 0:163155b607df 908 */
gume 0:163155b607df 909 void openReadingPipe(uint8_t number, uint64_t address);
gume 0:163155b607df 910
gume 0:163155b607df 911 /**
gume 0:163155b607df 912 * Open a pipe for writing
gume 0:163155b607df 913 * @note For compatibility with old code only, see new function
gume 0:163155b607df 914 *
gume 0:163155b607df 915 * Addresses are 40-bit hex values, e.g.:
gume 0:163155b607df 916 *
gume 0:163155b607df 917 * @code
gume 0:163155b607df 918 * openWritingPipe(0xF0F0F0F0F0);
gume 0:163155b607df 919 * @endcode
gume 0:163155b607df 920 *
gume 0:163155b607df 921 * @param address The 40-bit address of the pipe to open.
gume 0:163155b607df 922 */
gume 0:163155b607df 923 void openWritingPipe(uint64_t address);
gume 0:163155b607df 924
gume 0:163155b607df 925 private:
gume 0:163155b607df 926
gume 0:163155b607df 927 /**
gume 0:163155b607df 928 * @name Low-level internal interface.
gume 0:163155b607df 929 *
gume 0:163155b607df 930 * Protected methods that address the chip directly. Regular users cannot
gume 0:163155b607df 931 * ever call these. They are documented for completeness and for developers who
gume 0:163155b607df 932 * may want to extend this class.
gume 0:163155b607df 933 */
gume 0:163155b607df 934 /**@{*/
gume 0:163155b607df 935
gume 0:163155b607df 936 /**
gume 0:163155b607df 937 * Set chip select pin
gume 0:163155b607df 938 *
gume 0:163155b607df 939 * Running SPI bus at PI_CLOCK_DIV2 so we don't waste time transferring data
gume 0:163155b607df 940 * and best of all, we make use of the radio's FIFO buffers. A lower speed
gume 0:163155b607df 941 * means we're less likely to effectively leverage our FIFOs and pay a higher
gume 0:163155b607df 942 * AVR runtime cost as toll.
gume 0:163155b607df 943 *
gume 3:13e43d3101a5 944 * @param level HIGH to actively begin transmission or LOW to put in standby. Please see data sheet
gume 3:13e43d3101a5 945 * for a much more detailed description of this pin.
gume 0:163155b607df 946 */
gume 3:13e43d3101a5 947 void csn(int mode);
gume 0:163155b607df 948
gume 0:163155b607df 949 /**
gume 0:163155b607df 950 * Set chip enable
gume 0:163155b607df 951 *
gume 3:13e43d3101a5 952 * @param mode HIGH to take this unit off the SPI bus, LOW to put it on
gume 0:163155b607df 953 */
gume 3:13e43d3101a5 954 void ce(int level);
gume 0:163155b607df 955
gume 0:163155b607df 956 /**
gume 0:163155b607df 957 * Read a chunk of data in from a register
gume 0:163155b607df 958 *
gume 0:163155b607df 959 * @param reg Which register. Use constants from nRF24L01.h
gume 0:163155b607df 960 * @param buf Where to put the data
gume 0:163155b607df 961 * @param len How many bytes of data to transfer
gume 0:163155b607df 962 * @return Current value of status register
gume 0:163155b607df 963 */
gume 0:163155b607df 964 uint8_t read_register(uint8_t reg, uint8_t* buf, uint8_t len);
gume 0:163155b607df 965
gume 0:163155b607df 966 /**
gume 0:163155b607df 967 * Read single byte from a register
gume 0:163155b607df 968 *
gume 0:163155b607df 969 * @param reg Which register. Use constants from nRF24L01.h
gume 0:163155b607df 970 * @return Current value of register @p reg
gume 0:163155b607df 971 */
gume 0:163155b607df 972 uint8_t read_register(uint8_t reg);
gume 0:163155b607df 973
gume 0:163155b607df 974 /**
gume 0:163155b607df 975 * Write a chunk of data to a register
gume 0:163155b607df 976 *
gume 0:163155b607df 977 * @param reg Which register. Use constants from nRF24L01.h
gume 0:163155b607df 978 * @param buf Where to get the data
gume 0:163155b607df 979 * @param len How many bytes of data to transfer
gume 0:163155b607df 980 * @return Current value of status register
gume 0:163155b607df 981 */
gume 0:163155b607df 982 uint8_t write_register(uint8_t reg, const uint8_t* buf, uint8_t len);
gume 0:163155b607df 983
gume 0:163155b607df 984 /**
gume 0:163155b607df 985 * Write a single byte to a register
gume 0:163155b607df 986 *
gume 0:163155b607df 987 * @param reg Which register. Use constants from nRF24L01.h
gume 0:163155b607df 988 * @param value The new value to write
gume 0:163155b607df 989 * @return Current value of status register
gume 0:163155b607df 990 */
gume 0:163155b607df 991 uint8_t write_register(uint8_t reg, uint8_t value);
gume 0:163155b607df 992
gume 0:163155b607df 993 /**
gume 0:163155b607df 994 * Write the transmit payload
gume 0:163155b607df 995 *
gume 0:163155b607df 996 * The size of data written is the fixed payload size, see getPayloadSize()
gume 0:163155b607df 997 *
gume 0:163155b607df 998 * @param buf Where to get the data
gume 0:163155b607df 999 * @param len Number of bytes to be sent
gume 0:163155b607df 1000 * @return Current value of status register
gume 0:163155b607df 1001 */
gume 0:163155b607df 1002 uint8_t write_payload(const void* buf, uint8_t len, const uint8_t writeType);
gume 0:163155b607df 1003
gume 0:163155b607df 1004 /**
gume 0:163155b607df 1005 * Read the receive payload
gume 0:163155b607df 1006 *
gume 0:163155b607df 1007 * The size of data read is the fixed payload size, see getPayloadSize()
gume 0:163155b607df 1008 *
gume 0:163155b607df 1009 * @param buf Where to put the data
gume 0:163155b607df 1010 * @param len Maximum number of bytes to read
gume 0:163155b607df 1011 * @return Current value of status register
gume 0:163155b607df 1012 */
gume 0:163155b607df 1013 uint8_t read_payload(void* buf, uint8_t len);
gume 0:163155b607df 1014
gume 0:163155b607df 1015 /**
gume 0:163155b607df 1016 * Empty the receive buffer
gume 0:163155b607df 1017 *
gume 0:163155b607df 1018 * @return Current value of status register
gume 0:163155b607df 1019 */
gume 0:163155b607df 1020 uint8_t flush_rx(void);
gume 0:163155b607df 1021
gume 0:163155b607df 1022 /**
gume 0:163155b607df 1023 * Retrieve the current status of the chip
gume 0:163155b607df 1024 *
gume 0:163155b607df 1025 * @return Current value of status register
gume 0:163155b607df 1026 */
gume 0:163155b607df 1027 uint8_t get_status(void);
gume 0:163155b607df 1028
gume 0:163155b607df 1029 #if !defined (MINIMAL)
gume 0:163155b607df 1030 /**
gume 0:163155b607df 1031 * Decode and print the given status to stdout
gume 0:163155b607df 1032 *
gume 0:163155b607df 1033 * @param status Status value to print
gume 0:163155b607df 1034 *
gume 0:163155b607df 1035 * @warning Does nothing if stdout is not defined. See fdevopen in stdio.h
gume 0:163155b607df 1036 */
gume 0:163155b607df 1037 void print_status(uint8_t status);
gume 0:163155b607df 1038
gume 0:163155b607df 1039 /**
gume 0:163155b607df 1040 * Decode and print the given 'observe_tx' value to stdout
gume 0:163155b607df 1041 *
gume 0:163155b607df 1042 * @param value The observe_tx value to print
gume 0:163155b607df 1043 *
gume 0:163155b607df 1044 * @warning Does nothing if stdout is not defined. See fdevopen in stdio.h
gume 0:163155b607df 1045 */
gume 0:163155b607df 1046 void print_observe_tx(uint8_t value);
gume 0:163155b607df 1047
gume 0:163155b607df 1048 /**
gume 0:163155b607df 1049 * Print the name and value of an 8-bit register to stdout
gume 0:163155b607df 1050 *
gume 0:163155b607df 1051 * Optionally it can print some quantity of successive
gume 0:163155b607df 1052 * registers on the same line. This is useful for printing a group
gume 0:163155b607df 1053 * of related registers on one line.
gume 0:163155b607df 1054 *
gume 0:163155b607df 1055 * @param name Name of the register
gume 0:163155b607df 1056 * @param reg Which register. Use constants from nRF24L01.h
gume 0:163155b607df 1057 * @param qty How many successive registers to print
gume 0:163155b607df 1058 */
gume 0:163155b607df 1059 void print_byte_register(const char* name, uint8_t reg, uint8_t qty = 1);
gume 0:163155b607df 1060
gume 0:163155b607df 1061 /**
gume 0:163155b607df 1062 * Print the name and value of a 40-bit address register to stdout
gume 0:163155b607df 1063 *
gume 0:163155b607df 1064 * Optionally it can print some quantity of successive
gume 0:163155b607df 1065 * registers on the same line. This is useful for printing a group
gume 0:163155b607df 1066 * of related registers on one line.
gume 0:163155b607df 1067 *
gume 0:163155b607df 1068 * @param name Name of the register
gume 0:163155b607df 1069 * @param reg Which register. Use constants from nRF24L01.h
gume 0:163155b607df 1070 * @param qty How many successive registers to print
gume 0:163155b607df 1071 */
gume 0:163155b607df 1072 void print_address_register(const char* name, uint8_t reg, uint8_t qty = 1);
gume 0:163155b607df 1073 #endif
gume 0:163155b607df 1074 /**
gume 0:163155b607df 1075 * Turn on or off the special features of the chip
gume 0:163155b607df 1076 *
gume 0:163155b607df 1077 * The chip has certain 'features' which are only available when the 'features'
gume 0:163155b607df 1078 * are enabled. See the datasheet for details.
gume 0:163155b607df 1079 */
gume 0:163155b607df 1080 void toggle_features(void);
gume 0:163155b607df 1081
gume 0:163155b607df 1082 /**
gume 0:163155b607df 1083 * Built in spi transfer function to simplify repeating code repeating code
gume 0:163155b607df 1084 */
gume 0:163155b607df 1085
gume 0:163155b607df 1086 uint8_t spiTrans(uint8_t cmd);
gume 0:163155b607df 1087
gume 0:163155b607df 1088 #if defined (FAILURE_HANDLING)
gume 0:163155b607df 1089 void errNotify(void);
gume 0:163155b607df 1090 #endif
gume 0:163155b607df 1091
gume 0:163155b607df 1092 /**@}*/
gume 0:163155b607df 1093
gume 0:163155b607df 1094 };
gume 0:163155b607df 1095
gume 0:163155b607df 1096
gume 0:163155b607df 1097 #endif // __RF24_H__
gume 0:163155b607df 1098