Optimized high speed nRF24L01+ driver class documentation v1.4.11
TMRh20 2020 - Optimized fork of the nRF24L01+ driver
Loading...
Searching...
No Matches
RF24.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2011 J. Coliz <maniacbug@ymail.com>
3
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 */
8
15#ifndef RF24_H_
16#define RF24_H_
17
18#include "RF24_config.h"
19
20#if defined(RF24_LINUX) || defined(LITTLEWIRE)
21 #include "utility/includes.h"
22#elif defined SOFTSPI
23 #include <DigitalIO.h>
24#endif
25
70
89
110
130
135class RF24
136{
137private:
138#ifdef SOFTSPI
139 SoftSPI<SOFT_SPI_MISO_PIN, SOFT_SPI_MOSI_PIN, SOFT_SPI_SCK_PIN, SPI_MODE> spi;
140#elif defined(SPI_UART)
141 SPIUARTClass uspi;
142#endif
143
144#if defined(RF24_LINUX) || defined(XMEGA_D3) /* XMEGA can use SPI class */
145 SPI spi;
146#endif // defined (RF24_LINUX) || defined (XMEGA_D3)
147#if defined(RF24_SPI_PTR)
148 _SPI* _spi;
149#endif // defined (RF24_SPI_PTR)
150
151 rf24_gpio_pin_t ce_pin; /* "Chip Enable" pin, activates the RX or TX role */
152 rf24_gpio_pin_t csn_pin; /* SPI Chip select */
153 uint32_t spi_speed; /* SPI Bus Speed */
154#if defined(RF24_LINUX) || defined(XMEGA_D3) || defined(RF24_RP2)
155 uint8_t spi_rxbuff[32 + 1]; //SPI receive buffer (payload max 32 bytes)
156 uint8_t spi_txbuff[32 + 1]; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command)
157#endif
158 uint8_t status; /* The status byte returned from every SPI transaction */
159 uint8_t payload_size; /* Fixed size of payloads */
160 uint8_t pipe0_reading_address[5]; /* Last address set on pipe 0 for reading. */
161 uint8_t config_reg; /* For storing the value of the NRF_CONFIG register */
162 bool _is_p_variant; /* For storing the result of testing the toggleFeatures() affect */
163 bool _is_p0_rx; /* For keeping track of pipe 0's usage in user-triggered RX mode. */
164
165protected:
172 inline void beginTransaction();
173
174 inline void endTransaction();
175
179 uint8_t addr_width;
182
192 void read_register(uint8_t reg, uint8_t* buf, uint8_t len);
193
200 uint8_t read_register(uint8_t reg);
201
202public:
229 RF24(rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin, uint32_t _spi_speed = RF24_SPI_SPEED);
230
241 RF24(uint32_t _spi_speed = RF24_SPI_SPEED);
242
243#if defined(RF24_LINUX)
244 virtual ~RF24() {};
245#endif
246
261 bool begin(void);
262
263#if defined(RF24_SPI_PTR) || defined(DOXYGEN_FORCED)
279 bool begin(_SPI* spiBus);
280
302 bool begin(_SPI* spiBus, rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin);
303#endif // defined (RF24_SPI_PTR) || defined (DOXYGEN_FORCED)
304
314 bool begin(rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin);
315
319 bool isChipConnected();
320
341 void startListening(void);
342
356 void stopListening(void);
357
389 bool available(void);
390
447 void read(void* buf, uint8_t len);
448
489 bool write(const void* buf, uint8_t len);
490
533 void openWritingPipe(const uint8_t* address);
534
579 void openReadingPipe(uint8_t number, const uint8_t* address);
580
603 void printDetails(void);
604
628 void printPrettyDetails(void);
629
673 uint16_t sprintfPrettyDetails(char* debugging_information);
674
728 void encodeRadioDetails(uint8_t* encoded_status);
729
788 bool available(uint8_t* pipe_num);
789
803 bool rxFifoFull();
804
813 rf24_fifo_state_e isFifo(bool about_tx);
814
823 bool isFifo(bool about_tx, bool check_empty);
824
842 void powerDown(void);
843
850 void powerUp(void);
851
885 bool write(const void* buf, uint8_t len, const bool multicast);
886
935 bool writeFast(const void* buf, uint8_t len);
936
972 bool writeFast(const void* buf, uint8_t len, const bool multicast);
973
1015 bool writeBlocking(const void* buf, uint8_t len, uint32_t timeout);
1016
1048 bool txStandBy();
1049
1075 bool txStandBy(uint32_t timeout, bool startTx = 0);
1076
1121 bool writeAckPayload(uint8_t pipe, const void* buf, uint8_t len);
1122
1153 void whatHappened(bool& tx_ok, bool& tx_fail, bool& rx_ready);
1154
1193 void startFastWrite(const void* buf, uint8_t len, const bool multicast, bool startTx = 1);
1194
1228 bool startWrite(const void* buf, uint8_t len, const bool multicast);
1229
1260 void reUseTX();
1261
1269 uint8_t flush_tx(void);
1270
1276 uint8_t flush_rx(void);
1277
1286 bool testCarrier(void);
1287
1306 bool testRPD(void);
1307
1315 bool isValid();
1316
1323 void closeReadingPipe(uint8_t pipe);
1324
1325#if defined(FAILURE_HANDLING)
1357#endif // defined (FAILURE_HANDLING)
1358
1375 void setAddressWidth(uint8_t a_width);
1376
1392 void setRetries(uint8_t delay, uint8_t count);
1393
1410 void setChannel(uint8_t channel);
1411
1424 uint8_t getChannel(void);
1425
1443 void setPayloadSize(uint8_t size);
1444
1459 uint8_t getPayloadSize(void);
1460
1481 uint8_t getDynamicPayloadSize(void);
1482
1502 void enableAckPayload(void);
1503
1509 void disableAckPayload(void);
1510
1518 void enableDynamicPayloads(void);
1519
1529 void disableDynamicPayloads(void);
1530
1549 void enableDynamicAck();
1550
1557 bool isPVariant(void);
1558
1589 void setAutoAck(bool enable);
1590
1629 void setAutoAck(uint8_t pipe, bool enable);
1630
1648 void setPALevel(uint8_t level, bool lnaEnable = 1);
1649
1656 uint8_t getPALevel(void);
1657
1665 uint8_t getARC(void);
1666
1683 bool setDataRate(rf24_datarate_e speed);
1684
1692
1704 void setCRCLength(rf24_crclength_e length);
1705
1714
1720 void disableCRC(void);
1721
1752 void maskIRQ(bool tx_ok, bool tx_fail, bool rx_ready);
1753
1765 uint32_t txDelay;
1766
1775 uint32_t csDelay;
1776
1792 void startConstCarrier(rf24_pa_dbm_e level, uint8_t channel);
1793
1808 void stopConstCarrier(void);
1809
1817 void toggleAllPipes(bool isEnabled);
1818
1828 void setRadiation(uint8_t level, rf24_datarate_e speed, bool lnaEnable = true);
1829
1861 void openReadingPipe(uint8_t number, uint64_t address);
1862
1876 void openWritingPipe(uint64_t address);
1877
1887 bool isAckPayloadAvailable(void);
1888
1889private:
1904 void _init_obj();
1905
1911 bool _init_radio();
1912
1916 bool _init_pins();
1917
1928 void csn(bool mode);
1929
1936 void ce(bool level);
1937
1947 void write_register(uint8_t reg, const uint8_t* buf, uint8_t len);
1948
1957 void write_register(uint8_t reg, uint8_t value);
1958
1970 void write_payload(const void* buf, uint8_t len, const uint8_t writeType);
1971
1982 void read_payload(void* buf, uint8_t len);
1983
1989 uint8_t get_status(void);
1990
1991#if !defined(MINIMAL)
1992
2000 void print_status(uint8_t status);
2001
2009 void print_observe_tx(uint8_t value);
2010
2022 void print_byte_register(const char* name, uint8_t reg, uint8_t qty = 1);
2023
2035 void print_address_register(const char* name, uint8_t reg, uint8_t qty = 1);
2036
2049 uint8_t sprintf_address_register(char* out_buffer, uint8_t reg, uint8_t qty = 1);
2050#endif
2051
2058 void toggle_features(void);
2059
2060#if defined(FAILURE_HANDLING) || defined(RF24_LINUX)
2061
2062 void errNotify(void);
2063
2064#endif
2065
2072 inline uint8_t _data_rate_reg_value(rf24_datarate_e speed);
2073
2081 inline uint8_t _pa_level_reg_value(uint8_t level, bool lnaEnable);
2082
2084};
2085
2435#endif // RF24_H_
#define RF24_SPI_SPEED
The default SPI speed (in Hz)
Definition RF24_config.h:44
Driver class for nRF24L01(+) 2.4GHz Wireless Transceiver.
Definition RF24.h:136
void disableAckPayload(void)
Definition RF24.cpp:1740
RF24(rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin, uint32_t _spi_speed=RF24_SPI_SPEED)
Definition RF24.cpp:564
uint16_t sprintfPrettyDetails(char *debugging_information)
Definition RF24.cpp:839
bool begin(void)
Definition RF24.cpp:976
uint8_t getPayloadSize(void)
Definition RF24.cpp:644
bool available(void)
Definition RF24.cpp:1517
bool txStandBy()
Definition RF24.cpp:1427
void endTransaction()
Definition RF24.cpp:135
bool failureDetected
Definition RF24.h:1356
void startListening(void)
Definition RF24.cpp:1147
bool isAckPayloadAvailable(void)
Definition RF24.cpp:1780
void printPrettyDetails(void)
Definition RF24.cpp:742
void setPayloadSize(uint8_t size)
Definition RF24.cpp:631
bool isValid()
Definition RF24.cpp:1140
bool writeAckPayload(uint8_t pipe, const void *buf, uint8_t len)
Definition RF24.cpp:1767
void stopConstCarrier(void)
Definition RF24.cpp:1997
rf24_fifo_state_e isFifo(bool about_tx)
Definition RF24.cpp:1412
bool dynamic_payloads_enabled
Definition RF24.h:181
void enableDynamicPayloads(void)
Definition RF24.cpp:1681
bool writeFast(const void *buf, uint8_t len)
Definition RF24.cpp:1360
void disableDynamicPayloads(void)
Definition RF24.cpp:1701
void setRetries(uint8_t delay, uint8_t count)
Definition RF24.cpp:1957
bool write(const void *buf, uint8_t len)
Definition RF24.cpp:1275
uint8_t getARC(void)
Definition RF24.cpp:1859
uint8_t flush_rx(void)
Definition RF24.cpp:469
void beginTransaction()
Definition RF24.cpp:117
void powerUp(void)
Definition RF24.cpp:1206
void setChannel(uint8_t channel)
Definition RF24.cpp:618
void disableCRC(void)
Definition RF24.cpp:1950
void enableDynamicAck()
Definition RF24.cpp:1754
bool isPVariant(void)
Definition RF24.cpp:1787
uint8_t getDynamicPayloadSize(void)
Definition RF24.cpp:1503
bool ack_payloads_enabled
Definition RF24.h:177
uint8_t getChannel(void)
Definition RF24.cpp:624
void stopListening(void)
Definition RF24.cpp:1171
rf24_datarate_e getDataRate(void)
Definition RF24.cpp:1886
bool testRPD(void)
Definition RF24.cpp:1836
void setCRCLength(rf24_crclength_e length)
Definition RF24.cpp:1910
void read(void *buf, uint8_t len)
Definition RF24.cpp:1535
uint32_t txDelay
Definition RF24.h:1765
void closeReadingPipe(uint8_t pipe)
Definition RF24.cpp:1655
void read_register(uint8_t reg, uint8_t *buf, uint8_t len)
Definition RF24.cpp:149
void openReadingPipe(uint8_t number, const uint8_t *address)
Definition RF24.cpp:1628
void powerDown(void)
Definition RF24.cpp:1196
void toggleAllPipes(bool isEnabled)
Open or close all data pipes.
Definition RF24.cpp:2012
uint8_t addr_width
Definition RF24.h:179
void setPALevel(uint8_t level, bool lnaEnable=1)
Definition RF24.cpp:1843
rf24_crclength_e getCRCLength(void)
Definition RF24.cpp:1930
void encodeRadioDetails(uint8_t *encoded_status)
Definition RF24.cpp:922
void maskIRQ(bool tx_ok, bool tx_fail, bool rx_ready)
Definition RF24.cpp:1492
void enableAckPayload(void)
Definition RF24.cpp:1722
bool isChipConnected()
Definition RF24.cpp:1133
void startConstCarrier(rf24_pa_dbm_e level, uint8_t channel)
Definition RF24.cpp:1963
void startFastWrite(const void *buf, uint8_t len, const bool multicast, bool startTx=1)
Definition RF24.cpp:1372
uint32_t csDelay
Definition RF24.h:1775
bool testCarrier(void)
Definition RF24.cpp:1829
bool rxFifoFull()
Definition RF24.cpp:1405
void setAddressWidth(uint8_t a_width)
Definition RF24.cpp:1613
void setRadiation(uint8_t level, rf24_datarate_e speed, bool lnaEnable=true)
configure the RF_SETUP register in 1 transaction
Definition RF24.cpp:2062
uint8_t flush_tx(void)
Definition RF24.cpp:478
bool startWrite(const void *buf, uint8_t len, const bool multicast)
Definition RF24.cpp:1385
void printDetails(void)
Definition RF24.cpp:698
bool writeBlocking(const void *buf, uint8_t len, uint32_t timeout)
Definition RF24.cpp:1283
void reUseTX()
Definition RF24.cpp:1318
bool setDataRate(rf24_datarate_e speed)
Definition RF24.cpp:1866
void setAutoAck(bool enable)
Definition RF24.cpp:1794
void openWritingPipe(const uint8_t *address)
Definition RF24.cpp:1572
uint8_t getPALevel(void)
Definition RF24.cpp:1852
void whatHappened(bool &tx_ok, bool &tx_fail, bool &rx_ready)
Definition RF24.cpp:1547
rf24_crclength_e
Definition RF24.h:102
@ RF24_CRC_16
Definition RF24.h:108
@ RF24_CRC_DISABLED
Definition RF24.h:104
@ RF24_CRC_8
Definition RF24.h:106
rf24_datarate_e
Definition RF24.h:81
@ RF24_2MBPS
Definition RF24.h:85
@ RF24_250KBPS
Definition RF24.h:87
@ RF24_1MBPS
Definition RF24.h:83
rf24_pa_dbm_e
Definition RF24.h:36
@ RF24_PA_ERROR
Definition RF24.h:68
@ RF24_PA_MIN
Definition RF24.h:43
@ RF24_PA_LOW
Definition RF24.h:50
@ RF24_PA_MAX
Definition RF24.h:64
@ RF24_PA_HIGH
Definition RF24.h:57
#define delay(millisec)
uint16_t rf24_gpio_pin_t
#define _SPI
rf24_fifo_state_e
Definition RF24.h:120
@ RF24_FIFO_INVALID
Represents corruption of data over SPI (when observed).
Definition RF24.h:128
@ RF24_FIFO_FULL
The FIFO is full.
Definition RF24.h:126
@ RF24_FIFO_OCCUPIED
The FIFO is not full nor empty, but it is occupied with 1 or 2 payloads.
Definition RF24.h:122
@ RF24_FIFO_EMPTY
The FIFO is empty.
Definition RF24.h:124