Optimized high speed nRF24L01+ driver class documentation v1.4.10
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
115class RF24
116{
117private:
118#ifdef SOFTSPI
119 SoftSPI<SOFT_SPI_MISO_PIN, SOFT_SPI_MOSI_PIN, SOFT_SPI_SCK_PIN, SPI_MODE> spi;
120#elif defined(SPI_UART)
121 SPIUARTClass uspi;
122#endif
123
124#if defined(RF24_LINUX) || defined(XMEGA_D3) /* XMEGA can use SPI class */
125 SPI spi;
126#endif // defined (RF24_LINUX) || defined (XMEGA_D3)
127#if defined(RF24_SPI_PTR)
128 _SPI* _spi;
129#endif // defined (RF24_SPI_PTR)
130
131 rf24_gpio_pin_t ce_pin; /* "Chip Enable" pin, activates the RX or TX role */
132 rf24_gpio_pin_t csn_pin; /* SPI Chip select */
133 uint32_t spi_speed; /* SPI Bus Speed */
134#if defined(RF24_LINUX) || defined(XMEGA_D3) || defined(RF24_RP2)
135 uint8_t spi_rxbuff[32 + 1]; //SPI receive buffer (payload max 32 bytes)
136 uint8_t spi_txbuff[32 + 1]; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command)
137#endif
138 uint8_t status; /* The status byte returned from every SPI transaction */
139 uint8_t payload_size; /* Fixed size of payloads */
140 uint8_t pipe0_reading_address[5]; /* Last address set on pipe 0 for reading. */
141 uint8_t config_reg; /* For storing the value of the NRF_CONFIG register */
142 bool _is_p_variant; /* For storing the result of testing the toggleFeatures() affect */
143 bool _is_p0_rx; /* For keeping track of pipe 0's usage in user-triggered RX mode. */
144
145protected:
152 inline void beginTransaction();
153
154 inline void endTransaction();
155
159 uint8_t addr_width;
162
172 void read_register(uint8_t reg, uint8_t* buf, uint8_t len);
173
180 uint8_t read_register(uint8_t reg);
181
182public:
209 RF24(rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin, uint32_t _spi_speed = RF24_SPI_SPEED);
210
221 RF24(uint32_t _spi_speed = RF24_SPI_SPEED);
222
223#if defined(RF24_LINUX)
224 virtual ~RF24() {};
225#endif
226
241 bool begin(void);
242
243#if defined(RF24_SPI_PTR) || defined(DOXYGEN_FORCED)
259 bool begin(_SPI* spiBus);
260
282 bool begin(_SPI* spiBus, rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin);
283#endif // defined (RF24_SPI_PTR) || defined (DOXYGEN_FORCED)
284
294 bool begin(rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin);
295
299 bool isChipConnected();
300
321 void startListening(void);
322
336 void stopListening(void);
337
369 bool available(void);
370
427 void read(void* buf, uint8_t len);
428
469 bool write(const void* buf, uint8_t len);
470
513 void openWritingPipe(const uint8_t* address);
514
559 void openReadingPipe(uint8_t number, const uint8_t* address);
560
583 void printDetails(void);
584
608 void printPrettyDetails(void);
609
653 uint16_t sprintfPrettyDetails(char* debugging_information);
654
708 void encodeRadioDetails(uint8_t* encoded_status);
709
768 bool available(uint8_t* pipe_num);
769
783 bool rxFifoFull();
784
792 uint8_t isFifo(bool about_tx);
793
801 bool isFifo(bool about_tx, bool check_empty);
802
820 void powerDown(void);
821
828 void powerUp(void);
829
863 bool write(const void* buf, uint8_t len, const bool multicast);
864
913 bool writeFast(const void* buf, uint8_t len);
914
950 bool writeFast(const void* buf, uint8_t len, const bool multicast);
951
993 bool writeBlocking(const void* buf, uint8_t len, uint32_t timeout);
994
1026 bool txStandBy();
1027
1053 bool txStandBy(uint32_t timeout, bool startTx = 0);
1054
1099 bool writeAckPayload(uint8_t pipe, const void* buf, uint8_t len);
1100
1131 void whatHappened(bool& tx_ok, bool& tx_fail, bool& rx_ready);
1132
1171 void startFastWrite(const void* buf, uint8_t len, const bool multicast, bool startTx = 1);
1172
1206 bool startWrite(const void* buf, uint8_t len, const bool multicast);
1207
1238 void reUseTX();
1239
1247 uint8_t flush_tx(void);
1248
1254 uint8_t flush_rx(void);
1255
1264 bool testCarrier(void);
1265
1284 bool testRPD(void);
1285
1293 bool isValid();
1294
1301 void closeReadingPipe(uint8_t pipe);
1302
1303#if defined(FAILURE_HANDLING)
1335#endif // defined (FAILURE_HANDLING)
1336
1353 void setAddressWidth(uint8_t a_width);
1354
1370 void setRetries(uint8_t delay, uint8_t count);
1371
1388 void setChannel(uint8_t channel);
1389
1402 uint8_t getChannel(void);
1403
1421 void setPayloadSize(uint8_t size);
1422
1437 uint8_t getPayloadSize(void);
1438
1459 uint8_t getDynamicPayloadSize(void);
1460
1480 void enableAckPayload(void);
1481
1487 void disableAckPayload(void);
1488
1496 void enableDynamicPayloads(void);
1497
1507 void disableDynamicPayloads(void);
1508
1527 void enableDynamicAck();
1528
1535 bool isPVariant(void);
1536
1567 void setAutoAck(bool enable);
1568
1607 void setAutoAck(uint8_t pipe, bool enable);
1608
1626 void setPALevel(uint8_t level, bool lnaEnable = 1);
1627
1634 uint8_t getPALevel(void);
1635
1643 uint8_t getARC(void);
1644
1661 bool setDataRate(rf24_datarate_e speed);
1662
1670
1682 void setCRCLength(rf24_crclength_e length);
1683
1692
1698 void disableCRC(void);
1699
1730 void maskIRQ(bool tx_ok, bool tx_fail, bool rx_ready);
1731
1743 uint32_t txDelay;
1744
1753 uint32_t csDelay;
1754
1770 void startConstCarrier(rf24_pa_dbm_e level, uint8_t channel);
1771
1786 void stopConstCarrier(void);
1787
1795 void toggleAllPipes(bool isEnabled);
1796
1806 void setRadiation(uint8_t level, rf24_datarate_e speed, bool lnaEnable = true);
1807
1839 void openReadingPipe(uint8_t number, uint64_t address);
1840
1854 void openWritingPipe(uint64_t address);
1855
1865 bool isAckPayloadAvailable(void);
1866
1867private:
1882 void _init_obj();
1883
1889 bool _init_radio();
1890
1894 bool _init_pins();
1895
1906 void csn(bool mode);
1907
1914 void ce(bool level);
1915
1925 void write_register(uint8_t reg, const uint8_t* buf, uint8_t len);
1926
1935 void write_register(uint8_t reg, uint8_t value);
1936
1948 void write_payload(const void* buf, uint8_t len, const uint8_t writeType);
1949
1960 void read_payload(void* buf, uint8_t len);
1961
1967 uint8_t get_status(void);
1968
1969#if !defined(MINIMAL)
1970
1978 void print_status(uint8_t status);
1979
1987 void print_observe_tx(uint8_t value);
1988
2000 void print_byte_register(const char* name, uint8_t reg, uint8_t qty = 1);
2001
2013 void print_address_register(const char* name, uint8_t reg, uint8_t qty = 1);
2014
2027 uint8_t sprintf_address_register(char* out_buffer, uint8_t reg, uint8_t qty = 1);
2028#endif
2029
2036 void toggle_features(void);
2037
2038#if defined(FAILURE_HANDLING) || defined(RF24_LINUX)
2039
2040 void errNotify(void);
2041
2042#endif
2043
2050 inline uint8_t _data_rate_reg_value(rf24_datarate_e speed);
2051
2059 inline uint8_t _pa_level_reg_value(uint8_t level, bool lnaEnable);
2060
2062};
2063
2413#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:116
void disableAckPayload(void)
Definition RF24.cpp:1717
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:817
bool begin(void)
Definition RF24.cpp:954
uint8_t getPayloadSize(void)
Definition RF24.cpp:622
bool available(void)
Definition RF24.cpp:1489
bool txStandBy()
Definition RF24.cpp:1399
void endTransaction()
Definition RF24.cpp:135
bool failureDetected
Definition RF24.h:1334
void startListening(void)
Definition RF24.cpp:1125
bool isAckPayloadAvailable(void)
Definition RF24.cpp:1757
void printPrettyDetails(void)
Definition RF24.cpp:720
void setPayloadSize(uint8_t size)
Definition RF24.cpp:609
bool isValid()
Definition RF24.cpp:1118
bool writeAckPayload(uint8_t pipe, const void *buf, uint8_t len)
Definition RF24.cpp:1744
uint8_t isFifo(bool about_tx)
Definition RF24.cpp:1385
void stopConstCarrier(void)
Definition RF24.cpp:1975
bool dynamic_payloads_enabled
Definition RF24.h:161
void enableDynamicPayloads(void)
Definition RF24.cpp:1658
bool writeFast(const void *buf, uint8_t len)
Definition RF24.cpp:1338
void disableDynamicPayloads(void)
Definition RF24.cpp:1678
void setRetries(uint8_t delay, uint8_t count)
Definition RF24.cpp:1935
bool write(const void *buf, uint8_t len)
Definition RF24.cpp:1253
uint8_t getARC(void)
Definition RF24.cpp:1837
uint8_t flush_rx(void)
Definition RF24.cpp:469
void beginTransaction()
Definition RF24.cpp:117
void powerUp(void)
Definition RF24.cpp:1184
void setChannel(uint8_t channel)
Definition RF24.cpp:596
void disableCRC(void)
Definition RF24.cpp:1928
void enableDynamicAck()
Definition RF24.cpp:1731
bool isPVariant(void)
Definition RF24.cpp:1765
uint8_t getDynamicPayloadSize(void)
Definition RF24.cpp:1475
bool ack_payloads_enabled
Definition RF24.h:157
uint8_t getChannel(void)
Definition RF24.cpp:602
void stopListening(void)
Definition RF24.cpp:1149
rf24_datarate_e getDataRate(void)
Definition RF24.cpp:1864
bool testRPD(void)
Definition RF24.cpp:1814
void setCRCLength(rf24_crclength_e length)
Definition RF24.cpp:1888
void read(void *buf, uint8_t len)
Definition RF24.cpp:1512
uint32_t txDelay
Definition RF24.h:1743
void closeReadingPipe(uint8_t pipe)
Definition RF24.cpp:1632
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:1605
void powerDown(void)
Definition RF24.cpp:1174
void toggleAllPipes(bool isEnabled)
Open or close all data pipes.
Definition RF24.cpp:1990
uint8_t addr_width
Definition RF24.h:159
void setPALevel(uint8_t level, bool lnaEnable=1)
Definition RF24.cpp:1821
rf24_crclength_e getCRCLength(void)
Definition RF24.cpp:1908
void encodeRadioDetails(uint8_t *encoded_status)
Definition RF24.cpp:900
void maskIRQ(bool tx_ok, bool tx_fail, bool rx_ready)
Definition RF24.cpp:1464
void enableAckPayload(void)
Definition RF24.cpp:1699
bool isChipConnected()
Definition RF24.cpp:1111
void startConstCarrier(rf24_pa_dbm_e level, uint8_t channel)
Definition RF24.cpp:1941
void startFastWrite(const void *buf, uint8_t len, const bool multicast, bool startTx=1)
Definition RF24.cpp:1350
uint32_t csDelay
Definition RF24.h:1753
bool testCarrier(void)
Definition RF24.cpp:1807
bool rxFifoFull()
Definition RF24.cpp:1378
void setAddressWidth(uint8_t a_width)
Definition RF24.cpp:1590
void setRadiation(uint8_t level, rf24_datarate_e speed, bool lnaEnable=true)
configure the RF_SETUP register in 1 transaction
Definition RF24.cpp:2040
uint8_t flush_tx(void)
Definition RF24.cpp:478
bool startWrite(const void *buf, uint8_t len, const bool multicast)
Definition RF24.cpp:1363
void printDetails(void)
Definition RF24.cpp:676
bool writeBlocking(const void *buf, uint8_t len, uint32_t timeout)
Definition RF24.cpp:1261
void reUseTX()
Definition RF24.cpp:1296
bool setDataRate(rf24_datarate_e speed)
Definition RF24.cpp:1844
void setAutoAck(bool enable)
Definition RF24.cpp:1772
void openWritingPipe(const uint8_t *address)
Definition RF24.cpp:1549
uint8_t getPALevel(void)
Definition RF24.cpp:1830
void whatHappened(bool &tx_ok, bool &tx_fail, bool &rx_ready)
Definition RF24.cpp:1524
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