diff --git a/RF24.cpp b/RF24.cpp index d3854fb..f443dbe 100644 --- a/RF24.cpp +++ b/RF24.cpp @@ -514,7 +514,7 @@ void RF24::openReadingPipe(uint8_t child, uint64_t value) if (child == 0) pipe0_reading_address = value; - if (child < 5) + if (child < 6) { // For pipes 2-5, only write the LSB if ( child < 2 ) @@ -524,7 +524,7 @@ void RF24::openReadingPipe(uint8_t child, uint64_t value) write_register(child_payload_size[child],payload_size); - // Note this is kind of an inefficient way to set up these enable bits, bit I thought it made + // Note this is kind of an inefficient way to set up these enable bits, but I thought it made // the calling code more simple uint8_t en_rx; read_register(EN_RXADDR,&en_rx,1); @@ -613,13 +613,95 @@ boolean RF24::testCarrier(void) /******************************************************************/ +boolean RF24::testRPD(void) +{ + return ( read_register(RPD) & 1 ) ; +} + +/******************************************************************/ + +void RF24::setPALevel(rf24_pa_dbm_e level) +{ + uint8_t setup = read_register(RF_SETUP) ; + setup &= ~(_BV(RF_PWR_LOW) | _BV(RF_PWR_HIGH)) ; + + switch( level ) + { + case RF24_PA_MAX: + setup |= RF_PWR_0DB ; + break ; + + case RF24_PA_HIGH: + setup |= RF_PWR_6DB ; + break ; + + case RF24_PA_LOW: + setup |= RF_PWR_12DB ; + break ; + + case RF24_PA_MIN: + setup |= RF_PWR_18DB ; + break ; + } + + write_register( RF_SETUP, setup ) ; +} + +/******************************************************************/ + +rf24_pa_dbm_e RF24::getPALevel(void) +{ + rf24_pa_dbm_e result = RF24_PA_ERROR ; + uint8_t power = read_register(RF_SETUP) & RF_PWR ; + + switch( power ) + { + case RF_PWR_0DB: + result = RF24_PA_MAX ; + break ; + + case RF_PWR_6DB: + result = RF24_PA_HIGH ; + break ; + + case RF_PWR_12DB: + result = RF24_PA_LOW ; + break ; + + case RF_PWR_18DB: + result = RF24_PA_MIN ; + break ; + } + + return result ; +} + +/******************************************************************/ + void RF24::setDataRate(rf24_datarate_e speed) { - uint8_t setup = read_register(RF_SETUP) & RF_DR; - if (speed == RF24_2MBPS) - setup |= _BV(RF_DR); - write_register(RF_SETUP,setup); + uint8_t setup = read_register(RF_SETUP) ; + // HIGH and LOW '00' is 1Mbs - our default + setup &= ~(_BV(RF_DR_LOW) | _BV(RF_DR_HIGH)) ; + if( speed == RF24_250KBPS ) + { + // Must set the RF_DR_LOW to 1; RF_DR_HIGH (used to be RF_DR) is already 0 + // Making it '10'. + setup |= _BV( RF_DR_LOW ) ; + } + else + { + // Set 2Mbs, RF_DR (RF_DR_HIGH) is set 1 + // Making it '01' + if ( speed == RF24_2MBPS ) + { + setup |= _BV(RF_DR_HIGH); + } + + } + + write_register(RF_SETUP,setup); } /******************************************************************/ diff --git a/RF24.h b/RF24.h index 386b628..70c5028 100644 --- a/RF24.h +++ b/RF24.h @@ -11,7 +11,8 @@ #include -typedef enum { RF24_1MBPS = 0, RF24_2MBPS } rf24_datarate_e; +typedef enum { RF24_PA_MIN = 0,RF24_PA_LOW, RF24_PA_HIGH, RF24_PA_MAX, RF24_PA_ERROR } rf24_pa_dbm_e ; +typedef enum { RF24_1MBPS = 0, RF24_2MBPS, RF24_250KBPS } rf24_datarate_e; typedef enum { RF24_CRC_8 = 0, RF24_CRC_16 } rf24_crclength_e; /** @@ -423,10 +424,39 @@ public: */ boolean testCarrier(void); + /** + * Test whether a signal (carrier or otherwise) greater than + * or equal to -64dBm is present on the channel. Valid only + * on nRF24L01P (+) hardware. On nRF24L01, use testCarrier(). + * + * Useful to check for interference on the current channel and + * channel hopping strategies. + * + * @return true if signal => -64dBm, false if not + */ + boolean testRPD(void); + + /** + * Set Power Amplifier (PA) level to one of four levels. + * Relative mnemonics have been used to allow for future PA level + * changes. According to 6.5 of the nRF24L01+ specification sheet, + * they translate to: RF24_PA_MIN=-18dBm, RF24_PA_LOW=-12dBm, + * RF24_PA_MED=-6dBM, and RF24_PA_HIGH=0dBm. + * + * @param Desired PA level. + */ + void setPALevel( rf24_pa_dbm_e level ) ; + + /** + * Fetches the current PA level. See setPALevel for + * return value definitions. + */ + rf24_pa_dbm_e getPALevel( void ) ; + /** * Set the transmission data rate * - * @param speed RF24_1MBPS for 1Mbps or RF24_2MBPS for 2Mbps + * @param speed RF24_250KBPS for 250kbs, RF24_1MBPS for 1Mbps, or RF24_2MBPS for 2Mbps */ void setDataRate(rf24_datarate_e speed); diff --git a/nRF24L01.h b/nRF24L01.h index 5f4d1b8..cc3b5bd 100644 --- a/nRF24L01.h +++ b/nRF24L01.h @@ -33,6 +33,7 @@ #define STATUS 0x07 #define OBSERVE_TX 0x08 #define CD 0x09 +#define RPD 0x09 #define RX_ADDR_P0 0x0A #define RX_ADDR_P1 0x0B #define RX_ADDR_P2 0x0C @@ -73,10 +74,17 @@ #define AW 0 #define ARD 4 #define ARC 0 +#define RF_DR_LOW 5 #define PLL_LOCK 4 -#define RF_DR 3 -#define RF_PWR 1 -#define LNA_HCURR 0 +#define RF_DR_HIGH 3 +#define RF_PWR 6 +#define RF_PWR_LOW 1 +#define RF_PWR_HIGH 2 +#define RF_PWR_0DB 6 +#define RF_PWR_6DB 4 +#define RF_PWR_12DB 2 +#define RF_PWR_18DB 0 +#define LNA_HCURR 0 #define RX_DR 6 #define TX_DS 5 #define MAX_RT 4