Christiaan M / nRF24L01P

Fork of nRF24L01P by Owen Edwards

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers nRF24L01P.h Source File

nRF24L01P.h

Go to the documentation of this file.
00001 /**
00002  * @file nRF24L01P.h
00003  *
00004  * @author Owen Edwards
00005  *
00006  * @section LICENSE
00007  *
00008  * Copyright (c) 2010 Owen Edwards
00009  *
00010  *    This program is free software: you can redistribute it and/or modify
00011  *    it under the terms of the GNU General Public License as published by
00012  *    the Free Software Foundation, either version 3 of the License, or
00013  *    (at your option) any later version.
00014  *
00015  *    This program is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *    GNU General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU General Public License
00021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
00022  *
00023  * The above copyright notice and this permission notice shall be included in
00024  * all copies or substantial portions of the Software.
00025  *
00026  * @section DESCRIPTION
00027  *
00028  * nRF24L01+ Single Chip 2.4GHz Transceiver from Nordic Semiconductor.
00029  *
00030  * Datasheet:
00031  *
00032  * http://www.nordicsemi.no/files/Product/data_sheet/nRF24L01P_Product_Specification_1_0.pdf
00033  */
00034 
00035 #ifndef __NRF24L01P_H__
00036 #define __NRF24L01P_H__
00037 
00038 /**
00039  * Includes
00040  */
00041 #include "mbed.h"
00042 
00043 /**
00044  * Defines
00045  */
00046 #define NRF24L01P_TX_PWR_ZERO_DB         0
00047 #define NRF24L01P_TX_PWR_MINUS_6_DB     -6
00048 #define NRF24L01P_TX_PWR_MINUS_12_DB   -12
00049 #define NRF24L01P_TX_PWR_MINUS_18_DB   -18
00050 
00051 #define NRF24L01P_DATARATE_250_KBPS    250
00052 #define NRF24L01P_DATARATE_1_MBPS     1000
00053 #define NRF24L01P_DATARATE_2_MBPS     2000
00054 
00055 #define NRF24L01P_CRC_NONE               0
00056 #define NRF24L01P_CRC_8_BIT              8
00057 #define NRF24L01P_CRC_16_BIT            16
00058 
00059 #define NRF24L01P_MIN_RF_FREQUENCY    2400
00060 #define NRF24L01P_MAX_RF_FREQUENCY    2525
00061 
00062 #define NRF24L01P_PIPE_P0                0
00063 #define NRF24L01P_PIPE_P1                1
00064 #define NRF24L01P_PIPE_P2                2
00065 #define NRF24L01P_PIPE_P3                3
00066 #define NRF24L01P_PIPE_P4                4
00067 #define NRF24L01P_PIPE_P5                5
00068 
00069 /**
00070 * Default setup for the nRF24L01+, based on the Sparkfun "Nordic Serial Interface Board"
00071 *  for evaluation (http://www.sparkfun.com/products/9019)
00072 */
00073 #define DEFAULT_NRF24L01P_ADDRESS       ((unsigned long long) 0xE7E7E7E7E7 )
00074 #define DEFAULT_NRF24L01P_ADDRESS_WIDTH  5
00075 #define DEFAULT_NRF24L01P_CRC            NRF24L01P_CRC_8_BIT
00076 #define DEFAULT_NRF24L01P_RF_FREQUENCY  (NRF24L01P_MIN_RF_FREQUENCY + 2)
00077 #define DEFAULT_NRF24L01P_DATARATE       NRF24L01P_DATARATE_1_MBPS
00078 #define DEFAULT_NRF24L01P_TX_PWR         NRF24L01P_TX_PWR_ZERO_DB
00079 #define DEFAULT_NRF24L01P_TRANSFER_SIZE  4
00080 
00081 /**
00082  * nRF24L01+ Single Chip 2.4GHz Transceiver from Nordic Semiconductor.
00083  */
00084 class nRF24L01P
00085 {
00086 
00087 public:
00088 
00089     /**
00090      * Constructor.
00091      *
00092      * @param mosi mbed pin to use for MOSI line of SPI interface.
00093      * @param miso mbed pin to use for MISO line of SPI interface.
00094      * @param sck mbed pin to use for SCK line of SPI interface.
00095      * @param csn mbed pin to use for not chip select line of SPI interface.
00096      * @param ce mbed pin to use for the chip enable line.
00097      * @param irq mbed pin to use for the interrupt request line.
00098      */
00099     nRF24L01P(PinName mosi, PinName miso, PinName sck, PinName csn, PinName ce, PinName irq = NC);
00100 
00101     /**
00102      * Set the RF frequency.
00103      *
00104      * @param frequency the frequency of RF transmission in MHz (2400..2525).
00105      */
00106     void setRfFrequency(int frequency = DEFAULT_NRF24L01P_RF_FREQUENCY);
00107 
00108     /**
00109      * Get the RF frequency.
00110      *
00111      * @return the frequency of RF transmission in MHz (2400..2525).
00112      */
00113     int getRfFrequency(void);
00114 
00115     /**
00116      * Set the RF output power.
00117      *
00118      * @param power the RF output power in dBm (0, -6, -12 or -18).
00119      */
00120     void setRfOutputPower(int power = DEFAULT_NRF24L01P_TX_PWR);
00121 
00122     /**
00123      * Get the RF output power.
00124      *
00125      * @return the RF output power in dBm (0, -6, -12 or -18).
00126      */
00127     int getRfOutputPower(void);
00128 
00129     /**
00130      * Set the Air data rate.
00131      *
00132      * @param rate the air data rate in kbps (250, 1M or 2M).
00133      */
00134     void setAirDataRate(int rate = DEFAULT_NRF24L01P_DATARATE);
00135 
00136     /**
00137      * Get the Air data rate.
00138      *
00139      * @return the air data rate in kbps (250, 1M or 2M).
00140      */
00141     int getAirDataRate(void);
00142 
00143     /**
00144      * Set the CRC width.
00145      *
00146      * @param width the number of bits for the CRC (0, 8 or 16).
00147      */
00148     void setCrcWidth(int width = DEFAULT_NRF24L01P_CRC);
00149 
00150     /**
00151      * Get the CRC width.
00152      *
00153      * @return the number of bits for the CRC (0, 8 or 16).
00154      */
00155     int getCrcWidth(void);
00156 
00157     /**
00158      * Set the Receive address.
00159      *
00160      * @param address address associated with the particular pipe
00161      * @param width width of the address in bytes (3..5)
00162      * @param pipe pipe to associate the address with (0..5, default 0)
00163      *
00164      * Note that Pipes 0 & 1 have 3, 4 or 5 byte addresses,
00165      *  while Pipes 2..5 only use the lowest byte (bits 7..0) of the
00166      *  address provided here, and use 2, 3 or 4 bytes from Pipe 1's address.
00167      *  The width parameter is ignored for Pipes 2..5.
00168      */
00169     void setRxAddress(unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, int pipe = NRF24L01P_PIPE_P0);
00170 
00171     void setRxAddress(unsigned long msb_address, unsigned long lsb_address, int width, int pipe = NRF24L01P_PIPE_P0);
00172 
00173     /**
00174      * Set the Transmit address.
00175      *
00176      * @param address address for transmission
00177      * @param width width of the address in bytes (3..5)
00178      *
00179      * Note that the address width is shared with the Receive pipes,
00180      *  so a change to that address width affect transmissions.
00181      */
00182     void setTxAddress(unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH);
00183 
00184     void setTxAddress(unsigned long msb_address, unsigned long lsb_address, int width);
00185 
00186     /**
00187      * Get the Receive address.
00188      *
00189      * @param pipe pipe to get the address from (0..5, default 0)
00190      * @return the address associated with the particular pipe
00191      */
00192     unsigned long long getRxAddress(int pipe = NRF24L01P_PIPE_P0);
00193 
00194     /**
00195      * Get the Transmit address.
00196      *
00197      * @return address address for transmission
00198      */
00199     unsigned long long getTxAddress(void);
00200 
00201     /**
00202      * Set the transfer size.
00203      *
00204      * @param size the size of the transfer, in bytes (1..32)
00205      * @param pipe pipe for the transfer (0..5, default 0)
00206      */
00207     void setTransferSize(int size = DEFAULT_NRF24L01P_TRANSFER_SIZE, int pipe = NRF24L01P_PIPE_P0);
00208 
00209     /**
00210      * Get the transfer size.
00211      *
00212      * @return the size of the transfer, in bytes (1..32).
00213      */
00214     int getTransferSize(int pipe = NRF24L01P_PIPE_P0);
00215 
00216 
00217     /**
00218      * Get the RPD (Received Power Detector) state.
00219      *
00220      * @return true if the received power exceeded -64dBm
00221      */
00222     bool getRPD(void);
00223 
00224     /**
00225      * Put the nRF24L01+ into Receive mode
00226      */
00227     void setReceiveMode(void);
00228 
00229     /**
00230      * Put the nRF24L01+ into Transmit mode
00231      */
00232     void setTransmitMode(void);
00233 
00234     /**
00235      * Power up the nRF24L01+ into Standby mode
00236      */
00237     void powerUp(void);
00238 
00239     /**
00240      * Power down the nRF24L01+ into Power Down mode
00241      */
00242     void powerDown(void);
00243 
00244     /**
00245      * Enable the nRF24L01+ to Receive or Transmit (using the CE pin)
00246      */
00247     void enable(void);
00248 
00249     /**
00250      * Disable the nRF24L01+ to Receive or Transmit (using the CE pin)
00251      */
00252     void disable(void);
00253 
00254     /**
00255      * Transmit data
00256      *
00257      * @param pipe is ignored (included for consistency with file write routine)
00258      * @param data pointer to an array of bytes to write
00259      * @param count the number of bytes to send (1..32)
00260      * @return the number of bytes actually written, or -1 for an error
00261      */
00262     int write(int pipe, char *data, int count);
00263 
00264     /**
00265      * Receive data
00266      *
00267      * @param pipe the receive pipe to get data from
00268      * @param data pointer to an array of bytes to store the received data
00269      * @param count the number of bytes to receive (1..32)
00270      * @return the number of bytes actually received, 0 if none are received, or -1 for an error
00271      */
00272     int read(int pipe, char *data, int count);
00273 
00274     /**
00275      * Determine if there is data available to read
00276      *
00277      * @param pipe the receive pipe to check for data
00278      * @return true if the is data waiting in the given pipe
00279      */
00280     bool readable(int pipe = NRF24L01P_PIPE_P0);
00281 
00282     /**
00283      * Disable all receive pipes
00284      *
00285      * Note: receive pipes are enabled when their address is set.
00286      */
00287     void disableAllRxPipes(void);
00288 
00289     /**
00290      * Disable AutoAcknowledge function
00291      */
00292     void disableAutoAcknowledge(void);
00293 
00294     /**
00295      * Enable AutoAcknowledge function
00296      *
00297      * @param pipe the receive pipe
00298      */
00299     void enableAutoAcknowledge(int pipe = NRF24L01P_PIPE_P0);
00300 
00301     /**
00302      * Disable AutoRetransmit function
00303      */
00304     void disableAutoRetransmit(void);
00305 
00306     /**
00307      * Enable AutoRetransmit function
00308      *
00309      * @param delay the delay between restransmits, in uS (250uS..4000uS)
00310      * @param count number of retransmits before generating an error (1..15)
00311      */
00312     void enableAutoRetransmit(int delay, int count);
00313 
00314     int flushRxBuffer(void);
00315     int flushTxBuffer(void);
00316 
00317 private:
00318 
00319     /**
00320      * Get the contents of an addressable register.
00321      *
00322      * @param regAddress address of the register
00323      * @return the contents of the register
00324      */
00325     int getRegister(int regAddress);
00326 
00327     /**
00328      * Set the contents of an addressable register.
00329      *
00330      * @param regAddress address of the register
00331      * @param regData data to write to the register
00332      */
00333     void setRegister(int regAddress, int regData);
00334 
00335     /**
00336      * Get the contents of the status register.
00337      *
00338      * @return the contents of the status register
00339      */
00340     int getStatusRegister(void);
00341 
00342 
00343 
00344     SPI         spi_;
00345     DigitalOut  nCS_;
00346     DigitalOut  ce_;
00347     InterruptIn nIRQ_;
00348 
00349     int mode;
00350 
00351 };
00352 
00353 #endif /* __NRF24L01P_H__ */