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
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 * *}
NRF2401P.h@15:9998698cb041, 2015-06-18 (annotated)
- Committer:
- epgmdm
- Date:
- Thu Jun 18 20:54:25 2015 +0000
- Revision:
- 15:9998698cb041
- Parent:
- 5:7e253c677a1f
Cleaned up code. most functions now return void. Status is tracked in the class. Use checkStatus().
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:7e253c677a1f | 26 | |
epgmdm | 5:7e253c677a1f | 27 | /** \brief A library to drive the nRF24l01+ |
epgmdm | 5:7e253c677a1f | 28 | * |
epgmdm | 5:7e253c677a1f | 29 | * This library is a simple way to get the nRF24l01+ up and running. |
epgmdm | 5:7e253c677a1f | 30 | * |
epgmdm | 5:7e253c677a1f | 31 | *- Use case: For a simple transmitter |
epgmdm | 5:7e253c677a1f | 32 | *@code |
epgmdm | 5:7e253c677a1f | 33 | *int main() { |
epgmdm | 5:7e253c677a1f | 34 | * |
epgmdm | 5:7e253c677a1f | 35 | * long long addr1=0xAB00CD; // setup address - any 5 byte number - same as RX |
epgmdm | 5:7e253c677a1f | 36 | * int channel =0x12; // [0-126] setup channel, must be same as RX |
epgmdm | 5:7e253c677a1f | 37 | * bool txOK; |
epgmdm | 5:7e253c677a1f | 38 | * char msg[32]; |
epgmdm | 5:7e253c677a1f | 39 | * char ackData[32]; |
epgmdm | 5:7e253c677a1f | 40 | * char len; |
epgmdm | 5:7e253c677a1f | 41 | * |
epgmdm | 5:7e253c677a1f | 42 | * // Setup |
epgmdm | 5:7e253c677a1f | 43 | * NRF2401P nrf1(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce) |
epgmdm | 5:7e253c677a1f | 44 | * nrf1.quickTxSetup(channel, addr1); // sets nrf24l01+ as transmitter |
epgmdm | 5:7e253c677a1f | 45 | * |
epgmdm | 5:7e253c677a1f | 46 | * // transmit |
epgmdm | 5:7e253c677a1f | 47 | * strcpy (msg, "Hello"); |
epgmdm | 5:7e253c677a1f | 48 | * txOK= nrf1.transmitData(msg,strlen(msg)); |
epgmdm | 5:7e253c677a1f | 49 | * |
epgmdm | 5:7e253c677a1f | 50 | * // read ack data if available |
epgmdm | 5:7e253c677a1f | 51 | * if (nrf1.isAckData()) { |
epgmdm | 5:7e253c677a1f | 52 | * len= nrf1.getRxData(ackData); // len is number of bytes in ackData |
epgmdm | 5:7e253c677a1f | 53 | * } |
epgmdm | 5:7e253c677a1f | 54 | *} |
epgmdm | 5:7e253c677a1f | 55 | *@endcode |
epgmdm | 5:7e253c677a1f | 56 | *- Use case: For a simple receiver |
epgmdm | 5:7e253c677a1f | 57 | *@code |
epgmdm | 5:7e253c677a1f | 58 | *int main(){ |
epgmdm | 5:7e253c677a1f | 59 | * |
epgmdm | 5:7e253c677a1f | 60 | * long long addr1=0xAB00CD; // setup address - any 5 byte number - same as TX |
epgmdm | 5:7e253c677a1f | 61 | * int channel =0x12; // [0-126] setup channel, must be same as TX |
epgmdm | 5:7e253c677a1f | 62 | * bool txOK; |
epgmdm | 5:7e253c677a1f | 63 | * char msg[32]; |
epgmdm | 5:7e253c677a1f | 64 | * char ackData[32]; |
epgmdm | 5:7e253c677a1f | 65 | * char len; |
epgmdm | 5:7e253c677a1f | 66 | * |
epgmdm | 5:7e253c677a1f | 67 | * // Setup |
epgmdm | 5:7e253c677a1f | 68 | * NRF2401P nrf1(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce) |
epgmdm | 5:7e253c677a1f | 69 | * nrf1.quickRxSetup(channel, addr1); // sets nrf24l01+ as receiver, using pipe 1 |
epgmdm | 5:7e253c677a1f | 70 | * |
epgmdm | 5:7e253c677a1f | 71 | * // set ack data |
epgmdm | 5:7e253c677a1f | 72 | * sprintf(ackData,"Ack data"); |
epgmdm | 5:7e253c677a1f | 73 | * nrf1.acknowledgeData(ackData, strlen(ackData),1); // ack for pipe 1 |
epgmdm | 5:7e253c677a1f | 74 | * |
epgmdm | 5:7e253c677a1f | 75 | * // receive |
epgmdm | 5:7e253c677a1f | 76 | * while (! nrf1.isRxData()); // note this blocks until RX data |
epgmdm | 5:7e253c677a1f | 77 | * len= nrf1.getRxData(msg); // gets the message, len is length of msg |
epgmdm | 5:7e253c677a1f | 78 | * |
epgmdm | 5:7e253c677a1f | 79 | *} |
epgmdm | 5:7e253c677a1f | 80 | *@endcode |
epgmdm | 5:7e253c677a1f | 81 | */ |
epgmdm | 0:8fd0531ae0be | 82 | class NRF2401P |
epgmdm | 0:8fd0531ae0be | 83 | { |
epgmdm | 0:8fd0531ae0be | 84 | public: |
epgmdm | 0:8fd0531ae0be | 85 | SPI *spi; |
epgmdm | 0:8fd0531ae0be | 86 | DigitalOut csn,ce; |
epgmdm | 0:8fd0531ae0be | 87 | char addressWidth; |
epgmdm | 0:8fd0531ae0be | 88 | char logMsg[80]; |
epgmdm | 0:8fd0531ae0be | 89 | char status; |
epgmdm | 0:8fd0531ae0be | 90 | char statusS[32]; |
epgmdm | 2:ca0a3c0bba70 | 91 | char pipe0Add[5]; |
epgmdm | 2:ca0a3c0bba70 | 92 | char txAdd[5]; |
epgmdm | 0:8fd0531ae0be | 93 | bool dynamic,debug; |
epgmdm | 0:8fd0531ae0be | 94 | Serial *pc; |
epgmdm | 0:8fd0531ae0be | 95 | |
epgmdm | 0:8fd0531ae0be | 96 | NRF2401P (PinName mosi, PinName miso, PinName sclk, PinName _csn, PinName _ce); |
epgmdm | 0:8fd0531ae0be | 97 | |
epgmdm | 15:9998698cb041 | 98 | void acknowledgeData( char *data, char width, char pipe); |
epgmdm | 1:ff53b1ac3bad | 99 | char checkStatus(); |
epgmdm | 15:9998698cb041 | 100 | void clearStatus(); |
epgmdm | 15:9998698cb041 | 101 | void flushRx(); |
epgmdm | 15:9998698cb041 | 102 | void flushTx(); |
epgmdm | 0:8fd0531ae0be | 103 | char getRxData(char * buffer); |
epgmdm | 0:8fd0531ae0be | 104 | char getRxWidth(); |
epgmdm | 1:ff53b1ac3bad | 105 | bool isAckData(); |
epgmdm | 0:8fd0531ae0be | 106 | bool isRxData(); |
epgmdm | 0:8fd0531ae0be | 107 | void log (char *msg); |
epgmdm | 0:8fd0531ae0be | 108 | void quickRxSetup(int channel,long long addr); |
epgmdm | 0:8fd0531ae0be | 109 | void quickTxSetup(int channel,long long addr); |
epgmdm | 0:8fd0531ae0be | 110 | char readReg(char address, char *data); |
epgmdm | 15:9998698cb041 | 111 | void testReceive(); |
epgmdm | 15:9998698cb041 | 112 | void testTransmit(); |
epgmdm | 3:afe8d307b5c3 | 113 | bool transmitData( char *data, char width ); |
epgmdm | 0:8fd0531ae0be | 114 | |
epgmdm | 15:9998698cb041 | 115 | void setAddressWidth(char width); |
epgmdm | 15:9998698cb041 | 116 | void setChannel(char chan); |
epgmdm | 2:ca0a3c0bba70 | 117 | void setDynamicPayload(); |
epgmdm | 15:9998698cb041 | 118 | void setPwrUp(); |
epgmdm | 15:9998698cb041 | 119 | void setRadio(char speed,char power); |
epgmdm | 15:9998698cb041 | 120 | void setRxAddress(char *address, char pipe); |
epgmdm | 15:9998698cb041 | 121 | void setRxAddress(long long address, char pipe); |
epgmdm | 0:8fd0531ae0be | 122 | bool setRxMode(); |
epgmdm | 15:9998698cb041 | 123 | void setTxAddress(char *address); |
epgmdm | 15:9998698cb041 | 124 | void setTxAddress(long long address); |
epgmdm | 0:8fd0531ae0be | 125 | bool setTxMode(); |
epgmdm | 2:ca0a3c0bba70 | 126 | void setTxRetry(char delay, char numTries); |
epgmdm | 2:ca0a3c0bba70 | 127 | void start(); |
epgmdm | 0:8fd0531ae0be | 128 | char * statusString(); |
epgmdm | 15:9998698cb041 | 129 | void writeReg(char address, char *data, char width); |
epgmdm | 15:9998698cb041 | 130 | void writeReg(char address, char data); |
epgmdm | 0:8fd0531ae0be | 131 | |
epgmdm | 0:8fd0531ae0be | 132 | void scratch(); |
epgmdm | 0:8fd0531ae0be | 133 | |
epgmdm | 0:8fd0531ae0be | 134 | |
epgmdm | 0:8fd0531ae0be | 135 | }; |