From c5823c2605e80ffd73e31d6a5b6a76249be13228 Mon Sep 17 00:00:00 2001 From: maniacbug Date: Wed, 29 Jun 2011 21:38:17 -0700 Subject: [PATCH] Made timeouts more explicit in write() --- RF24.cpp | 36 +++++++++++++++++++--------------- examples/pingpair/pingpair.pde | 3 +++ 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/RF24.cpp b/RF24.cpp index 86c8880..9dc4266 100644 --- a/RF24.cpp +++ b/RF24.cpp @@ -13,7 +13,7 @@ #undef SERIAL_DEBUG #ifdef SERIAL_DEBUG -#define IF_SERIAL_DEBUG(x) (x) +#define IF_SERIAL_DEBUG(x) ({x;}) #else #define IF_SERIAL_DEBUG(x) #endif @@ -365,31 +365,34 @@ boolean RF24::write( const void* buf, uint8_t len ) // Allons! ce(HIGH); + delayMicroseconds(15); + ce(LOW); + + // ------------ + // At this point we could return from a non-blocking write, and then call + // the rest after an interrupt + + // Instead, we are going to block here until we get TX_DS (transmission completed and ack'd) + // or MAX_RT (maximum retries, transmission failed). Also, we'll timeout in case the radio + // is flaky and we get neither. - // IN the end, the send should be blocking. It comes back in 60ms worst case, or much faster - // if I tighted up the retry logic. (Default settings will be 750us. - // Monitor the send uint8_t observe_tx; uint8_t status; - uint8_t retries = 255; + uint32_t sent_at = millis(); + const uint32_t timeout = 100; //ms to wait for timeout do { status = read_register(OBSERVE_TX,&observe_tx,1); - IF_SERIAL_DEBUG(Serial.print(status,HEX)); IF_SERIAL_DEBUG(Serial.print(observe_tx,HEX)); - if ( ! retries-- ) - { - IF_SERIAL_DEBUG(printf("ABORTED: too many retries\n\r")); - break; - } } - while( ! ( status & ( _BV(TX_DS) | _BV(MAX_RT) ) ) ); + while( ! ( status & ( _BV(TX_DS) | _BV(MAX_RT) ) ) && ( millis() - sent_at < timeout ) ); + // What was the result of the send? if ( status & _BV(TX_DS) ) result = true; - IF_SERIAL_DEBUG(Serial.print(result?"...OK.":"...Failed")); - + IF_SERIAL_DEBUG(Serial.print(result?"...OK.":"...Failed"); if ( status & _BV(MAX_RT) ) Serial.print(" too many retries")); + ack_payload_available = ( status & _BV(RX_DR) ); if ( ack_payload_available ) { @@ -400,7 +403,6 @@ boolean RF24::write( const void* buf, uint8_t len ) } // Yay, we are done. - ce(LOW); // Power down write_register(CONFIG,read_register(CONFIG) & ~_BV(PWR_UP)); @@ -440,7 +442,9 @@ boolean RF24::available(void) boolean RF24::available(uint8_t* pipe_num) { uint8_t status = get_status(); - IF_SERIAL_DEBUG(print_status(status)); + + // Too noisy, enable if you really want lots o data!! IF_SERIAL_DEBUG(print_status(status)); + boolean result = ( status & _BV(RX_DR) ); if (result) diff --git a/examples/pingpair/pingpair.pde b/examples/pingpair/pingpair.pde index 9c449cf..1f0429f 100644 --- a/examples/pingpair/pingpair.pde +++ b/examples/pingpair/pingpair.pde @@ -96,6 +96,9 @@ void setup(void) // optionally, use a high channel to avoid WiFi chatter radio.setChannel(110); + // optionally, increase the CRC length for improved reliability + radio.setCRCLength(RF24_CRC_16); + // // Open pipes to other nodes for communication //