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
*
*}
Committer:
epgmdm
Date:
Thu Jun 11 23:19:52 2015 +0000
Revision:
2:ca0a3c0bba70
Parent:
1:ff53b1ac3bad
Child:
3:afe8d307b5c3
Ack working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
epgmdm 0:8fd0531ae0be 1 /**
epgmdm 0:8fd0531ae0be 2 *@section DESCRIPTION
epgmdm 0:8fd0531ae0be 3 * mbed NRF2401+ Library
epgmdm 0:8fd0531ae0be 4 *@section LICENSE
epgmdm 0:8fd0531ae0be 5 * Copyright (c) 2015, Malcolm McCulloch
epgmdm 0:8fd0531ae0be 6 *
epgmdm 0:8fd0531ae0be 7 * Permission is hereby granted, free of charge, to any person obtaining a copy
epgmdm 0:8fd0531ae0be 8 * of this software and associated documentation files (the "Software"), to deal
epgmdm 0:8fd0531ae0be 9 * in the Software without restriction, including without limitation the rights
epgmdm 0:8fd0531ae0be 10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
epgmdm 0:8fd0531ae0be 11 * copies of the Software, and to permit persons to whom the Software is
epgmdm 0:8fd0531ae0be 12 * furnished to do so, subject to the following conditions:
epgmdm 0:8fd0531ae0be 13 *
epgmdm 0:8fd0531ae0be 14 * The above copyright notice and this permission notice shall be included in
epgmdm 0:8fd0531ae0be 15 * all copies or substantial portions of the Software.
epgmdm 0:8fd0531ae0be 16 *
epgmdm 0:8fd0531ae0be 17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
epgmdm 0:8fd0531ae0be 18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
epgmdm 0:8fd0531ae0be 19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
epgmdm 0:8fd0531ae0be 20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
epgmdm 0:8fd0531ae0be 21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
epgmdm 0:8fd0531ae0be 22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
epgmdm 0:8fd0531ae0be 23 * THE SOFTWARE.
epgmdm 0:8fd0531ae0be 24 * @file "NRF2401P.h"
epgmdm 0:8fd0531ae0be 25 */
epgmdm 0:8fd0531ae0be 26 class NRF2401P
epgmdm 0:8fd0531ae0be 27 {
epgmdm 0:8fd0531ae0be 28 public:
epgmdm 0:8fd0531ae0be 29 SPI *spi;
epgmdm 0:8fd0531ae0be 30 DigitalOut csn,ce;
epgmdm 0:8fd0531ae0be 31 char addressWidth;
epgmdm 0:8fd0531ae0be 32 char logMsg[80];
epgmdm 0:8fd0531ae0be 33 char status;
epgmdm 0:8fd0531ae0be 34 char statusS[32];
epgmdm 2:ca0a3c0bba70 35 char pipe0Add[5];
epgmdm 2:ca0a3c0bba70 36 char txAdd[5];
epgmdm 0:8fd0531ae0be 37 bool dynamic,debug;
epgmdm 0:8fd0531ae0be 38 Serial *pc;
epgmdm 0:8fd0531ae0be 39
epgmdm 0:8fd0531ae0be 40 NRF2401P (PinName mosi, PinName miso, PinName sclk, PinName _csn, PinName _ce);
epgmdm 0:8fd0531ae0be 41
epgmdm 0:8fd0531ae0be 42 char acknowledgeData( char *data, char width, char pipe);
epgmdm 1:ff53b1ac3bad 43 char checkStatus();
epgmdm 0:8fd0531ae0be 44 bool clearStatus();
epgmdm 0:8fd0531ae0be 45 bool flushRx();
epgmdm 0:8fd0531ae0be 46 bool flushTx();
epgmdm 0:8fd0531ae0be 47 char getRxData(char * buffer);
epgmdm 0:8fd0531ae0be 48 char getRxWidth();
epgmdm 1:ff53b1ac3bad 49 bool isAckData();
epgmdm 0:8fd0531ae0be 50 bool isRxData();
epgmdm 0:8fd0531ae0be 51 void log (char *msg);
epgmdm 0:8fd0531ae0be 52 void quickRxSetup(int channel,long long addr);
epgmdm 0:8fd0531ae0be 53 void quickTxSetup(int channel,long long addr);
epgmdm 0:8fd0531ae0be 54 char readReg(char address, char *data);
epgmdm 0:8fd0531ae0be 55 char testReceive();
epgmdm 0:8fd0531ae0be 56 char testTransmit();
epgmdm 0:8fd0531ae0be 57 char transmitData( char *data, char width );
epgmdm 0:8fd0531ae0be 58
epgmdm 0:8fd0531ae0be 59 bool setAddressWidth(char width);
epgmdm 0:8fd0531ae0be 60 char setChannel(char chan);
epgmdm 2:ca0a3c0bba70 61 void setDynamicPayload();
epgmdm 0:8fd0531ae0be 62 bool setPwrUp();
epgmdm 0:8fd0531ae0be 63 char setRadio(char speed,char power);
epgmdm 0:8fd0531ae0be 64 char setRxAddress(char *address, char pipe);
epgmdm 0:8fd0531ae0be 65 char setRxAddress(long long address, char pipe);
epgmdm 0:8fd0531ae0be 66 bool setRxMode();
epgmdm 0:8fd0531ae0be 67 char setTxAddress(char *address);
epgmdm 0:8fd0531ae0be 68 char setTxAddress(long long address);
epgmdm 0:8fd0531ae0be 69 bool setTxMode();
epgmdm 2:ca0a3c0bba70 70 void setTxRetry(char delay, char numTries);
epgmdm 2:ca0a3c0bba70 71 void start();
epgmdm 0:8fd0531ae0be 72 char * statusString();
epgmdm 0:8fd0531ae0be 73 char writeReg(char address, char *data, char width);
epgmdm 0:8fd0531ae0be 74 char writeReg(char address, char data);
epgmdm 0:8fd0531ae0be 75
epgmdm 0:8fd0531ae0be 76 void scratch();
epgmdm 0:8fd0531ae0be 77
epgmdm 0:8fd0531ae0be 78
epgmdm 0:8fd0531ae0be 79 };