I-O DATA DEV2 / nRF24L01P

Dependents:   stm32_hello_nrf24 commu4 commu4-2 commu4 ... more

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 public:
00087 
00088     /**
00089      * Constructor.
00090      *
00091      * @param mosi mbed pin to use for MOSI line of SPI interface.
00092      * @param miso mbed pin to use for MISO line of SPI interface.
00093      * @param sck mbed pin to use for SCK line of SPI interface.
00094      * @param csn mbed pin to use for not chip select line of SPI interface.
00095      * @param ce mbed pin to use for the chip enable line.
00096      * @param irq mbed pin to use for the interrupt request line.
00097      */
00098     nRF24L01P(PinName mosi, PinName miso, PinName sck, PinName csn, PinName ce, PinName irq = NC);
00099 
00100     void flush_rx_fifo();
00101     void flush_tx_fifo();
00102     /**
00103      * Set the RF frequency.
00104      *
00105      * @param frequency the frequency of RF transmission in MHz (2400..2525).
00106      */
00107     void setRfFrequency(int frequency = DEFAULT_NRF24L01P_RF_FREQUENCY);
00108 
00109     /**
00110      * Get the RF frequency.
00111      *
00112      * @return the frequency of RF transmission in MHz (2400..2525).
00113      */
00114     int getRfFrequency(void);
00115 
00116     /**
00117      * Set the RF output power.
00118      *
00119      * @param power the RF output power in dBm (0, -6, -12 or -18).
00120      */
00121     void setRfOutputPower(int power = DEFAULT_NRF24L01P_TX_PWR);
00122 
00123     /**
00124      * Get the RF output power.
00125      *
00126      * @return the RF output power in dBm (0, -6, -12 or -18).
00127      */
00128     int getRfOutputPower(void);
00129 
00130     /**
00131      * Set the Air data rate.
00132      *
00133      * @param rate the air data rate in kbps (250, 1M or 2M).
00134      */
00135     void setAirDataRate(int rate = DEFAULT_NRF24L01P_DATARATE);
00136 
00137     /**
00138      * Get the Air data rate.
00139      *
00140      * @return the air data rate in kbps (250, 1M or 2M).
00141      */
00142     int getAirDataRate(void);
00143 
00144     /**
00145      * Set the CRC width.
00146      *
00147      * @param width the number of bits for the CRC (0, 8 or 16).
00148      */
00149     void setCrcWidth(int width = DEFAULT_NRF24L01P_CRC);
00150 
00151     /**
00152      * Get the CRC width.
00153      *
00154      * @return the number of bits for the CRC (0, 8 or 16).
00155      */
00156     int getCrcWidth(void);
00157 
00158     /**
00159      * Set the Receive address.
00160      *
00161      * @param address address associated with the particular pipe
00162      * @param width width of the address in bytes (3..5)
00163      * @param pipe pipe to associate the address with (0..5, default 0)
00164      *
00165      * Note that Pipes 0 & 1 have 3, 4 or 5 byte addresses,
00166      *  while Pipes 2..5 only use the lowest byte (bits 7..0) of the
00167      *  address provided here, and use 2, 3 or 4 bytes from Pipe 1's address.
00168      *  The width parameter is ignored for Pipes 2..5.
00169      */
00170     void setRxAddress(unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, int pipe = NRF24L01P_PIPE_P0);
00171 
00172     void setRxAddress(unsigned long msb_address, unsigned long lsb_address, int width, int pipe = NRF24L01P_PIPE_P0);
00173 
00174     /**
00175      * Set the Transmit address.
00176      *
00177      * @param address address for transmission
00178      * @param width width of the address in bytes (3..5)
00179      *
00180      * Note that the address width is shared with the Receive pipes,
00181      *  so a change to that address width affect transmissions.
00182      */
00183     void setTxAddress(unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH);
00184 
00185     void setTxAddress(unsigned long msb_address, unsigned long lsb_address, int width);
00186 
00187     /**
00188      * Get the Receive address.
00189      *
00190      * @param pipe pipe to get the address from (0..5, default 0)
00191      * @return the address associated with the particular pipe
00192      */
00193     unsigned long long getRxAddress(int pipe = NRF24L01P_PIPE_P0);
00194 
00195     /**
00196      * Get the Transmit address.
00197      *
00198      * @return address address for transmission
00199      */
00200     unsigned long long getTxAddress(void);
00201 
00202     /**
00203      * Set the transfer size.
00204      *
00205      * @param size the size of the transfer, in bytes (1..32)
00206      * @param pipe pipe for the transfer (0..5, default 0)
00207      */
00208     void setTransferSize(int size = DEFAULT_NRF24L01P_TRANSFER_SIZE, int pipe = NRF24L01P_PIPE_P0);
00209 
00210     /**
00211      * Get the transfer size.
00212      *
00213      * @return the size of the transfer, in bytes (1..32).
00214      */
00215     int getTransferSize(int pipe = NRF24L01P_PIPE_P0);
00216 
00217 
00218     /**
00219      * Get the RPD (Received Power Detector) state.
00220      *
00221      * @return true if the received power exceeded -64dBm
00222      */
00223     bool getRPD(void);
00224 
00225     /**
00226      * Put the nRF24L01+ into Receive mode
00227      */
00228     void setReceiveMode(void);
00229 
00230     /**
00231      * Put the nRF24L01+ into Transmit mode
00232      */
00233     void setTransmitMode(void);
00234 
00235     /**
00236      * Power up the nRF24L01+ into Standby mode
00237      */
00238     void powerUp(void);
00239 
00240     /**
00241      * Power down the nRF24L01+ into Power Down mode
00242      */
00243     void powerDown(void);
00244 
00245     /**
00246      * Enable the nRF24L01+ to Receive or Transmit (using the CE pin)
00247      */
00248     void enable(void);
00249 
00250     /**
00251      * Disable the nRF24L01+ to Receive or Transmit (using the CE pin)
00252      */
00253     void disable(void);
00254 
00255     /**
00256      * Transmit data
00257      *
00258      * @param pipe is ignored (included for consistency with file write routine)
00259      * @param data pointer to an array of bytes to write
00260      * @param count the number of bytes to send (1..32)
00261      * @return the number of bytes actually written, or -1 for an error
00262      */
00263     int write(int pipe, char *data, int count);
00264     
00265     /**
00266      * Receive data
00267      *
00268      * @param pipe the receive pipe to get data from
00269      * @param data pointer to an array of bytes to store the received data
00270      * @param count the number of bytes to receive (1..32)
00271      * @return the number of bytes actually received, 0 if none are received, or -1 for an error
00272      */
00273     int read(int pipe, char *data, int count);
00274 
00275     /**
00276      * Determine if there is data available to read
00277      *
00278      * @param pipe the receive pipe to check for data
00279      * @return true if the is data waiting in the given pipe
00280      */
00281     bool readable(int pipe = NRF24L01P_PIPE_P0);
00282 
00283     /**
00284      * Disable all receive pipes
00285      *
00286      * Note: receive pipes are enabled when their address is set.
00287      */
00288     void disableAllRxPipes(void);
00289     
00290     /**
00291      * Disable AutoAcknowledge function
00292      */
00293     void disableAutoAcknowledge(void);
00294     
00295     /**
00296      * Enable AutoAcknowledge function
00297      *
00298      * @param pipe the receive pipe
00299      */
00300     void enableAutoAcknowledge(int pipe = NRF24L01P_PIPE_P0);
00301     
00302     /**
00303      * Disable AutoRetransmit function
00304      */
00305     void disableAutoRetransmit(void);
00306     
00307     /**
00308      * Enable AutoRetransmit function
00309      *
00310      * @param delay the delay between restransmits, in uS (250uS..4000uS)
00311      * @param count number of retransmits before generating an error (1..15)
00312      */
00313     void enableAutoRetransmit(int delay, int count);
00314 
00315 private:
00316 
00317     /**
00318      * Get the contents of an addressable register.
00319      *
00320      * @param regAddress address of the register
00321      * @return the contents of the register
00322      */
00323     int getRegister(int regAddress);
00324 
00325     /**
00326      * Set the contents of an addressable register.
00327      *
00328      * @param regAddress address of the register
00329      * @param regData data to write to the register
00330      */
00331     void setRegister(int regAddress, int regData);
00332 
00333     /**
00334      * Get the contents of the status register.
00335      *
00336      * @return the contents of the status register
00337      */
00338     int getStatusRegister(void);
00339 
00340     SPI         spi_;
00341     DigitalOut  nCS_;
00342     DigitalOut  ce_;
00343     InterruptIn nIRQ_;
00344 
00345     int mode;
00346 
00347 };
00348 
00349 #endif /* __NRF24L01P_H__ */