Ported it to c without arduino dependencies. Switched from printf to a
[RF24-C.git] / RF24.h
blob3a9176365b2ecfb1f6b4f1c1a1597a4d5237b350
1 /*
2 Copyright (C) 2011,2012 J. Coliz <maniacbug@ymail.com>, jaseg <s@jaseg.de>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 version 2 as published by the Free Software Foundation.
7 */
9 /**
10 * @file RF24.h
12 * Class declaration for RF24 and helper enums
15 #ifndef __RF24_H__
16 #define __RF24_H__
18 #include <RF24_config.h>
19 #include "spi.h"
21 #define HIGH 1
22 #define LOW 0
24 #define TRUE 1
25 #define FALSE 0
27 /**
28 * Power Amplifier level.
30 * For use with setPALevel()
32 typedef enum { RF24_PA_MIN = 0,RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX, RF24_PA_ERROR } rf24_pa_dbm_e ;
34 /**
35 * Data rate. How fast data moves through the air.
37 * For use with setDataRate()
39 typedef enum { RF24_1MBPS = 0, RF24_2MBPS, RF24_250KBPS } rf24_datarate_e;
41 /**
42 * CRC Length. How big (if any) of a CRC is included.
44 * For use with setCRCLength()
46 typedef enum { RF24_CRC_DISABLED = 0, RF24_CRC_8, RF24_CRC_16 } rf24_crclength_e;
48 /**
49 * Driver for nRF24L01(+) 2.4GHz Wireless Transceiver
52 /**
53 * @name Low-level internal interface.
55 * Protected methods that address the chip directly. Regular users cannot
56 * ever call these. They are documented for completeness and for developers who
57 * may want to extend this class.
59 /**@{*/
61 /**
62 * Set chip select pin
64 * Running SPI bus at PI_CLOCK_DIV2 so we don't waste time transferring data
65 * and best of all, we make use of the radio's FIFO buffers. A lower speed
66 * means we're less likely to effectively leverage our FIFOs and pay a higher
67 * AVR runtime cost as toll.
69 * @param mode HIGH to take this unit off the SPI bus, LOW to put it on
71 void nrf24_csn(int mode);
73 /**
74 * Set chip enable
76 * @param level HIGH to actively begin transmission or LOW to put in standby. Please see data sheet
77 * for a much more detailed description of this pin.
79 void nrf24_ce(int level);
81 /**
82 * Read a chunk of data in from a register
84 * @param reg Which register. Use constants from nRF24L01.h
85 * @param buf Where to put the data
86 * @param len How many bytes of data to transfer
87 * @return Current value of status register
89 uint8_t nrf24_read_register_buf(uint8_t reg, uint8_t* buf, uint8_t len);
91 /**
92 * Read single byte from a register
94 * @param reg Which register. Use constants from nRF24L01.h
95 * @return Current value of register @p reg
97 uint8_t nrf24_read_register(uint8_t reg);
99 /**
100 * Write a chunk of data to a register
102 * @param reg Which register. Use constants from nRF24L01.h
103 * @param buf Where to get the data
104 * @param len How many bytes of data to transfer
105 * @return Current value of status register
107 uint8_t nrf24_write_register_buf(uint8_t reg, const uint8_t* buf, uint8_t len);
110 * Write a single byte to a register
112 * @param reg Which register. Use constants from nRF24L01.h
113 * @param value The new value to write
114 * @return Current value of status register
116 uint8_t nrf24_write_register(uint8_t reg, uint8_t value);
119 * Write the transmit payload
121 * The size of data written is the fixed payload size, see getPayloadSize()
123 * @param buf Where to get the data
124 * @param len Number of bytes to be sent
125 * @return Current value of status register
127 uint8_t nrf24_write_payload(const void* buf, uint8_t len);
130 * Read the receive payload
132 * The size of data read is the fixed payload size, see getPayloadSize()
134 * @param buf Where to put the data
135 * @param len Maximum number of bytes to read
136 * @return Current value of status register
138 uint8_t nrf24_read_payload(void* buf, uint8_t len);
141 * Empty the receive buffer
143 * @return Current value of status register
145 uint8_t nrf24_flush_rx(void);
148 * Empty the transmit buffer
150 * @return Current value of status register
152 uint8_t nrf24_flush_tx(void);
155 * Retrieve the current status of the chip
157 * @return Current value of status register
159 uint8_t nrf24_get_status(void);
162 * Decode and print the given status to stdout
164 * @param status Status value to print
166 * @warning Does nothing if stdout is not defined. See fdevopen in stdio.h
168 void nrf24_print_status(uint8_t status);
171 * Decode and print the given 'observe_tx' value to stdout
173 * @param value The observe_tx value to print
175 * @warning Does nothing if stdout is not defined. See fdevopen in stdio.h
177 void nrf24_print_observe_tx(uint8_t value);
180 * Print the name and value of an 8-bit register to stdout
182 * Optionally it can print some quantity of successive
183 * registers on the same line. This is useful for printing a group
184 * of related registers on one line.
186 * @param name Name of the register
187 * @param reg Which register. Use constants from nRF24L01.h
188 * @param qty How many successive registers to print
190 void nrf24_print_byte_register(const char* name, uint8_t reg, uint8_t qty);
193 * Print the name and value of a 40-bit address register to stdout
195 * Optionally it can print some quantity of successive
196 * registers on the same line. This is useful for printing a group
197 * of related registers on one line.
199 * @param name Name of the register
200 * @param reg Which register. Use constants from nRF24L01.h
201 * @param qty How many successive registers to print
203 void nrf24_print_address_register(const char* name, uint8_t reg, uint8_t qty);
206 * Turn on or off the special features of the chip
208 * The chip has certain 'features' which are only available when the 'features'
209 * are enabled. See the datasheet for details.
211 void nrf24_toggle_features(void);
212 /**@}*/
215 * @name Primary public interface
217 * These are the main methods you need to operate the chip
219 /**@{*/
222 * Begin operation of the chip
224 * Call this in setup(), before calling any other methods.
226 void nrf24_begin(void);
229 * Start listening on the pipes opened for reading.
231 * Be sure to call openReadingPipe() first. Do not call write() while
232 * in this mode, without first calling stopListening(). Call
233 * isAvailable() to check for incoming traffic, and read() to get it.
235 void nrf24_startListening(void);
238 * Stop listening for incoming messages
240 * Do this before calling write().
242 void nrf24_stopListening(void);
245 * Write to the open writing pipe
247 * Be sure to call openWritingPipe() first to set the destination
248 * of where to write to.
250 * This blocks until the message is successfully acknowledged by
251 * the receiver or the timeout/retransmit maxima are reached. In
252 * the current configuration, the max delay here is 60ms.
254 * The maximum size of data written is the fixed payload size, see
255 * getPayloadSize(). However, you can write less, and the remainder
256 * will just be filled with zeroes.
258 * @param buf Pointer to the data to be sent
259 * @param len Number of bytes to be sent
260 * @return True if the payload was delivered successfully FALSE if not
262 uint8_t nrf24_write( const void* buf, uint8_t len );
265 * Test whether there are bytes available to be read
267 * @return True if there is a payload available, FALSE if none is
269 uint8_t nrf24_available(void);
272 * Read the payload
274 * Return the last payload received
276 * The size of data read is the fixed payload size, see getPayloadSize()
278 * @note I specifically chose 'void*' as a data type to make it easier
279 * for beginners to use. No casting needed.
281 * @param buf Pointer to a buffer where the data should be written
282 * @param len Maximum number of bytes to read into the buffer
283 * @return True if the payload was delivered successfully FALSE if not
285 uint8_t nrf24_read( void* buf, uint8_t len );
288 * Open a pipe for writing
290 * Only one pipe can be open at once, but you can change the pipe
291 * you'll listen to. Do not call this while actively listening.
292 * Remember to stopListening() first.
294 * Addresses are 40-bit hex values, e.g.:
296 * @code
297 * openWritingPipe(0xF0F0F0F0F0);
298 * @endcode
300 * @param address The 40-bit address of the pipe to open. This can be
301 * any value whatsoever, as long as you are the only one writing to it
302 * and only one other radio is listening to it. Coordinate these pipe
303 * addresses amongst nodes on the network.
305 void nrf24_openWritingPipe(uint64_t address);
308 * Open a pipe for reading
310 * Up to 6 pipes can be open for reading at once. Open all the
311 * reading pipes, and then call startListening().
313 * @see openWritingPipe
315 * @warning Pipes 1-5 should share the first 32 bits.
316 * Only the least significant byte should be unique, e.g.
317 * @code
318 * openReadingPipe(1,0xF0F0F0F0AA);
319 * openReadingPipe(2,0xF0F0F0F066);
320 * @endcode
322 * @warning Pipe 0 is also used by the writing pipe. So if you open
323 * pipe 0 for reading, and then startListening(), it will overwrite the
324 * writing pipe. Ergo, do an openWritingPipe() again before write().
326 * @todo Enforce the restriction that pipes 1-5 must share the top 32 bits
328 * @param number Which pipe# to open, 0-5.
329 * @param address The 40-bit address of the pipe to open.
331 void nrf24_openReadingPipe(uint8_t number, uint64_t address);
333 /**@}*/
335 * @name Optional Configurators
337 * Methods you can use to get or set the configuration of the chip.
338 * None are required. Calling begin() sets up a reasonable set of
339 * defaults.
341 /**@{*/
343 * Set the number and delay of retries upon failed submit
345 * @param delay How long to wait between each retry, in multiples of 250us,
346 * max is 15. 0 means 250us, 15 means 4000us.
347 * @param count How many retries before giving up, max 15
349 void nrf24_setRetries(uint8_t delay, uint8_t count);
352 * Set RF communication channel
354 * @param channel Which RF channel to communicate on, 0-127
356 void nrf24_setChannel(uint8_t channel);
359 * Set Static Payload Size
361 * This implementation uses a pre-stablished fixed payload size for all
362 * transmissions. If this method is never called, the driver will always
363 * transmit the maximum payload size (32 bytes), no matter how much
364 * was sent to write().
366 * @todo Implement variable-sized payloads feature
368 * @param size The number of bytes in the payload
370 void nrf24_setPayloadSize(uint8_t size);
373 * Get Static Payload Size
375 * @see setPayloadSize()
377 * @return The number of bytes in the payload
379 uint8_t nrf24_getPayloadSize(void);
382 * Get Dynamic Payload Size
384 * For dynamic payloads, this pulls the size of the payload off
385 * the chip
387 * @return Payload length of last-received dynamic payload
389 uint8_t nrf24_getDynamicPayloadSize(void);
392 * Enable custom payloads on the acknowledge packets
394 * Ack payloads are a handy way to return data back to senders without
395 * manually changing the radio modes on both units.
397 * @see examples/pingpair_pl/pingpair_pl.pde
399 void nrf24_enableAckPayload(void);
402 * Enable dynamically-sized payloads
404 * This way you don't always have to send large packets just to send them
405 * once in a while. This enables dynamic payloads on ALL pipes.
407 * @see examples/pingpair_pl/pingpair_dyn.pde
409 void nrf24_enableDynamicPayloads(void);
412 * Determine whether the hardware is an nRF24L01+ or not.
414 * @return TRUE if the hardware is nRF24L01+ (or compatible) and FALSE
415 * if its not.
417 uint8_t nrf24_isPVariant(void) ;
420 * Enable or disable auto-acknowlede packets
422 * This is enabled by default, so it's only needed if you want to turn
423 * it off for some reason.
425 * @param enable Whether to enable (TRUE) or disable (FALSE) auto-acks
427 void nrf24_setAutoAck(uint8_t enable);
430 * Enable or disable auto-acknowlede packets on a per pipeline basis.
432 * AA is enabled by default, so it's only needed if you want to turn
433 * it off/on for some reason on a per pipeline basis.
435 * @param pipe Which pipeline to modify
436 * @param enable Whether to enable (TRUE) or disable (FALSE) auto-acks
438 void nrf24_setAutoAck_pipe( uint8_t pipe, uint8_t enable ) ;
441 * Set Power Amplifier (PA) level to one of four levels.
442 * Relative mnemonics have been used to allow for future PA level
443 * changes. According to 6.5 of the nRF24L01+ specification sheet,
444 * they translate to: RF24_PA_MIN=-18dBm, RF24_PA_LOW=-12dBm,
445 * RF24_PA_MED=-6dBM, and RF24_PA_HIGH=0dBm.
447 * @param level Desired PA level.
449 void nrf24_setPALevel( rf24_pa_dbm_e level ) ;
452 * Fetches the current PA level.
454 * @return Returns a value from the rf24_pa_dbm_e enum describing
455 * the current PA setting. Please remember, all values represented
456 * by the enum mnemonics are negative dBm. See setPALevel for
457 * return value descriptions.
459 rf24_pa_dbm_e nrf24_getPALevel( void ) ;
462 * Set the transmission data rate
464 * @warning setting RF24_250KBPS will fail for non-plus units
466 * @param speed RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps
467 * @return TRUE if the change was successful
469 uint8_t nrf24_setDataRate(rf24_datarate_e speed);
472 * Fetches the transmission data rate
474 * @return Returns the hardware's currently configured datarate. The value
475 * is one of 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS, as defined in the
476 * rf24_datarate_e enum.
478 rf24_datarate_e nrf24_getDataRate( void ) ;
481 * Set the CRC length
483 * @param length RF24_CRC_8 for 8-bit or RF24_CRC_16 for 16-bit
485 void nrf24_setCRCLength(rf24_crclength_e length);
488 * Get the CRC length
490 * @return RF24_DISABLED if disabled or RF24_CRC_8 for 8-bit or RF24_CRC_16 for 16-bit
492 rf24_crclength_e nrf24_getCRCLength(void);
495 * Disable CRC validation
498 void nrf24_disableCRC( void ) ;
500 /**@}*/
502 * @name Advanced Operation
504 * Methods you can use to drive the chip in more advanced ways
506 /**@{*/
509 * Print a giant block of debugging information to stdout
511 * @warning Does nothing if stdout is not defined. See fdevopen in stdio.h
513 void nrf24_printDetails(void);
516 * Enter low-power mode
518 * To return to normal power mode, either write() some data or
519 * startListening, or powerUp().
521 void nrf24_powerDown(void);
524 * Leave low-power mode - making radio more responsive
526 * To return to low power mode, call powerDown().
528 void nrf24_powerUp(void) ;
531 * Test whether there are bytes available to be read
533 * Use this version to discover on which pipe the message
534 * arrived.
536 * @param[out] pipe_num Which pipe has the payload available
537 * @return True if there is a payload available, FALSE if none is
539 uint8_t nrf24_available_pipe(uint8_t* pipe_num);
542 * Non-blocking write to the open writing pipe
544 * Just like write(), but it returns immediately. To find out what happened
545 * to the send, catch the IRQ and then call whatHappened().
547 * @see write()
548 * @see whatHappened()
550 * @param buf Pointer to the data to be sent
551 * @param len Number of bytes to be sent
552 * @return True if the payload was delivered successfully FALSE if not
554 void nrf24_startWrite( const void* buf, uint8_t len );
557 * Write an ack payload for the specified pipe
559 * The next time a message is received on @p pipe, the data in @p buf will
560 * be sent back in the acknowledgement.
562 * @warning According to the data sheet, only three of these can be pending
563 * at any time. I have not tested this.
565 * @param pipe Which pipe# (typically 1-5) will get this response.
566 * @param buf Pointer to data that is sent
567 * @param len Length of the data to send, up to 32 bytes max. Not affected
568 * by the static payload set by setPayloadSize().
570 void nrf24_writeAckPayload(uint8_t pipe, const void* buf, uint8_t len);
573 * Determine if an ack payload was received in the most recent call to
574 * write().
576 * Call read() to retrieve the ack payload.
578 * @warning Calling this function clears the internal flag which indicates
579 * a payload is available. If it returns TRUE, you must read the packet
580 * out as the very next interaction with the radio, or the results are
581 * undefined.
583 * @return True if an ack payload is available.
585 uint8_t nrf24_isAckPayloadAvailable(void);
588 * Call this when you get an interrupt to find out why
590 * Tells you what caused the interrupt, and clears the state of
591 * interrupts.
593 * @param[out] tx_ok The send was successful (TX_DS)
594 * @param[out] tx_fail The send failed, too many retries (MAX_RT)
595 * @param[out] rx_ready There is a message waiting to be read (RX_DS)
597 void nrf24_whatHappened(uint8_t tx_ok,uint8_t tx_fail,uint8_t rx_ready);
600 * Test whether there was a carrier on the line for the
601 * previous listening period.
603 * Useful to check for interference on the current channel.
605 * @return TRUE if was carrier, FALSE if not
607 uint8_t nrf24_testCarrier(void);
610 * Test whether a signal (carrier or otherwise) greater than
611 * or equal to -64dBm is present on the channel. Valid only
612 * on nRF24L01P (+) hardware. On nRF24L01, use testCarrier().
614 * Useful to check for interference on the current channel and
615 * channel hopping strategies.
617 * @return TRUE if signal => -64dBm, FALSE if not
619 uint8_t nrf24_testRPD(void) ;
621 /**@}*/
624 * @example GettingStarted.pde
626 * This is an example which corresponds to my "Getting Started" blog post:
627 * <a style="text-align:center" href="http://maniacbug.wordpress.com/2011/11/02/getting-started-rf24/">Getting Started with nRF24L01+ on Arduino</a>.
629 * It is an example of how to use the RF24 class. Write this sketch to two
630 * different nodes. Put one of the nodes into 'transmit' mode by connecting
631 * with the serial monitor and sending a 'T'. The ping node sends the current
632 * time to the pong node, which responds by sending the value back. The ping
633 * node can then see how long the whole cycle took.
637 * @example nordic_fob.pde
639 * This is an example of how to use the RF24 class to receive signals from the
640 * Sparkfun Nordic FOB. See http://www.sparkfun.com/products/8602 .
641 * Thanks to Kirk Mower for providing test hardware.
645 * @example led_remote.pde
647 * This is an example of how to use the RF24 class to control a remote
648 * bank of LED's using buttons on a remote control.
650 * Every time the buttons change on the remote, the entire state of
651 * buttons is send to the led board, which displays the state.
655 * @example pingpair.pde
657 * This is an example of how to use the RF24 class. Write this sketch to two
658 * different nodes, connect the role_pin to ground on one. The ping node sends
659 * the current time to the pong node, which responds by sending the value back.
660 * The ping node can then see how long the whole cycle took.
664 * @example pingpair_maple.pde
666 * This is an example of how to use the RF24 class on the Maple. For a more
667 * detailed explanation, see my blog post:
668 * <a href="http://maniacbug.wordpress.com/2011/12/14/nrf24l01-running-on-maple-3/">nRF24L01+ Running on Maple</a>
670 * It will communicate well to an Arduino-based unit as well, so it's not for only Maple-to-Maple communication.
672 * Write this sketch to two different nodes,
673 * connect the role_pin to ground on one. The ping node sends the current time to the pong node,
674 * which responds by sending the value back. The ping node can then see how long the whole cycle
675 * took.
679 * @example starping.pde
681 * This sketch is a more complex example of using the RF24 library for Arduino.
682 * Deploy this on up to six nodes. Set one as the 'pong receiver' by tying the
683 * role_pin low, and the others will be 'ping transmit' units. The ping units
684 * unit will send out the value of millis() once a second. The pong unit will
685 * respond back with a copy of the value. Each ping unit can get that response
686 * back, and determine how long the whole cycle took.
688 * This example requires a bit more complexity to determine which unit is which.
689 * The pong receiver is identified by having its role_pin tied to ground.
690 * The ping senders are further differentiated by a byte in eeprom.
694 * @example pingpair_pl.pde
696 * This is an example of how to do two-way communication without changing
697 * transmit/receive modes. Here, a payload is set to the transmitter within
698 * the Ack packet of each transmission. Note that the payload is set BEFORE
699 * the sender's message arrives.
703 * @example pingpair_irq.pde
705 * This is an example of how to user interrupts to interact with the radio.
706 * It builds on the pingpair_pl example, and uses ack payloads.
710 * @example pingpair_sleepy.pde
712 * This is an example of how to use the RF24 class to create a battery-
713 * efficient system. It is just like the pingpair.pde example, but the
714 * ping node powers down the radio and sleeps the MCU after every
715 * ping/pong cycle.
719 * @example scanner.pde
721 * Example to detect interference on the various channels available.
722 * This is a good diagnostic tool to check whether you're picking a
723 * good channel for your application.
725 * Inspired by cpixip.
726 * See http://arduino.cc/forum/index.php/topic,54795.0.html
730 * @mainpage Driver for nRF24L01(+) 2.4GHz Wireless Transceiver
732 * @section Goals Design Goals
734 * This library is designed to be...
735 * @li Maximally compliant with the intended operation of the chip
736 * @li Easy for beginners to use
737 * @li Consumed with a public interface that's similiar to other Arduino standard libraries
739 * @section News News
741 * NOW COMPATIBLE WITH ARDUINO 1.0 - The 'master' branch and all examples work with both Arduino 1.0 and earlier versions.
742 * Please <a href="https://github.com/maniacbug/RF24/issues/new">open an issue</a> if you find any problems using it with any version of Arduino.
744 * NOW COMPATIBLE WITH MAPLE - RF24 has been tested with the
745 * <a href="http://leaflabs.com/store/#Maple-Native">Maple Native</a>,
746 * and should work with any Maple board. See the pingpair_maple example.
747 * Note that only the pingpair_maple example has been tested on Maple, although
748 * the others can certainly be adapted.
750 * @section Useful Useful References
752 * Please refer to:
754 * @li <a href="http://maniacbug.github.com/RF24/">Documentation Main Page</a>
755 * @li <a href="http://maniacbug.github.com/RF24/classRF24.html">RF24 Class Documentation</a>
756 * @li <a href="https://github.com/maniacbug/RF24/">Source Code</a>
757 * @li <a href="https://github.com/maniacbug/RF24/archives/master">Downloads Page</a>
758 * @li <a href="http://www.nordicsemi.com/files/Product/data_sheet/nRF24L01_Product_Specification_v2_0.pdf">Chip Datasheet</a>
760 * This chip uses the SPI bus, plus two chip control pins. Remember that pin 10 must still remain an output, or
761 * the SPI hardware will go into 'slave' mode.
763 * @section More More Information
765 * @subpage FAQ
767 * @section Projects Projects
769 * Stuff I have built with RF24
771 * <img src="http://farm7.staticflickr.com/6044/6307669179_a8d19298a6_m.jpg" width="240" height="160" alt="RF24 Getting Started - Finished Product">
773 * <a style="text-align:center" href="http://maniacbug.wordpress.com/2011/11/02/getting-started-rf24/">Getting Started with nRF24L01+ on Arduino</a>
775 * <img src="http://farm8.staticflickr.com/7159/6645514331_38eb2bdeaa_m.jpg" width="240" height="160" alt="Nordic FOB and nRF24L01+">
777 * <a style="text-align:center" href="http://maniacbug.wordpress.com/2012/01/08/nordic-fob/">Using the Sparkfun Nordic FOB</a>
779 * <img src="http://farm7.staticflickr.com/6097/6224308836_b9b3b421a3_m.jpg" width="240" height="160" alt="RF Duinode V3 (2V4)">
781 * <a href="http://maniacbug.wordpress.com/2011/10/19/sensor-node/">Low-Power Wireless Sensor Node</a>
783 * <img src="http://farm8.staticflickr.com/7012/6489477865_b56edb629b_m.jpg" width="240" height="161" alt="nRF24L01+ connected to Leaf Labs Maple Native">
785 * <a href="http://maniacbug.wordpress.com/2011/12/14/nrf24l01-running-on-maple-3/">nRF24L01+ Running on Maple</a>
788 #endif // __RF24_H__
789 // vim:ai:cin:sts=2 sw=2 ft=cpp