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@14:976a876819ae, 2015-07-11 (annotated)
- Committer:
- nixonkj
- Date:
- Sat Jul 11 22:17:36 2015 +0000
- Revision:
- 14:976a876819ae
- Parent:
- 13:5cbc726f2bbb
- Child:
- 17:b132fc1a27d2
Minro housekeeping.
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 | */ |
nixonkj | 9:c21b80aaf250 | 26 | |
nixonkj | 9:c21b80aaf250 | 27 | #ifndef MBED_NRF2401P_H |
nixonkj | 9:c21b80aaf250 | 28 | #define MBED_NRF2401P_H |
epgmdm | 5:7e253c677a1f | 29 | |
epgmdm | 5:7e253c677a1f | 30 | /** \brief A library to drive the nRF24l01+ |
epgmdm | 5:7e253c677a1f | 31 | * |
epgmdm | 5:7e253c677a1f | 32 | * This library is a simple way to get the nRF24l01+ up and running. |
epgmdm | 5:7e253c677a1f | 33 | * |
epgmdm | 5:7e253c677a1f | 34 | *- Use case: For a simple transmitter |
epgmdm | 5:7e253c677a1f | 35 | *@code |
epgmdm | 5:7e253c677a1f | 36 | *int main() { |
epgmdm | 5:7e253c677a1f | 37 | * |
epgmdm | 5:7e253c677a1f | 38 | * long long addr1=0xAB00CD; // setup address - any 5 byte number - same as RX |
epgmdm | 5:7e253c677a1f | 39 | * int channel =0x12; // [0-126] setup channel, must be same as RX |
epgmdm | 5:7e253c677a1f | 40 | * bool txOK; |
epgmdm | 5:7e253c677a1f | 41 | * char msg[32]; |
epgmdm | 5:7e253c677a1f | 42 | * char ackData[32]; |
epgmdm | 5:7e253c677a1f | 43 | * char len; |
epgmdm | 5:7e253c677a1f | 44 | * |
epgmdm | 5:7e253c677a1f | 45 | * // Setup |
epgmdm | 5:7e253c677a1f | 46 | * NRF2401P nrf1(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce) |
epgmdm | 5:7e253c677a1f | 47 | * nrf1.quickTxSetup(channel, addr1); // sets nrf24l01+ as transmitter |
epgmdm | 5:7e253c677a1f | 48 | * |
epgmdm | 5:7e253c677a1f | 49 | * // transmit |
epgmdm | 5:7e253c677a1f | 50 | * strcpy (msg, "Hello"); |
epgmdm | 5:7e253c677a1f | 51 | * txOK= nrf1.transmitData(msg,strlen(msg)); |
epgmdm | 5:7e253c677a1f | 52 | * |
epgmdm | 5:7e253c677a1f | 53 | * // read ack data if available |
epgmdm | 5:7e253c677a1f | 54 | * if (nrf1.isAckData()) { |
epgmdm | 5:7e253c677a1f | 55 | * len= nrf1.getRxData(ackData); // len is number of bytes in ackData |
epgmdm | 5:7e253c677a1f | 56 | * } |
epgmdm | 5:7e253c677a1f | 57 | *} |
epgmdm | 5:7e253c677a1f | 58 | *@endcode |
epgmdm | 5:7e253c677a1f | 59 | *- Use case: For a simple receiver |
epgmdm | 5:7e253c677a1f | 60 | *@code |
epgmdm | 5:7e253c677a1f | 61 | *int main(){ |
epgmdm | 5:7e253c677a1f | 62 | * |
epgmdm | 5:7e253c677a1f | 63 | * long long addr1=0xAB00CD; // setup address - any 5 byte number - same as TX |
epgmdm | 5:7e253c677a1f | 64 | * int channel =0x12; // [0-126] setup channel, must be same as TX |
epgmdm | 5:7e253c677a1f | 65 | * bool txOK; |
epgmdm | 5:7e253c677a1f | 66 | * char msg[32]; |
epgmdm | 5:7e253c677a1f | 67 | * char ackData[32]; |
epgmdm | 5:7e253c677a1f | 68 | * char len; |
epgmdm | 5:7e253c677a1f | 69 | * |
epgmdm | 5:7e253c677a1f | 70 | * // Setup |
epgmdm | 5:7e253c677a1f | 71 | * NRF2401P nrf1(PTD6,PTD7, PTD5,PTD4, PTC12); //mosi, miso, sclk, csn, ce) |
epgmdm | 5:7e253c677a1f | 72 | * nrf1.quickRxSetup(channel, addr1); // sets nrf24l01+ as receiver, using pipe 1 |
epgmdm | 5:7e253c677a1f | 73 | * |
epgmdm | 5:7e253c677a1f | 74 | * // set ack data |
epgmdm | 5:7e253c677a1f | 75 | * sprintf(ackData,"Ack data"); |
epgmdm | 5:7e253c677a1f | 76 | * nrf1.acknowledgeData(ackData, strlen(ackData),1); // ack for pipe 1 |
epgmdm | 5:7e253c677a1f | 77 | * |
epgmdm | 5:7e253c677a1f | 78 | * // receive |
epgmdm | 5:7e253c677a1f | 79 | * while (! nrf1.isRxData()); // note this blocks until RX data |
epgmdm | 5:7e253c677a1f | 80 | * len= nrf1.getRxData(msg); // gets the message, len is length of msg |
epgmdm | 5:7e253c677a1f | 81 | * |
epgmdm | 5:7e253c677a1f | 82 | *} |
epgmdm | 5:7e253c677a1f | 83 | *@endcode |
epgmdm | 5:7e253c677a1f | 84 | */ |
nixonkj | 9:c21b80aaf250 | 85 | |
epgmdm | 0:8fd0531ae0be | 86 | class NRF2401P |
epgmdm | 0:8fd0531ae0be | 87 | { |
epgmdm | 0:8fd0531ae0be | 88 | public: |
epgmdm | 0:8fd0531ae0be | 89 | SPI *spi; |
epgmdm | 0:8fd0531ae0be | 90 | DigitalOut csn,ce; |
epgmdm | 0:8fd0531ae0be | 91 | char addressWidth; |
epgmdm | 0:8fd0531ae0be | 92 | char logMsg[80]; |
epgmdm | 0:8fd0531ae0be | 93 | char status; |
epgmdm | 0:8fd0531ae0be | 94 | char statusS[32]; |
epgmdm | 2:ca0a3c0bba70 | 95 | char pipe0Add[5]; |
epgmdm | 2:ca0a3c0bba70 | 96 | char txAdd[5]; |
epgmdm | 0:8fd0531ae0be | 97 | bool dynamic,debug; |
epgmdm | 0:8fd0531ae0be | 98 | Serial *pc; |
epgmdm | 0:8fd0531ae0be | 99 | |
nixonkj | 9:c21b80aaf250 | 100 | /** Create a radio device for communicating with the RF24L01P via SPI. |
nixonkj | 9:c21b80aaf250 | 101 | * |
nixonkj | 9:c21b80aaf250 | 102 | * @param mosi The SPI slave data input pin. |
nixonkj | 9:c21b80aaf250 | 103 | * @param miso The SPI Slave Data Output (with tri-state option). |
nixonkj | 9:c21b80aaf250 | 104 | * @param sclk The SPI clock pin. |
nixonkj | 9:c21b80aaf250 | 105 | * @param _csn The SPI chip select pin. |
nixonkj | 9:c21b80aaf250 | 106 | * @param _ce Chip Enable pin (Activates RX or TX mode). |
nixonkj | 9:c21b80aaf250 | 107 | */ |
epgmdm | 0:8fd0531ae0be | 108 | NRF2401P (PinName mosi, PinName miso, PinName sclk, PinName _csn, PinName _ce); |
epgmdm | 0:8fd0531ae0be | 109 | |
nixonkj | 8:3e027705ce23 | 110 | char acknowledgeData(char *data, char width, char pipe); |
epgmdm | 1:ff53b1ac3bad | 111 | char checkStatus(); |
nixonkj | 6:77ead8abdd1c | 112 | void clearStatus(); |
nixonkj | 6:77ead8abdd1c | 113 | void flushRx(); |
nixonkj | 6:77ead8abdd1c | 114 | void flushTx(); |
epgmdm | 0:8fd0531ae0be | 115 | char getRxData(char * buffer); |
nixonkj | 14:976a876819ae | 116 | |
nixonkj | 14:976a876819ae | 117 | /** Get the payload width for the next FIFO item (can be dynamic or static payload width) |
nixonkj | 14:976a876819ae | 118 | * |
nixonkj | 14:976a876819ae | 119 | * @returns |
nixonkj | 14:976a876819ae | 120 | * width of next available payload. |
nixonkj | 14:976a876819ae | 121 | */ |
epgmdm | 0:8fd0531ae0be | 122 | char getRxWidth(); |
nixonkj | 14:976a876819ae | 123 | |
epgmdm | 1:ff53b1ac3bad | 124 | bool isAckData(); |
epgmdm | 0:8fd0531ae0be | 125 | bool isRxData(); |
nixonkj | 8:3e027705ce23 | 126 | |
nixonkj | 13:5cbc726f2bbb | 127 | /** Is the Received Power Detector (ala carrier detect) register set high? |
nixonkj | 13:5cbc726f2bbb | 128 | * |
nixonkj | 13:5cbc726f2bbb | 129 | * @returns |
nixonkj | 13:5cbc726f2bbb | 130 | * true on received power levels above -64 dBm present in the RF channel. |
nixonkj | 13:5cbc726f2bbb | 131 | * false received power is less than -64 dBm (could indicate interference, etc). |
nixonkj | 13:5cbc726f2bbb | 132 | */ |
nixonkj | 13:5cbc726f2bbb | 133 | bool isRPDset(); |
nixonkj | 13:5cbc726f2bbb | 134 | |
nixonkj | 14:976a876819ae | 135 | |
nixonkj | 8:3e027705ce23 | 136 | /** Writes message and waits for 10 ms ( |
nixonkj | 8:3e027705ce23 | 137 | * |
nixonkj | 8:3e027705ce23 | 138 | * @param msg |
nixonkj | 8:3e027705ce23 | 139 | */ |
epgmdm | 0:8fd0531ae0be | 140 | void log (char *msg); |
nixonkj | 8:3e027705ce23 | 141 | |
epgmdm | 0:8fd0531ae0be | 142 | void quickRxSetup(int channel,long long addr); |
epgmdm | 0:8fd0531ae0be | 143 | void quickTxSetup(int channel,long long addr); |
nixonkj | 6:77ead8abdd1c | 144 | void readReg(char address, char *data); |
nixonkj | 10:8a217441c38e | 145 | void readReg(char address, char *data, char width); |
epgmdm | 0:8fd0531ae0be | 146 | char testReceive(); |
epgmdm | 0:8fd0531ae0be | 147 | char testTransmit(); |
nixonkj | 6:77ead8abdd1c | 148 | char transmitData( char *data, char width ); |
epgmdm | 0:8fd0531ae0be | 149 | |
nixonkj | 6:77ead8abdd1c | 150 | char setAddressWidth(char width); |
nixonkj | 8:3e027705ce23 | 151 | |
nixonkj | 8:3e027705ce23 | 152 | /** Sets the frequency channel nRF24L01+ operates on |
nixonkj | 8:3e027705ce23 | 153 | * |
nixonkj | 8:3e027705ce23 | 154 | * @param chan F0 = 2400 + chan [MHz] |
nixonkj | 8:3e027705ce23 | 155 | * @returns |
nixonkj | 8:3e027705ce23 | 156 | * 0 on success, |
nixonkj | 8:3e027705ce23 | 157 | * non-zero on failure. |
nixonkj | 8:3e027705ce23 | 158 | */ |
epgmdm | 0:8fd0531ae0be | 159 | char setChannel(char chan); |
nixonkj | 8:3e027705ce23 | 160 | |
epgmdm | 2:ca0a3c0bba70 | 161 | void setDynamicPayload(); |
nixonkj | 6:77ead8abdd1c | 162 | char setPwrUp(); |
nixonkj | 7:621a5b0cf1aa | 163 | |
nixonkj | 7:621a5b0cf1aa | 164 | /** Set RF data rate and RF output power in TX mode |
nixonkj | 7:621a5b0cf1aa | 165 | * |
nixonkj | 7:621a5b0cf1aa | 166 | * @param speed RF data rate: '0x00' – 1Mbps, '0x01' – 2Mbps, '0x02' – 250kbps, '0x03' – Reserved |
nixonkj | 7:621a5b0cf1aa | 167 | * @param power RF output power: '0x00' – -18dBm, '0x01' – -12dBm, '0x02' – -6dBm, '0x03' – 0dBm |
nixonkj | 7:621a5b0cf1aa | 168 | * @returns |
nixonkj | 7:621a5b0cf1aa | 169 | * 0 on success, |
nixonkj | 7:621a5b0cf1aa | 170 | * non-zero on failure. |
nixonkj | 7:621a5b0cf1aa | 171 | */ |
nixonkj | 7:621a5b0cf1aa | 172 | char setRadio(char speed, char power); |
nixonkj | 7:621a5b0cf1aa | 173 | |
epgmdm | 0:8fd0531ae0be | 174 | char setRxAddress(char *address, char pipe); |
epgmdm | 0:8fd0531ae0be | 175 | char setRxAddress(long long address, char pipe); |
nixonkj | 6:77ead8abdd1c | 176 | char setRxMode(); |
epgmdm | 0:8fd0531ae0be | 177 | char setTxAddress(char *address); |
epgmdm | 0:8fd0531ae0be | 178 | char setTxAddress(long long address); |
nixonkj | 6:77ead8abdd1c | 179 | char setTxMode(); |
nixonkj | 8:3e027705ce23 | 180 | |
nixonkj | 8:3e027705ce23 | 181 | /** Sets the timing and number of TX retries |
nixonkj | 8:3e027705ce23 | 182 | * |
nixonkj | 8:3e027705ce23 | 183 | * @param delay Auto Retransmit Delay: '0000' – Wait 250uS', '0001' – Wait 500uS, ..., '1111' – Wait 4000uS |
nixonkj | 8:3e027705ce23 | 184 | * @param numTries Auto Retransmit Count: '0000' – Re-Transmit disabled, '0001' – Up to 1 Re-Transmit on fail of AA, ..., '1111' – Up to 15 Re-Transmit on fail of AA |
nixonkj | 8:3e027705ce23 | 185 | * @returns |
nixonkj | 8:3e027705ce23 | 186 | * 0 on success, |
nixonkj | 8:3e027705ce23 | 187 | * non-zero on failure. |
nixonkj | 8:3e027705ce23 | 188 | */ |
nixonkj | 6:77ead8abdd1c | 189 | char setTxRetry(char delay, char numTries); |
nixonkj | 8:3e027705ce23 | 190 | |
epgmdm | 2:ca0a3c0bba70 | 191 | void start(); |
nixonkj | 8:3e027705ce23 | 192 | char* statusString(); |
nixonkj | 11:07f76589f00a | 193 | |
nixonkj | 11:07f76589f00a | 194 | /** Print out 1 byte content of register |
nixonkj | 11:07f76589f00a | 195 | * |
nixonkj | 11:07f76589f00a | 196 | * @param name Name of register to print. |
nixonkj | 11:07f76589f00a | 197 | * @param address Register address of interest. |
nixonkj | 11:07f76589f00a | 198 | * @param newline By default print a trailing newline, can disable by setting false. |
nixonkj | 11:07f76589f00a | 199 | */ |
nixonkj | 11:07f76589f00a | 200 | void printReg(char* name, char address, bool newline=true); |
nixonkj | 9:c21b80aaf250 | 201 | |
nixonkj | 11:07f76589f00a | 202 | /** Print out 'width' bytes content of register |
nixonkj | 11:07f76589f00a | 203 | * |
nixonkj | 11:07f76589f00a | 204 | * @param name Name of register to print. |
nixonkj | 11:07f76589f00a | 205 | * @param address Register address of interest. |
nixonkj | 11:07f76589f00a | 206 | * @param width Number of bytes to print. |
nixonkj | 11:07f76589f00a | 207 | * @param newline By default print a trailing newline, can disable by setting false. |
nixonkj | 11:07f76589f00a | 208 | */ |
nixonkj | 11:07f76589f00a | 209 | void printReg(char* name, char address, char width, bool newline=true); |
nixonkj | 11:07f76589f00a | 210 | |
nixonkj | 11:07f76589f00a | 211 | /** Print verbose details about the radio setup and configuration |
nixonkj | 9:c21b80aaf250 | 212 | * |
nixonkj | 9:c21b80aaf250 | 213 | */ |
nixonkj | 9:c21b80aaf250 | 214 | void printDetails(); |
nixonkj | 9:c21b80aaf250 | 215 | |
nixonkj | 6:77ead8abdd1c | 216 | void writeReg(char address, char *data, char width); |
nixonkj | 6:77ead8abdd1c | 217 | void writeReg(char address, char data); |
epgmdm | 0:8fd0531ae0be | 218 | |
epgmdm | 0:8fd0531ae0be | 219 | void scratch(); |
epgmdm | 0:8fd0531ae0be | 220 | |
epgmdm | 0:8fd0531ae0be | 221 | |
nixonkj | 9:c21b80aaf250 | 222 | }; |
nixonkj | 9:c21b80aaf250 | 223 | #endif |