This is a work in progress for an NRF2401P

Dependents:   NRF_receiver sender locker4 Weather_Station_Ofiicial ... more

About

This is a simple library to drive the nRF24l01+.

Hardware

This uses the commonly available breakout. The connections are shown below /media/uploads/epgmdm/nrf24l01pinout.png

Software

Use case: For a simple transmitter

tx code snipet

#include "NRF2401P.h"
int main() {
*
*  long long addr1=0xAB00CD; // setup address - any 5 byte number - same as RX
*  int channel =0x12;  // [0-126] setup channel, must be same as RX
*  bool txOK;
*  char msg[32];
*  char ackData[32];
*  char len;
*
*  // Setup 
*  NRF2401P nrf1(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce)
*  nrf1.quickTxSetup(channel, addr1); // sets nrf24l01+ as transmitter
*
*  // transmit
*  strcpy (msg, "Hello"); 
*  txOK= nrf1.transmitData(msg,strlen(msg));
*
*  // read ack data if available
*  if (nrf1.isAckData()) { 
*      len= nrf1.getRxData(ackData); // len is number of bytes in ackData
*   }
*}

Use case: For a simple receiver

rx code snipet

#include "NRF2401P.h"
*int main(){
*        
*  long long addr1=0xAB00CD; // setup address - any 5 byte number - same as TX
*  int channel =0x12;  // [0-126] setup channel, must be same as TX
*  bool txOK;
*  char msg[32];
*  char ackData[32];
*  char len;
*
*  // Setup 
*  NRF2401P nrf1(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce)
*  nrf1.quickRxSetup(channel, addr1); // sets nrf24l01+ as  receiver, using pipe 1
*
*  // set ack data
*  sprintf(ackData,"Ack data");
*  nrf1.acknowledgeData(ackData, strlen(ackData),1); // ack for pipe 1
*    
*  // receive
*  while (! nrf1.isRxData()); // note this blocks until RX data
*  len= nrf1.getRxData(msg); // gets the message, len is length of msg
*
*}
Revision:
20:e61edde5680d
Parent:
19:813161fd59a2
--- a/NRF2401P.cpp	Fri Feb 19 14:03:31 2016 +0000
+++ b/NRF2401P.cpp	Thu Feb 25 09:44:11 2016 +0000
@@ -28,7 +28,7 @@
 #include "nRF24l01.h"
 
 NRF2401P::NRF2401P ( PinName mosi, PinName miso, PinName sclk, PinName _csn, PinName _ce ) :
-        csn( DigitalOut( _csn ) ), ce( DigitalOut( _ce ) )
+    csn( DigitalOut( _csn ) ), ce( DigitalOut( _ce ) )
 {
     addressWidth = 5;
     //pc = new Serial( USBTX, USBRX ); // tx, rx
@@ -105,7 +105,7 @@
 /**
 * Sets up for receive of a message to address 0XA0A0A0
 */
-char NRF2401P::testReceive()
+void NRF2401P::testReceive()
 {
     char message[64];
     char width;
@@ -125,17 +125,10 @@
     }
 }
 
-char NRF2401P::setTxRetry(char delay, char numTries)
+void NRF2401P::setTxRetry(char delay, char numTries)
 {
     char val  = (delay&0xf)<<4 | (numTries&0xf);
-    char chk;
     writeReg(SETUP_RETR, val);
-    readReg(SETUP_RETR, &chk);
-    if (chk&0xff == val) {
-        return 0;
-    } else {
-        return 1;
-    }
 }
 
 /**
@@ -158,7 +151,7 @@
 /**
 * Sets up for transmit of a message to address 0XA0A0A0
 */
-char NRF2401P::testTransmit()
+void NRF2401P::testTransmit()
 {
     long long addr=0xA0B0C0;
     int channel = 0x12;
@@ -172,9 +165,9 @@
     }
 }
 
