Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "RF24_config.h"
#include "RF24.h"

using namespace nRF24L01;

/****************************************************************************/

void RF24::csn(bool mode)
Expand Down Expand Up @@ -157,7 +159,7 @@ void RF24::read_register(uint8_t reg, uint8_t* buf, uint8_t len)
*ptx++ = reg;

while (len--) {
*ptx++ = RF24_NOP; // Dummy operation, just for reading
*ptx++ = NOP; // Dummy operation, just for reading
}

#if defined(RF24_RP2)
Expand Down Expand Up @@ -207,7 +209,7 @@ uint8_t RF24::read_register(uint8_t reg)
uint8_t* prx = spi_rxbuff;
uint8_t* ptx = spi_txbuff;
*ptx++ = reg;
*ptx++ = RF24_NOP; // Dummy operation, just for reading
*ptx++ = NOP; // Dummy operation, just for reading

#if defined(RF24_RP2)
_spi->transfernb((const uint8_t*)spi_txbuff, spi_rxbuff, 2);
Expand Down Expand Up @@ -413,7 +415,7 @@ void RF24::read_payload(void* buf, uint8_t data_len)

*ptx++ = R_RX_PAYLOAD;
while (--size) {
*ptx++ = RF24_NOP;
*ptx++ = NOP;
}

size = static_cast<uint8_t>(data_len + blank_len + 1); // Size has been lost during while, re affect
Expand Down Expand Up @@ -718,7 +720,7 @@ void RF24::printDetails(void)
print_byte_register(PSTR("EN_RXADDR"), EN_RXADDR);
print_byte_register(PSTR("RF_CH\t"), RF_CH);
print_byte_register(PSTR("RF_SETUP"), RF_SETUP);
print_byte_register(PSTR("CONFIG\t"), NRF_CONFIG);
print_byte_register(PSTR("CONFIG\t"), CONFIG);
print_byte_register(PSTR("DYNPD/FEATURE"), DYNPD, 2);

printf_P(PSTR("Data Rate\t" PRIPSTR
Expand Down Expand Up @@ -812,7 +814,7 @@ void RF24::printPrettyDetails(void)
static_cast<char>(static_cast<bool>(autoAck & _BV(ENAA_P0)) + 48));
}

config_reg = read_register(NRF_CONFIG);
config_reg = read_register(CONFIG);
printf_P(PSTR("Primary Mode\t\t= %cX\r\n"), config_reg & _BV(PRIM_RX) ? 'R' : 'T');
print_address_register(PSTR("TX address\t"), TX_ADDR);

