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 * *}
Diff: NRF2401P.cpp
- Revision:
- 6:77ead8abdd1c
- Parent:
- 4:e55807cf840b
- Child:
- 7:621a5b0cf1aa
diff -r 7e253c677a1f -r 77ead8abdd1c NRF2401P.cpp --- a/NRF2401P.cpp Fri Jun 12 09:55:02 2015 +0000 +++ b/NRF2401P.cpp Sun Jul 05 21:45:51 2015 +0000 @@ -27,33 +27,33 @@ #include "NRF2401P.h" #include "nRF24l01.h" - NRF2401P::NRF2401P ( PinName mosi, PinName miso, PinName sclk, PinName _csn, PinName _ce ) : csn( DigitalOut( _csn ) ), ce( DigitalOut( _ce ) ) { addressWidth = 5; pc = new Serial( USBTX, USBRX ); // tx, rx - sprintf(logMsg, "Initialise " ); - log(logMsg); + if (debug) { + sprintf(logMsg, "Initialise" ); + log(logMsg); + } spi = new SPI( mosi, miso, sclk, NC ); //SPI (PinName mosi, PinName miso, PinName sclk, PinName _unused=NC) spi->frequency( 10000000 ); // 1MHZ max 10 MHz spi->format( 8, 0 ); // 0: 0e 08; 1: 0e 00; 2:0e 00 ;3:1c 00 csn = 1; ce = 0; dynamic = false; - debug=false; - -}; + debug = false; +} /** * writes to pc and waits */ -void NRF2401P::log (char *msg) +void NRF2401P::log(char *msg) { if(debug) { printf("\t <%s \t %s>\n\r",statusString(), msg); wait(0.01); - }; + } } void NRF2401P::scratch() @@ -70,12 +70,11 @@ sprintf(logMsg, " register %02x (%02x) = %02x", i, status, register1 ); log(logMsg); } +} -} /** * start here to configure the basics of the NRF */ - void NRF2401P::start() { writeReg( CONFIG, 0x0c ); // set 16 bit crc @@ -88,8 +87,8 @@ flushTx(); setPwrUp(); setTxMode(); // just make sure no spurious reads.... +} -} /** * Sets up a reciever using shockburst and dynamic payload. Uses pipe 1 * defaults to 5 bytes @@ -101,12 +100,12 @@ setRxAddress(addrRx,1); setRxMode(); ce=1; - wait (0.001f); + wait(0.001f); } + /** * Sets up for receive of a message to address 0XA0A0A0 */ - char NRF2401P::testReceive() { char message[64]; @@ -124,20 +123,25 @@ message[width]='\0'; sprintf(logMsg,"Received= [%s]",message); log(logMsg); - }; + } } /** * Sets the number and the timimg of TX retries */ - -void NRF2401P::setTxRetry(char delay, char numTries) +char 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; + } } - /** * Sets up a transmitter using shockburst and dynamic payload. Uses pipe 1 * defaults to 5 bytes @@ -155,7 +159,6 @@ /** * Sets up for transmit of a message to address 0XA0A0A0 */ - char NRF2401P::testTransmit() { long long addr=0xA0B0C0; @@ -168,8 +171,8 @@ transmitData(data,18); wait (1.0); } +} -} /** *Speed : ‘0x00’ – 1Mbps @@ -181,10 +184,11 @@ '0x01' – -12dBm '0x02' – -6dBm '0x03' – 0dBm +* Return: 0 for success, non-zero for fail */ -char NRF2401P::setRadio(char speed,char power) +char NRF2401P::setRadio(char speed, char power) { - char val=0; + char val=0, chk=0; sprintf(logMsg, "Set radio"); log(logMsg); if (speed & 0x02) { @@ -192,10 +196,20 @@ } val |= (speed & 0x01)<<3; - val |= ((power &0x03)<<1); - writeReg (0x06,val); + val |= ((power & 0x03)<<1); + printf("\n\r"); + + writeReg (0x06, val); + // read register to verify settings + readReg( 0x06, &chk ); + if (chk&0x2E == val) { + return 0; + } else { + return 1; + } } + /** Set RF_CH = chan; @@ -203,18 +217,31 @@ */ char NRF2401P::setChannel(char chan) { - sprintf(logMsg, "Set channel"); - log(logMsg); + char chk=0; + if (debug) { + sprintf(logMsg, "Set channel"); + log(logMsg); + } writeReg (0x05,(chan&0x7f)); + readReg(0x05,&chk); + if (chk&0x7f == chan&0x7f) { + return 0; + } else { + return 1; + } } + /** * Transmits width bytes of data. width <32 -* returns 1 if succeful */ -bool NRF2401P::transmitData( char *data, char width ) +char NRF2401P::transmitData( char *data, char width ) { - if (width>32) return 0; - + if (width>32) + return 0; + checkStatus(); + if ((status>>4)&1) { // Max retries - flush tx + flushTx(); + } //clearStatus(); //ce = 1; csn = 0; @@ -227,16 +254,11 @@ } csn = 1; wait(0.001); - checkStatus(); - if ((status>>4)&1) { // Max retries - flush tx - flushTx(); - } if (debug) { - sprintf(logMsg, " Transmit data %d bytes to %02x (%02x) = %10s", width, address, status, *data ); + sprintf(logMsg, " Transmit data %d bytes to %02x (%02x) = %10s", width, address, status, data ); log(logMsg); } return status; - } /** @@ -261,12 +283,12 @@ log(logMsg); } return status; +} -} /** * Writes 1 byte data to a register **/ -char NRF2401P::writeReg( char address, char data ) +void NRF2401P::writeReg( char address, char data ) { char status = 0; char reg; @@ -276,14 +298,16 @@ status = spi->write( reg ); spi->write( data ); csn = 1; - sprintf(logMsg, " register write %02x (%02x) = %02x", address, status, data ); - log(logMsg); - return status; + if (debug) { + sprintf(logMsg, " register write %02x (%02x) = %02x", address, status, data ); + log(logMsg); + } } + /** * Writes width bytes data to a register, ls byte to ms byte /for adressess **/ -char NRF2401P::writeReg( char address, char *data, char width ) +void NRF2401P::writeReg( char address, char *data, char width ) { char reg; csn = 0; @@ -300,37 +324,40 @@ sprintf(logMsg, " register write %d bytes to %02x (%02x) = %02x %02x %02x", width, address, status, data[0], data[1], data[2] ); log(logMsg); } - return status; } + /** * Reads 1 byte from a register **/ -char NRF2401P::readReg( char address, char *data ) +void NRF2401P::readReg( char address, char *data ) { csn = 0; address &= 0x1F; status = spi->write( address ); *data = spi->write( 0x00 ); csn = 1; - // sprintf(logMsg, " register read %02x (%02x) = %02x", address, status, *data ); - // log(logMsg); - return status; + if (debug && address != 0x07) { // In debug mode: print out anything other than a status request + sprintf(logMsg, " register read %02x (%02x) = %02x", address, status, *data ); + log(logMsg); + } } + /** * Clears the status flags RX_DR, TX_DS, MAX_RT */ -bool NRF2401P::clearStatus() +void NRF2401P::clearStatus() { - status = writeReg(STATUS,0x70); + writeReg(STATUS,0x70); if (debug) { sprintf(logMsg, "Clear status (%02x)", status ); log(logMsg); } } + /** * flushes TX FIFO and resets status flags */ -bool NRF2401P::flushTx() +void NRF2401P::flushTx() { csn = 0; status = spi->write( FLUSH_TX ); @@ -340,13 +367,12 @@ sprintf(logMsg, "Flush TX FIFO (%02x)", status ); log(logMsg); } - return; } /** * flushes RX FIFO and resets status flags */ -bool NRF2401P::flushRx() +void NRF2401P::flushRx() { csn = 0; status = spi->write( FLUSH_RX ); @@ -357,10 +383,11 @@ log(logMsg); } } + /** * Sets PRIM_RX = 0; */ -bool NRF2401P::setTxMode() +char NRF2401P::setTxMode() { char data; char bit; @@ -381,30 +408,41 @@ ce=1; wait(0.003); - return ( bit == 0 ); + if (bit == 0) { + return 0; + } else { + return 1; + } } /** * Sets the number of bytes of the address width = 3,4,5 */ -bool NRF2401P::setAddressWidth( char width ) +char NRF2401P::setAddressWidth( char width ) { + char chk=0; addressWidth = width; if ( ( width > 5 ) || ( width < 3 ) ) return false; width -= 2; - return writeReg( 0x03, width ); - + writeReg( 0x03, width ); + readReg( 0x03, &chk ); + if (chk&0x03 == width) { + return 0; + } else { + return 1; + } } + /** -* Sets the address, uses addess width set (either 3,4 or 5) +* Sets the address, uses address width set (either 3,4 or 5) */ char NRF2401P::setTxAddress( char *address ) { memcpy (txAdd,address, addressWidth); writeReg( 0x0A, address, addressWidth ); //Write to RX_ADDR_P0 - return writeReg( 0x10, address, addressWidth ); //Write to TX_ADDR - + writeReg( 0x10, address, addressWidth ); //Write to TX_ADDR + return 0; // must fix this } /** @@ -412,7 +450,6 @@ */ char NRF2401P::setTxAddress( long long address ) { - char buff[ 5 ]; buff[ 0 ] = address & 0xff; buff[ 1 ] = ( address >> 8 ) & 0xFF; @@ -420,7 +457,6 @@ buff[ 3 ] = ( address >> 24 ) & 0xFF; buff[ 4 ] = ( address >> 32 ) & 0xFF; return setTxAddress( buff ); - } /** @@ -441,14 +477,14 @@ switch ( pipe ) { case ( 0 ) : case ( 1 ) : { - status = writeReg( reg, address, addressWidth ); //Write to RX_ADDR_P0 or _P1 + writeReg( reg, address, addressWidth ); //Write to RX_ADDR_P0 or _P1 break; } case ( 2 ) : case ( 3 ) : case ( 4 ) : case ( 5 ) : { - status = writeReg( reg, address, 1 ); //Write to RX_ADDR_P2 ... _P5 + writeReg( reg, address, 1 ); //Write to RX_ADDR_P2 ... _P5 break; } @@ -456,8 +492,7 @@ readReg(EN_RXADDR,®); reg |= (1<<pipe); writeReg( EN_RXADDR,reg ); //Enable the pipe - return status; - + return 0; // Must fix this } /** @@ -473,6 +508,7 @@ buff[ 4 ] = ( address >> 32 ) & 0xFF; return setRxAddress( buff, pipe ); } + /** *checks the status flag */ @@ -481,6 +517,7 @@ readReg(0x07,&status); return status; } + /** * checks if Ack data available. */ @@ -501,6 +538,7 @@ bool isData = (status>>6)&0x01; return isData; } + /** * returns the width of the dynamic payload */ @@ -518,12 +556,12 @@ width=0; } } else { - status = readReg(0x12,&width); // width of p1 - + readReg(0x12,&width); // width of p1 } // width=18; return width; } + /** * return message in buffer, mem for buffer must have been allocated. * Return value is number of bytes of buffer @@ -543,18 +581,20 @@ buffer[i]=spi->write(0x00 ); } csn = 1; - sprintf(logMsg, "Receive data %d bytes", width ); - log(logMsg); + if (debug) { + sprintf(logMsg, "Receive data %d bytes", width ); + log(logMsg); + } clearStatus(); return width; - } else { - sprintf(logMsg, "Receive NO data %d bytes", width ); - log(logMsg); + if (debug) { + sprintf(logMsg, "Receive NO data %d bytes", width ); + log(logMsg); + } clearStatus(); return 0; } - } /** @@ -563,7 +603,6 @@ void NRF2401P::setDynamicPayload() { dynamic = true; - writeReg(FEATURE,0x07); // Enable Dyn payload, Payload with Ack and w_tx_noack command writeReg(EN_AA,0x3f); // EN_AA regi for P1 and P0 writeReg(DYNPD, 0x1F); @@ -571,8 +610,9 @@ /** * Sets PWR_UP = 1; +* return 0 on success */ -bool NRF2401P::setPwrUp() +char NRF2401P::setPwrUp() { char data; char bit; @@ -592,13 +632,17 @@ sprintf(logMsg, "Set PWR_UP to %x", bit); log(logMsg); } + if (bit == 1) { + return 0; + } else { + return 1; + } +} - return ( bit == 1 ); -} /** * Sets PRIM_RX = 0; */ -bool NRF2401P::setRxMode() +char NRF2401P::setRxMode() { char data; char bit; @@ -621,8 +665,13 @@ sprintf(logMsg, " set PRIM_RX to %x", bit); log(logMsg); } - return ( bit == 1 ); + if ( bit == 1 ) { + return 0; + } else { + return 1; + } } + /** * Prints status string */