Skip to content

Commit

Permalink
Reduce error rate in call response scenarios
Browse files Browse the repository at this point in the history
- Added delay to first call to available()
a: Only delays IF called after startListening AND called less than a
certain time after the last time it was called
- Corrected mistake on last commit where flush_rx was added back
inadvertently
  • Loading branch information
TMRh20 committed Sep 14, 2014
1 parent 82629d4 commit 9e28a60
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
9 changes: 6 additions & 3 deletions RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,9 +834,12 @@ bool RF24::available(void)

bool RF24::available(uint8_t* pipe_num)
{

//Check the FIFO buffer to see if data is waitng to be read

//Check the FIFO buffer to see if data is waiting to be read
if(listeningStarted){
while(micros() - lastAvailableCheck < 800 && listeningStarted){};
lastAvailableCheck = micros();
listeningStarted = 0;
}
if (!( read_register(FIFO_STATUS) & _BV(RX_EMPTY) )){

// If the caller wants the pipe number, include that
Expand Down
3 changes: 2 additions & 1 deletion RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ class RF24
bool dynamic_payloads_enabled; /**< Whether dynamic payloads are enabled. */
uint8_t pipe0_reading_address[5]; /**< Last address set on pipe 0 for reading. */
uint8_t addr_width; /**< The address width to use - 3,4 or 5 bytes. */
uint32_t lastAvailableCheck = 0; /**< Limits the amount of time betwen reading data, only when switching between modes */
boolean listeningStarted = 1; /**< Var for delaying available() after start listening */

This comment has been minimized.

Copy link
@reixd

reixd Sep 17, 2014

Contributor

This is now allowed by the compiler. This variables should be initialized in the constructor.



public:

/**
Expand Down
16 changes: 11 additions & 5 deletions RPi/RF24/RF24.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ void RF24::startListening(void)
closeReadingPipe(0);
}
// Flush buffers
flush_rx();
//flush_rx();
if(read_register(FEATURE) & _BV(EN_ACK_PAY)){
flush_tx();
}
Expand All @@ -523,7 +523,8 @@ void RF24::startListening(void)
bcm2835_gpio_write(ce_pin, HIGH);

// wait for the radio to come up (130us actually only needed)
delayMicroseconds(130);
//delayMicroseconds(130);
listeningStarted = 1;
}

/****************************************************************************/
Expand All @@ -541,7 +542,7 @@ void RF24::stopListening(void)
if(read_register(FEATURE) & _BV(EN_ACK_PAY)){
flush_tx();
}
flush_rx();
//flush_rx();

write_register(CONFIG, ( read_register(CONFIG) ) & ~_BV(PRIM_RX) );
write_register(EN_RXADDR,read_register(EN_RXADDR) | _BV(pgm_read_byte(&child_pipe_enable[0])));
Expand Down Expand Up @@ -812,11 +813,16 @@ bool RF24::available(void)
/****************************************************************************/

bool RF24::available(uint8_t* pipe_num)
{
{

if(listeningStarted){
if(millis() - lastAvailableCheck < 2 && listeningStarted){delayMicroseconds(400);}
lastAvailableCheck = millis();
listeningStarted = 0;
}
//Check the FIFO buffer to see if data is waitng to be read
if (!( read_register(FIFO_STATUS) & _BV(RX_EMPTY) )){


// If the caller wants the pipe number, include that
if ( pipe_num ){
uint8_t status = get_status();
Expand Down
3 changes: 3 additions & 0 deletions RPi/RF24/RF24.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ class RF24
uint8_t spi_rxbuff[32+1] ; //SPI receive buffer (payload max 32 bytes)
uint8_t spi_txbuff[32+1] ; //SPI transmit buffer (payload max 32 bytes + 1 byte for the command)

bool listeningStarted; /**< Var for delaying available() after start listening */
uint32_t lastAvailableCheck; /**< Var for preventing available() from being called too often */

protected:
/**
* @name Low-level internal interface.
Expand Down

0 comments on commit 9e28a60

Please sign in to comment.