-char NRF2401P::setRadio(char speed, char power)
+void NRF2401P::setRadio(char speed, char power)
 {
-    char val=0, chk=0;
+    char val=0;
     if (debug) {
         sprintf(logMsg, "Set radio");
         log(logMsg);
@@ -188,17 +181,9 @@
     printf("\n\r");
 
     writeReg(RF_SETUP, val);
-
-    // read register to verify settings
-    readReg(RF_SETUP, &chk);
-    if (chk&0x2E == val) {
-        return 0;
-    } else {
-        return 1;
-    }
 }
 
-char NRF2401P::setChannel(char chan)
+void NRF2401P::setChannel(char chan)
 {
     char chk=0;
     if (debug) {
@@ -207,11 +192,7 @@
     }
     writeReg(RF_CH, (chan&0x7f));
     readReg(RF_CH, &chk);
-    if (chk&0x7f == chan&0x7f) {
-        return 0;
-    } else {
-        return 1;
-    }
+
 }
 
 bool NRF2401P::isRPDset()
@@ -237,7 +218,7 @@
     if (width>32)
         return 0;
     checkStatus();
-    if ((status>>4)&1) { // Max retries - flush tx
+    if ((status>>MAX_RT)&1) { // Max retries - flush tx
         flushTx();
     }
     //clearStatus();
@@ -262,7 +243,7 @@
 /**
 * re Transmits data
 */
-char NRF2401P::retransmitData( )
+void NRF2401P::retransmitData( )
 {
     checkStatus();
     if ((status>>4)&1) { // Max retries - flush tx
@@ -275,23 +256,21 @@
     // set up for writing
     status = spi->write( address );
     csn = 1;
-    wait(0.001);
     if (debug) {
         sprintf(logMsg, "Reransmit data" );
         log(logMsg);
     }
-    return status;
 }
 /**
 * sets acknowledge data width bytes of data. width <32
 */
-char NRF2401P::acknowledgeData( char *data, char width, char pipe )
+void NRF2401P::acknowledgeData( char *data, char width, char pipe )
 {
     ce = 1;
     csn = 0;
     //writeReg(0x1d,0x06); // enable payload with ack
     char address = W_ACK_PAYLOAD | (pipe&0x07);
-    int i;
+    char i;
     // set up for writing
     csn = 0;
     status = spi->write( address );
@@ -303,7 +282,6 @@
         sprintf(logMsg, "  acknowledge data %d bytes to %02x (%02x) = %c", width, address, status, *data );
         log(logMsg);
     }
-    return status;
 }
 
 /**
@@ -431,10 +409,9 @@
 /**
 * Sets PRIM_RX = 0;
 */
-char NRF2401P::setTxMode()
+void NRF2401P::setTxMode()
 {
     char data;
-    char bit;
     if (debug) {
         sprintf(logMsg, "Set Tx Mode");
         log(logMsg);
@@ -448,113 +425,100 @@
     writeReg(EN_RXADDR, 0x01); // enable pipe 0 for reading
     // check
     readReg(CONFIG, &data);
-    bit = ( data >> 0 ) & 1;
-
+  
     ce=1;
     wait(0.003);
-    if (bit == 0) {
-        return 0;
-    } else {
-        return 1;
-    }
 }
 
 /**
 * Sets the number of bytes of the address width = 3,4,5
 */
-char NRF2401P::setAddressWidth( char width )
+void NRF2401P::setAddressWidth( char width )
 {
     char chk=0;
     addressWidth = width;
     if ( ( width > 5 ) || ( width < 3 ) )
-        return false;
+        return ;
     width -= 2;
     writeReg(SETUP_AW, width);
     readReg(SETUP_AW, &chk);
-    if (chk&0x03 == width) {
-        return 0;
-    } else {
-        return 1;
-    }
 }
 
 /**
 * Sets the address, uses address width set (either 3,4 or 5)
 */
-char NRF2401P::setTxAddress( char *address )
+void NRF2401P::setTxAddress( char *address )
 {
     memcpy (txAdd,address, addressWidth);
     writeReg(RX_ADDR_P0, address, addressWidth);
     writeReg(TX_ADDR, address, addressWidth);
-    return 0;       // must fix this
 }
 
 /**
 * Sets the address, uses addess width set (either 3,4 or 5)
 */
-char NRF2401P::setTxAddress( long long address )
+void NRF2401P::setTxAddress( long long address )
 {
     char buff[ 5 ];
     char width = addressWidth;
 
-    for (char w=0; w<width;w++){
+    for (char w=0; w<width; w++) {
         char ww = width -w-1; // Reverse bytes
         buff[w] = ( address >> (8*ww )) &0xFF;
     }
-    return setTxAddress( buff );
+    setTxAddress( buff );
 }
 
 /**
 * Sets the address, uses address width set (either 3,4 or 5)
 * Enables pipe for receiving;
 */
-char NRF2401P::setRxAddress( char *address, char pipe )
+void NRF2401P::setRxAddress( char *address, char pipe )
 {
     if(debug) {
         log ("Set Rx Address");
     }
-    if (pipe>5) return 0xff;
+    if (pipe>5) return;
     if (pipe ==0) {
         memcpy(pipe0Add,address, addressWidth);
     }
 
     char reg = 0x0A + pipe;
     switch ( pipe ) {
-    case ( 0 ) :
+        case ( 0 ) :
         case ( 1 ) : {
-                writeReg(reg, address, addressWidth); //Write to RX_ADDR_P0 or _P1
-                break;
-            }
-    case ( 2 ) :
-                case ( 3 ) :
-                    case ( 4 ) :
-                case ( 5 ) : {
-                        writeReg(reg, address, 1); //Write to RX_ADDR_P2 ... _P5
-                        break;
-                    }
+            writeReg(reg, address, addressWidth); //Write to RX_ADDR_P0 or _P1
+            break;
+        }
+        case ( 2 ) :
+        case ( 3 ) :
+        case ( 4 ) :
+        case ( 5 ) : {
+            writeReg(reg, address, 1); //Write to RX_ADDR_P2 ... _P5
+            break;
+        }
 
     }
     readReg(EN_RXADDR, &reg);
     reg |= (1<<pipe);
     writeReg(EN_RXADDR, reg); //Enable the pipe
-    return 0;                  // Must fix this
 }
 
 /**
 * Sets the address of pipe (<=5), uses addess width set (either 3,4 or 5)
 */
-char NRF2401P::setRxAddress( long long address, char pipe )
+void NRF2401P::setRxAddress( long long address, char pipe )
 {
     char buff[ 5 ];
     char width = addressWidth;
-    if (pipe>1){
+    if (pipe>1) {
         width = 1;
     }
-    for (char w=0; w<width;w++){
+    for (char w=0; w<width; w++) {
         char ww = width -w-1; // Reverse bytes
         buff[w] = ( address >> (8*ww ) )&0xFF;
     }
-    return setRxAddress( buff, pipe );
+    setRxAddress( buff, pipe );
 }
 
 /**
@@ -658,14 +622,14 @@
 * Sets PWR_UP = 0;
 * return 0 on success
 */
-char NRF2401P::setPwrDown()
+void NRF2401P::setPwrDown()
 {
     char data;
     char bit;
     ce=1;
     readReg(CONFIG, &data);
     if (!((data>>1) &0x01)) {
-        return true; // Already powered up
+        return; // Already powered up
     };
     data &= (0xFD); // clear bit 2
     writeReg(CONFIG, data);
@@ -678,24 +642,19 @@
         sprintf(logMsg, "Set PWR_UP to %x", bit);
         log(logMsg);
     }
-    if (bit == 0) {
-        return 0;
-    } else {
-        return 1;
-    }
 }
 /**
 * Sets PWR_UP = 1;
 * return 0 on success
 */
-char NRF2401P::setPwrUp()
+void NRF2401P::setPwrUp()
 {
     char data;
     char bit;
     ce=1;
     readReg(CONFIG, &data);
     if ((data>>1) &0x01) {
-        return true; // Already powered up
+        return; // Already powered up
     };
     data |= (0x02);
     writeReg(CONFIG, data);
@@ -708,16 +667,11 @@
         sprintf(logMsg, "Set PWR_UP to %x", bit);
         log(logMsg);
     }
-    if (bit == 1) {
-        return 0;
-    } else {
-        return 1;
-    }
 }
 /**
 * Sets PRIM_RX = 0;
 */
-char NRF2401P::setRxMode()
+void NRF2401P::setRxMode()
 {
     char data;
     char bit;
@@ -740,11 +694,7 @@
         sprintf(logMsg, " set PRIM_RX to %x", bit);
         log(logMsg);
     }
-    if ( bit == 1 ) {
-        return 0;
-    } else {
-        return 1;
-    }
+
 }
 
 /**
@@ -805,7 +755,8 @@
         printf("\r\n");
     }
 }
-void NRF2401P::printDetails() {
+void NRF2401P::printDetails()
+{
     status = checkStatus();
     char w=1;
     printf("STATUS = 0x%02x RX_DR=%x TX_DS=%x MAX_RT=%x RX_P_NO=%x TX_FULL=%x\r\n", status,