Expand Down Expand Up @@ -894,7 +896,7 @@ uint16_t RF24::sprintfPrettyDetails(char* debugging_information)
}
offset += sprintf_P(
debugging_information + offset, format_str2,
(read_register(NRF_CONFIG) & _BV(PRIM_RX) ? 'R' : 'T'));
(read_register(CONFIG) & _BV(PRIM_RX) ? 'R' : 'T'));
offset += sprintf_address_register(debugging_information + offset, TX_ADDR);
uint8_t openPipes = read_register(EN_RXADDR);
for (uint8_t i = 0; i < 6; ++i) {
Expand All @@ -919,7 +921,7 @@ uint16_t RF24::sprintfPrettyDetails(char* debugging_information)
void RF24::encodeRadioDetails(uint8_t* encoded_details)
{
uint8_t end = FEATURE + 1;
for (uint8_t i = NRF_CONFIG; i < end; ++i) {
for (uint8_t i = CONFIG; i < end; ++i) {
if (i == RX_ADDR_P0 || i == RX_ADDR_P1 || i == TX_ADDR) {
// get 40-bit registers
read_register(i, encoded_details, 5);
Expand Down Expand Up @@ -1103,7 +1105,7 @@ bool RF24::_init_radio()

// Reset current status
// Notice reset and flush is the last thing we do
write_register(NRF_STATUS, RF24_IRQ_ALL);
write_register(STATUS, RF24_IRQ_ALL);

// Flush buffers
flush_rx();
Expand All @@ -1116,8 +1118,8 @@ bool RF24::_init_radio()
// 16-bit CRC (CRC required by auto-ack)
// Do not write CE high so radio will remain in standby I mode
// PTX should use only 22uA of power
write_register(NRF_CONFIG, (_BV(EN_CRC) | _BV(CRCO) | _BV(MASK_RX_DR) | _BV(MASK_TX_DS) | _BV(MASK_MAX_RT)));
config_reg = read_register(NRF_CONFIG);
write_register(CONFIG, (_BV(EN_CRC) | _BV(CRCO) | _BV(MASK_RX_DR) | _BV(MASK_TX_DS) | _BV(MASK_MAX_RT)));
config_reg = read_register(CONFIG);

powerUp();

Expand Down Expand Up @@ -1147,8 +1149,8 @@ void RF24::startListening(void)
powerUp();
#endif
config_reg |= _BV(PRIM_RX);
write_register(NRF_CONFIG, config_reg);
write_register(NRF_STATUS, RF24_IRQ_ALL);
write_register(CONFIG, config_reg);
write_register(STATUS, RF24_IRQ_ALL);
ce(HIGH);

// Restore the pipe0 address, if exists
Expand Down Expand Up @@ -1176,7 +1178,7 @@ void RF24::stopListening(void)
}

config_reg = static_cast<uint8_t>(config_reg & ~_BV(PRIM_RX));
write_register(NRF_CONFIG, config_reg);
write_register(CONFIG, config_reg);

#if defined(RF24_TINY) || defined(LITTLEWIRE)
// for 3 pins solution TX mode is only left with additional powerDown/powerUp cycle
Expand Down Expand Up @@ -1213,7 +1215,7 @@ void RF24::powerDown(void)
{
ce(LOW); // Guarantee CE is low on powerDown
config_reg = static_cast<uint8_t>(config_reg & ~_BV(PWR_UP));
write_register(NRF_CONFIG, config_reg);
write_register(CONFIG, config_reg);
}

/****************************************************************************/
Expand All @@ -1224,7 +1226,7 @@ void RF24::powerUp(void)
// if not powered up then power up and wait for the radio to initialize
if (!(config_reg & _BV(PWR_UP))) {
config_reg |= _BV(PWR_UP);
write_register(NRF_CONFIG, config_reg);
write_register(CONFIG, config_reg);

// For nRF24L01+ to go from power down mode to TX or RX mode it must first pass through stand-by mode.
// There must be a delay of Tpd2stby (see Table 16.) after the nRF24L01+ leaves power down mode before
Expand Down Expand Up @@ -1323,7 +1325,7 @@ bool RF24::write(const void* buf, uint8_t len, const bool multicast)

ce(LOW);

write_register(NRF_STATUS, RF24_IRQ_ALL);
write_register(STATUS, RF24_IRQ_ALL);

//Max retries exceeded
if (status & RF24_TX_DF) {
Expand Down Expand Up @@ -1397,7 +1399,7 @@ bool RF24::writeBlocking(const void* buf, uint8_t len, uint32_t timeout)
void RF24::reUseTX()
{
ce(LOW);
write_register(NRF_STATUS, RF24_TX_DF); //Clear max retry flag
write_register(STATUS, RF24_TX_DF); //Clear max retry flag
read_register(REUSE_TX_PL, (uint8_t*)nullptr, 0);
IF_RF24_DEBUG(printf_P("[Reusing payload in TX FIFO]"););
ce(HIGH); //Re-Transfer packet
Expand Down Expand Up @@ -1524,7 +1526,7 @@ bool RF24::txStandBy()
#endif
while (!(read_register(FIFO_STATUS) & _BV(TX_EMPTY))) {
if (status & RF24_TX_DF) {
write_register(NRF_STATUS, RF24_TX_DF);
write_register(STATUS, RF24_TX_DF);
ce(LOW);
flush_tx(); //Non blocking, flush the data
#if defined(FAILURE_HANDLING)
Expand Down Expand Up @@ -1560,7 +1562,7 @@ bool RF24::txStandBy(uint32_t timeout, bool startTx)

while (!(read_register(FIFO_STATUS) & _BV(TX_EMPTY))) {
if (status & RF24_TX_DF) {
write_register(NRF_STATUS, RF24_TX_DF);
write_register(STATUS, RF24_TX_DF);
ce(LOW); // Set re-transmit
ce(HIGH);
if (millis() - start >= timeout) {
Expand Down Expand Up @@ -1595,7 +1597,7 @@ void RF24::maskIRQ(bool tx, bool fail, bool rx)
config_reg = static_cast<uint8_t>(config_reg & ~(1 << MASK_MAX_RT | 1 << MASK_TX_DS | 1 << MASK_RX_DR));
/* set the specified interrupt flags */
config_reg = static_cast<uint8_t>(config_reg | fail << MASK_MAX_RT | tx << MASK_TX_DS | rx << MASK_RX_DR);
write_register(NRF_CONFIG, config_reg);
write_register(CONFIG, config_reg);
}

/****************************************************************************/
Expand Down Expand Up @@ -1638,7 +1640,7 @@ void RF24::read(void* buf, uint8_t len)
read_payload(buf, len);

//Clear the only applicable interrupt flags
write_register(NRF_STATUS, RF24_RX_DR);
write_register(STATUS, RF24_RX_DR);
}

/****************************************************************************/
Expand All @@ -1647,7 +1649,7 @@ void RF24::whatHappened(bool& tx_ok, bool& tx_fail, bool& rx_ready)
{
// Read the status & reset the status in one easy call
// Or is that such a good idea?
write_register(NRF_STATUS, RF24_IRQ_ALL);
write_register(STATUS, RF24_IRQ_ALL);

// Report to the user what happened
tx_ok = status & RF24_TX_DS;
Expand All @@ -1659,7 +1661,7 @@ void RF24::whatHappened(bool& tx_ok, bool& tx_fail, bool& rx_ready)

uint8_t RF24::clearStatusFlags(uint8_t flags)
{
write_register(NRF_STATUS, flags & RF24_IRQ_ALL);
write_register(STATUS, flags & RF24_IRQ_ALL);
return status;
}

Expand All @@ -1669,7 +1671,7 @@ void RF24::setStatusFlags(uint8_t flags)
{
// flip the `flags` to translate from "human understanding"
config_reg = (config_reg & ~RF24_IRQ_ALL) | (~flags & RF24_IRQ_ALL);
write_register(NRF_CONFIG, config_reg);
write_register(CONFIG, config_reg);
}

/****************************************************************************/
Expand All @@ -1683,7 +1685,7 @@ uint8_t RF24::getStatusFlags()

uint8_t RF24::update()
{
read_register(RF24_NOP, (uint8_t*)nullptr, 0);
read_register(NOP, (uint8_t*)nullptr, 0);
return status;
}

Expand Down Expand Up @@ -2059,7 +2061,7 @@ void RF24::setCRCLength(rf24_crclength_e length)
config_reg |= _BV(EN_CRC);
config_reg |= _BV(CRCO);
}
write_register(NRF_CONFIG, config_reg);
write_register(CONFIG, config_reg);
}

/****************************************************************************/
Expand All @@ -2068,7 +2070,7 @@ rf24_crclength_e RF24::getCRCLength(void)
{
rf24_crclength_e result = RF24_CRC_DISABLED;
uint8_t AA = read_register(EN_AA);
config_reg = read_register(NRF_CONFIG);
config_reg = read_register(CONFIG);

if (config_reg & _BV(EN_CRC) || AA) {
if (config_reg & _BV(CRCO)) {
Expand All @@ -2087,7 +2089,7 @@ rf24_crclength_e RF24::getCRCLength(void)
void RF24::disableCRC(void)
{
config_reg = static_cast<uint8_t>(config_reg & ~_BV(EN_CRC));
write_register(NRF_CONFIG, config_reg);
write_register(CONFIG, config_reg);
}

/****************************************************************************/
Expand Down
16 changes: 8 additions & 8 deletions RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define RF24_H_

#include "RF24_config.h"
#include "nRF24L01.h"

#if defined(RF24_LINUX) || defined(LITTLEWIRE)
#include "utility/includes.h"
Expand Down Expand Up @@ -139,17 +140,16 @@ typedef enum
*/
typedef enum
{
#include "nRF24L01.h"
/// An alias of `0` to describe no IRQ events enabled.
RF24_IRQ_NONE = 0,
/// Represents an event where TX Data Failed to send.
RF24_TX_DF = 1 << MASK_MAX_RT,
RF24_TX_DF = 1 << nRF24L01::MASK_MAX_RT,
/// Represents an event where TX Data Sent successfully.
RF24_TX_DS = 1 << TX_DS,
RF24_TX_DS = 1 << nRF24L01::TX_DS,
/// Represents an event where RX Data is Ready to `RF24::read()`.
RF24_RX_DR = 1 << RX_DR,
RF24_RX_DR = 1 << nRF24L01::RX_DR,
/// Equivalent to `RF24_RX_DR | RF24_TX_DS | RF24_TX_DF`.
RF24_IRQ_ALL = (1 << MASK_MAX_RT) | (1 << TX_DS) | (1 << RX_DR),
RF24_IRQ_ALL = (1 << nRF24L01::MASK_MAX_RT) | (1 << nRF24L01::TX_DS) | (1 << nRF24L01::RX_DR),
} rf24_irq_flags_e;

/**
Expand Down Expand Up @@ -183,7 +183,7 @@ class RF24
uint8_t payload_size; /* Fixed size of payloads */
uint8_t pipe0_reading_address[5]; /* Last address set on pipe 0 for reading. */
uint8_t pipe0_writing_address[5]; /* Last address set on pipe 0 for writing. */
uint8_t config_reg; /* For storing the value of the NRF_CONFIG register */
uint8_t config_reg; /* For storing the value of the CONFIG register */
bool _is_p_variant; /* For storing the result of testing the toggleFeatures() affect */
bool _is_p0_rx; /* For keeping track of pipe 0's usage in user-triggered RX mode. */

Expand Down Expand Up @@ -760,14 +760,14 @@ class RF24
* Registers names and/or data corresponding to the index of the `encoded_details` array:
* | index | register/data |
* |------:|:--------------|
* | 0 | NRF_CONFIG |
* | 0 | CONFIG |
* | 1 | EN_AA |
* | 2 | EN_RXADDR |
* | 3 | SETUP_AW |
* | 4 | SETUP_RETR |
* | 5 | RF_CH |
* | 6 | RF_SETUP |
* | 7 | NRF_STATUS |
* | 7 | STATUS |
* | 8 | OBSERVE_TX |
* | 9 | CD (aka RPD) |
* | 10-14 | RX_ADDR_P0 |
Expand Down
Loading
Loading