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:
17:b132fc1a27d2
Parent:
16:a9b83d2b6915
Child:
18:220df99d2d41
--- a/NRF2401P.cpp	Thu Jan 28 14:37:32 2016 +0000
+++ b/NRF2401P.cpp	Sun Feb 07 22:34:47 2016 +0000
@@ -75,7 +75,7 @@
 void NRF2401P::start()
 {
     writeReg(CONFIG, 0x0c); // set 16 bit crc
-    setTxRetry(0x01, 0x0f); // 500 uS, 15 retries
+    setTxRetry(0x05, 0x0f); // 500 uS, 15 retries
     setRadio(0, 0x03); // 1MB/S  0dB
     setDynamicPayload();
     setChannel(76);  // should be clear?
@@ -472,11 +472,12 @@
 char NRF2401P::setTxAddress( long long address )
 {
     char buff[ 5 ];
-    buff[ 0 ] = address & 0xff;
-    buff[ 1 ] = ( address >> 8 ) & 0xFF;
-    buff[ 2 ] = ( address >> 16 ) & 0xFF;
-    buff[ 3 ] = ( address >> 24 ) & 0xFF;
-    buff[ 4 ] = ( address >> 32 ) & 0xFF;
+    char width = addressWidth;
+
+    for (char w=0; w<width;w++){
+        char ww = width -w-1; // Reverse bytes
+        buff[w] = ( address >> (8*ww )) &0xFF;
+    }
     return setTxAddress( buff );
 }
 
@@ -522,11 +523,14 @@
 char NRF2401P::setRxAddress( long long address, char pipe )
 {
     char buff[ 5 ];
-    buff[ 0 ] = address & 0xff;
-    buff[ 1 ] = ( address >> 8 ) & 0xFF;
-    buff[ 2 ] = ( address >> 16 ) & 0xFF;
-    buff[ 3 ] = ( address >> 24 ) & 0xFF;
-    buff[ 4 ] = ( address >> 32 ) & 0xFF;
+    char width = addressWidth;
+    if (pipe>1){
+        width = 1;
+    }
+    for (char w=0; w<width;w++){
+        char ww = width -w-1; // Reverse bytes
+        buff[w] = ( address >> (8*ww ) )&0xFF;
+    }
     return setRxAddress( buff, pipe );
 }
 
@@ -628,6 +632,36 @@
 }
 
 /**
+* Sets PWR_UP = 0;
+* return 0 on success
+*/
+char NRF2401P::setPwrDown()
+{
+    char data;
+    char bit;
+    ce=1;
+    readReg(CONFIG, &data);
+    if (!((data>>1) &0x01)) {
+        return true; // Already powered up
+    };
+    data &= (0xFD); // clear bit 2
+    writeReg(CONFIG, data);
+    // check
+    readReg(CONFIG, &data);
+    bit = ( data >> 1 ) & 1;
+
+    wait(0.005); // wait 5ms
+    if(debug) {
+        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
 */
@@ -657,7 +691,6 @@
         return 1;
     }
 }
-
 /**
 * Sets PRIM_RX = 0;
 */
@@ -734,9 +767,24 @@
         printf("\r\n");
     }
 }
-
+/*
+* Prints in reverse order for addresses
+*/
+void NRF2401P::printRegR(char* name, char address, char width, bool newline)
+{
+    char data[width];
+    readReg(address, data, width);
+    printf("%s = 0x", name);
+    for (int i=0; i<width; i++) {
+        printf("%02x", data[i]);
+    }
+    if (newline) {
+        printf("\r\n");
+    }
+}
 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,
            (status & (1<<MASK_RX_DR))?1:0,
            (status & (1<<MASK_TX_DS))?1:0,
@@ -744,13 +792,13 @@
            (status >> RX_P_NO) & 7,
            (status & (1<<TX_FULL))?1:0 );
 
-    printReg("RX_ADDR_P0", RX_ADDR_P0, addressWidth);
-    printReg("RX_ADDR_P1", RX_ADDR_P1, addressWidth);
-    printReg("RX_ADDR_P2", RX_ADDR_P2, addressWidth);
-    printReg("RX_ADDR_P3", RX_ADDR_P3, addressWidth);
-    printReg("RX_ADDR_P4", RX_ADDR_P4, addressWidth);
-    printReg("RX_ADDR_P5", RX_ADDR_P5, addressWidth);
-    printReg("TX_ADDR", TX_ADDR, addressWidth);
+    printRegR("RX_ADDR_P0", RX_ADDR_P0, addressWidth);
+    printRegR("RX_ADDR_P1", RX_ADDR_P1, addressWidth);
+    printRegR("RX_ADDR_P2", RX_ADDR_P2, w);
+    printRegR("RX_ADDR_P3", RX_ADDR_P3, w);
+    printRegR("RX_ADDR_P4", RX_ADDR_P4, w);
+    printRegR("RX_ADDR_P5", RX_ADDR_P5, w);
+    printRegR("TX_ADDR", TX_ADDR, addressWidth);
 
     printReg("RX_PW_P0", RX_PW_P0, false);      // false for no newline, save some space
     printReg("   RX_PW_P1", RX_PW_P1, false);
@@ -759,6 +807,8 @@
     printReg("   RX_PW_P4", RX_PW_P4, false);
     printReg("   RX_PW_P5", RX_PW_P5);
 
+
+    printReg("SETUP_RETR", SETUP_RETR);
     printReg("EN_AA", EN_AA);
     printReg("EN_RXADDR", EN_RXADDR);
     printReg("RF_CH", RF_CH);