From 059efa5ffa03d544c80d0101c6c6de14a1e5f582 Mon Sep 17 00:00:00 2001 From: Greg Copeland Date: Mon, 27 Jun 2011 15:02:33 -0500 Subject: [PATCH] Re-organized nRF24L01.h file to place 'P' features in one location. Changed some bit value interpretation. Now auto-detects P-vaiants and is queryable. setDataRate now returns a boolean indicating success or failure of the new data rate request. The wide_band state indicator has been added and now tracks the state, including on error fallback. Its now possible to query the current data rate via getDataRate(). The AA retry duration has been changed to 1500us, which is the lowest interval allowed when running at 250Kbs, with AA enabled, and 32-byte packets. --- RF24.cpp | 110 +++++++++++++++++++++++++++++++++++++++++------------ RF24.h | 30 +++++++++++++-- nRF24L01.h | 23 ++++++----- 3 files changed, 125 insertions(+), 38 deletions(-) diff --git a/RF24.cpp b/RF24.cpp index fed43c3..9c897ff 100644 --- a/RF24.cpp +++ b/RF24.cpp @@ -8,8 +8,8 @@ #include #include -#include "RF24.h" #include "nRF24L01.h" +#include "RF24.h" #undef SERIAL_DEBUG #ifdef SERIAL_DEBUG @@ -29,7 +29,7 @@ void RF24::csn(const int mode) const { SPI.setDataMode(SPI_MODE0); - SPI.setClockDivider(SPI_CLOCK_DIV8); + SPI.setClockDivider(SPI_CLOCK_DIV2); digitalWrite(csn_pin,mode); } @@ -210,8 +210,10 @@ void RF24::print_observe_tx(uint8_t value) const RF24::RF24(const uint8_t _cepin, const uint8_t _cspin, const rf24_datarate_e speed, const uint8_t channel): - ce_pin(_cepin), csn_pin(_cspin), wide_band(true), payload_size(32), ack_payload_available(false) + ce_pin(_cepin), csn_pin(_cspin), wide_band(true), p_variant(false), + payload_size(32), ack_payload_available(false) { + begin() ; setDataRate( speed ) ; setChannel( channel ) ; } @@ -283,13 +285,17 @@ void RF24::printDetails(void) const printf_P(PSTR("RX_PW_P1 = 0x%02x\n\r"),*buffer); read_register(EN_AA,buffer,1); - printf_P(PSTR("EN_AA = %02x\n\r"),*buffer); + printf_P(PSTR("EN_AA = 0x%02x\n\r"),*buffer); read_register(EN_RXADDR,buffer,1); - printf_P(PSTR("EN_RXADDR = %02x\n\r"),*buffer); + printf_P(PSTR("EN_RXADDR = 0x%02x\n\r"),*buffer); read_register(RF_CH,buffer,1); - printf_P(PSTR("RF_CH = %02x\n\r"),*buffer); + printf_P(PSTR("RF_CH = 0x%02x\n\r"),*buffer); + + read_register(RF_SETUP,buffer,1); + printf_P(PSTR("RF_SETUP = 0x%02x (data rate: %d)\n\r"),*buffer,getDataRate()); + printf_P(PSTR("Hardware; isPVariant: %d\n\r"),isPVariant()); } /******************************************************************/ @@ -307,8 +313,10 @@ void RF24::begin(void) SPI.setDataMode(SPI_MODE0); SPI.setClockDivider(SPI_CLOCK_DIV8); - // Set generous timeouts, to make testing a little easier - write_register(SETUP_RETR,(B1111 << ARD) | (B1111 << ARC)); + // 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. + write_register(SETUP_RETR,(B0100 << ARD) | (B1111 << ARC)); // Reset current status write_register(STATUS,_BV(RX_DR) | _BV(TX_DS) | _BV(MAX_RT) ); @@ -318,7 +326,16 @@ void RF24::begin(void) // Flush buffers flush_rx(); - flush_tx(); + flush_tx(); + + // Determine if this is a p or non-p RF24 module and then + // reset our data rate back to default value. This works + // because a non-P variant won't allow the data rate to + // be set to 250KBS. + if( setDataRate( RF24_250KBPS ) ) { + p_variant = true ; + } + setDataRate( RF24_2MBPS ) ; } /******************************************************************/ @@ -372,7 +389,7 @@ boolean RF24::write( const void* buf, uint8_t len ) ce(HIGH); // 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. + // if I tighted up the retry logic. (Default settings will be 1500us. // Monitor the send uint8_t observe_tx; uint8_t status; @@ -600,6 +617,12 @@ boolean RF24::isAckPayloadAvailable(void) /******************************************************************/ +boolean RF24::isPVariant(void) const { + return p_variant ; +} + +/******************************************************************/ + void RF24::setAutoAck(const bool enable) const { if ( enable ) @@ -613,7 +636,7 @@ void RF24::setAutoAck(const bool enable) const void RF24::setAutoAck( const uint8_t pipe, const bool enable ) const { uint8_t en_aa = read_register( EN_AA ) ; - en_aa &= ~((enable?0:1)<