Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use ack_payloads_enabled to full potential #2

Merged
merged 1 commit into from
Oct 20, 2020
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
121 changes: 66 additions & 55 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -633,8 +633,8 @@ bool RF24::begin(void)
// Set 1500uS (minimum for 32B payload in ESB@250KBPS) timeouts, to make testing a little easier
// WARNING: If this is ever lowered, either 250KBS mode with AA is broken or maximum packet
// sizes must never be used. See documentation for a more complete explanation.
setRetries(5, 15);
setRetries(5, 15);

// Then set the data rate to the slowest (and most reliable) speed supported by all
// hardware.
setDataRate(RF24_1MBPS);
Expand Down Expand Up @@ -662,7 +662,7 @@ bool RF24::begin(void)
// Clear CONFIG register, Enable PTX, Power Up & 16-bit CRC
// 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)) );
write_register(NRF_CONFIG, (_BV(EN_CRC) | _BV(CRCO)) );
config_reg = read_register(NRF_CONFIG);

powerUp();
Expand Down Expand Up @@ -696,7 +696,7 @@ void RF24::startListening(void)
* 3. Allows time for slower devices to update with the faster startListening() function prior to updating stopListening() & adjusting txDelay
*/
config_reg |= _BV(PRIM_RX);
write_register(NRF_CONFIG,config_reg);
write_register(NRF_CONFIG, config_reg);
write_register(NRF_STATUS, _BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT));
ce(HIGH);
// Restore the pipe0 adddress, if exists
Expand All @@ -705,11 +705,6 @@ void RF24::startListening(void)
} else {
closeReadingPipe(0);
}

if(ack_payloads_enabled){
flush_tx();
}

}

/****************************************************************************/
Expand All @@ -721,12 +716,10 @@ void RF24::stopListening(void)
ce(LOW);

delayMicroseconds(txDelay);

if (read_register(FEATURE) & _BV(EN_ACK_PAY)) {
delayMicroseconds(txDelay); //200
if (ack_payloads_enabled){
flush_tx();
}
//flush_rx();

config_reg &= ~_BV(PRIM_RX);
write_register(NRF_CONFIG, (read_register(NRF_CONFIG)) & ~_BV(PRIM_RX));

Expand Down Expand Up @@ -1027,7 +1020,7 @@ bool RF24::txStandBy(uint32_t timeout, bool startTx)
/****************************************************************************/

void RF24::maskIRQ(bool tx, bool fail, bool rx)
{
{
/* clear the interrupt flags */
config_reg &= ~(1 << MASK_MAX_RT | 1 << MASK_TX_DS | 1 << MASK_RX_DR);
/* set the specified interrupt flags */
Expand Down Expand Up @@ -1279,21 +1272,34 @@ void RF24::disableDynamicPayloads(void)

void RF24::enableAckPayload(void)
{
//
// enable ack payload and dynamic payload features
//
// enable ack payloads and dynamic payload features

//toggle_features();
write_register(FEATURE, read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL));
if (!ack_payloads_enabled){
write_register(FEATURE, read_register(FEATURE) | _BV(EN_ACK_PAY) | _BV(EN_DPL));

IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n", read_register(FEATURE)));
IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n", read_register(FEATURE)));

//
// Enable dynamic payload on pipes 0 & 1
//
write_register(DYNPD, read_register(DYNPD) | _BV(DPL_P1) | _BV(DPL_P0));
dynamic_payloads_enabled = true;
ack_payloads_enabled = true;
//
// Enable dynamic payload on pipes 0 & 1
//
write_register(DYNPD, read_register(DYNPD) | _BV(DPL_P1) | _BV(DPL_P0));
dynamic_payloads_enabled = true;
ack_payloads_enabled = true;
}
}

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

void RF24::disableAckPayload(void)
{
// disable ack payloads (leave dynamic payload features as is)
if (ack_payloads_enabled){
write_register(FEATURE, read_register(FEATURE) | ~_BV(EN_ACK_PAY));

IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n", read_register(FEATURE)));

ack_payloads_enabled = false;
}
}

/****************************************************************************/
Expand All @@ -1307,40 +1313,38 @@ void RF24::enableDynamicAck(void)
write_register(FEATURE, read_register(FEATURE) | _BV(EN_DYN_ACK));

IF_SERIAL_DEBUG(printf("FEATURE=%i\r\n", read_register(FEATURE)));


}

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

void RF24::writeAckPayload(uint8_t pipe, const void* buf, uint8_t len)
{
const uint8_t* current = reinterpret_cast<const uint8_t*>(buf);
if (ack_payloads_enabled){
const uint8_t* current = reinterpret_cast<const uint8_t*>(buf);

uint8_t data_len = rf24_min(len, 32);
uint8_t data_len = rf24_min(len, 32);

#if defined(RF24_LINUX)
beginTransaction();
uint8_t * ptx = spi_txbuff;
uint8_t size = data_len + 1 ; // Add register value to transmit buffer
*ptx++ = W_ACK_PAYLOAD | ( pipe & 0x07 );
while ( data_len-- ){
*ptx++ = *current++;
}

_SPI.transfern( (char *) spi_txbuff, size);
endTransaction();
#else
beginTransaction();
_SPI.transfer(W_ACK_PAYLOAD | (pipe & 0x07));
#if defined(RF24_LINUX)
beginTransaction();
uint8_t * ptx = spi_txbuff;
uint8_t size = data_len + 1 ; // Add register value to transmit buffer
*ptx++ = W_ACK_PAYLOAD | ( pipe & 0x07 );
while ( data_len-- ){
*ptx++ = *current++;
}

while (data_len--) {
_SPI.transfer(*current++);
}
endTransaction();
_SPI.transfern( (char *) spi_txbuff, size);
endTransaction();
#else
beginTransaction();
_SPI.transfer(W_ACK_PAYLOAD | (pipe & 0x07));

#endif
while (data_len--)
_SPI.transfer(*current++);

endTransaction();
#endif
}
}

/****************************************************************************/
Expand All @@ -1364,23 +1368,30 @@ bool RF24::isPVariant(void)

void RF24::setAutoAck(bool enable)
{
if (enable) {
if (enable){
write_register(EN_AA, 0x3F);
} else {
}else{
write_register(EN_AA, 0);
// accomodate ACK payloads feature
if (ack_payloads_enabled){
disableAckPayload();
}
}
}

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

void RF24::setAutoAck(uint8_t pipe, bool enable)
{
if (pipe <= 6) {
if (pipe < 6) {
uint8_t en_aa = read_register(EN_AA);
if (enable) {
en_aa |= _BV(pipe);
} else {
}else{
en_aa &= ~_BV(pipe);
if (ack_payloads_enabled && !pipe){
disableAckPayload();
}
}
write_register(EN_AA, en_aa);
}
Expand Down Expand Up @@ -1524,7 +1535,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);

if (config_reg & _BV(EN_CRC) || AA) {
if (config_reg & _BV(CRCO)) {
result = RF24_CRC_16;
Expand Down Expand Up @@ -1563,7 +1574,7 @@ void RF24::startConstCarrier(rf24_pa_dbm_e level, uint8_t channel )

/****************************************************************************/
void RF24::stopConstCarrier()
{
{
write_register(RF_SETUP, (read_register(RF_SETUP)) & ~_BV(CONT_WAVE));
write_register(RF_SETUP, (read_register(RF_SETUP)) & ~_BV(PLL_LOCK));
ce(LOW);
Expand Down
Loading