Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: IeTI_MOD24_nrf_emit IeTI_MOD24 Arts_DMX512_carteV3_MIDI_nRF IeTI_nRF24
MOD24_NRF.h
00001 /****************************************************************************/ 00002 /* Test MOD-24LR / nrf24L01 */ 00003 /****************************************************************************/ 00004 /* LEnsE / Julien VILLEMEJANE / Institut d'Optique Graduate School */ 00005 /****************************************************************************/ 00006 /* Bibliothèque */ 00007 /****************************************************************************/ 00008 /* Test réalisé sur Nucléo-F411RE */ 00009 /****************************************************************************/ 00010 00011 #ifndef __MOD24NRF_H__ 00012 #define __MOD24NRF_H__ 00013 00014 /** 00015 * Includes 00016 */ 00017 #include "mbed.h" 00018 00019 /** 00020 * Defines 00021 */ 00022 #define NRF24L01P_TX_PWR_ZERO_DB 0 00023 #define NRF24L01P_TX_PWR_MINUS_6_DB -6 00024 #define NRF24L01P_TX_PWR_MINUS_12_DB -12 00025 #define NRF24L01P_TX_PWR_MINUS_18_DB -18 00026 00027 #define NRF24L01P_DATARATE_250_KBPS 250 00028 #define NRF24L01P_DATARATE_1_MBPS 1000 00029 #define NRF24L01P_DATARATE_2_MBPS 2000 00030 00031 #define NRF24L01P_CRC_NONE 0 00032 #define NRF24L01P_CRC_8_BIT 8 00033 #define NRF24L01P_CRC_16_BIT 16 00034 00035 #define NRF24L01P_MIN_RF_FREQUENCY 2400 00036 #define NRF24L01P_MAX_RF_FREQUENCY 2525 00037 00038 #define NRF24L01P_PIPE_P0 0 00039 #define NRF24L01P_PIPE_P1 1 00040 #define NRF24L01P_PIPE_P2 2 00041 #define NRF24L01P_PIPE_P3 3 00042 #define NRF24L01P_PIPE_P4 4 00043 #define NRF24L01P_PIPE_P5 5 00044 00045 /** 00046 * Default setup for the nRF24L01+, based on the Sparkfun "Nordic Serial Interface Board" 00047 * for evaluation (http://www.sparkfun.com/products/9019) 00048 */ 00049 #define DEFAULT_NRF24L01P_ADDRESS ((unsigned long long) 0xE7E7E7E7E7 ) 00050 #define DEFAULT_NRF24L01P_ADDRESS_WIDTH 5 00051 #define DEFAULT_NRF24L01P_CRC NRF24L01P_CRC_8_BIT 00052 #define DEFAULT_NRF24L01P_RF_FREQUENCY (NRF24L01P_MIN_RF_FREQUENCY + 2) 00053 #define DEFAULT_NRF24L01P_DATARATE NRF24L01P_DATARATE_1_MBPS 00054 #define DEFAULT_NRF24L01P_TX_PWR NRF24L01P_TX_PWR_ZERO_DB 00055 #define DEFAULT_NRF24L01P_TRANSFER_SIZE 4 00056 00057 /** 00058 * nRF24L01+ Single Chip 2.4GHz Transceiver from Nordic Semiconductor. 00059 */ 00060 class nRF24L01P { 00061 00062 public: 00063 00064 /** 00065 * Constructor. 00066 * 00067 * @param mosi mbed pin to use for MOSI line of SPI interface. 00068 * @param miso mbed pin to use for MISO line of SPI interface. 00069 * @param sck mbed pin to use for SCK line of SPI interface. 00070 * @param csn mbed pin to use for not chip select line of SPI interface. 00071 * @param ce mbed pin to use for the chip enable line. 00072 * @param irq mbed pin to use for the interrupt request line. 00073 */ 00074 nRF24L01P(PinName mosi, PinName miso, PinName sck, PinName csn, PinName ce, PinName irq = NC); 00075 00076 /** 00077 * Set the RF frequency. 00078 * 00079 * @param frequency the frequency of RF transmission in MHz (2400..2525). 00080 */ 00081 void setRfFrequency(int frequency = DEFAULT_NRF24L01P_RF_FREQUENCY); 00082 00083 /** 00084 * Get the RF frequency. 00085 * 00086 * @return the frequency of RF transmission in MHz (2400..2525). 00087 */ 00088 int getRfFrequency(void); 00089 00090 /** 00091 * Set the RF output power. 00092 * 00093 * @param power the RF output power in dBm (0, -6, -12 or -18). 00094 */ 00095 void setRfOutputPower(int power = DEFAULT_NRF24L01P_TX_PWR); 00096 00097 /** 00098 * Get the RF output power. 00099 * 00100 * @return the RF output power in dBm (0, -6, -12 or -18). 00101 */ 00102 int getRfOutputPower(void); 00103 00104 /** 00105 * Set the Air data rate. 00106 * 00107 * @param rate the air data rate in kbps (250, 1M or 2M). 00108 */ 00109 void setAirDataRate(int rate = DEFAULT_NRF24L01P_DATARATE); 00110 00111 /** 00112 * Get the Air data rate. 00113 * 00114 * @return the air data rate in kbps (250, 1M or 2M). 00115 */ 00116 int getAirDataRate(void); 00117 00118 /** 00119 * Set the CRC width. 00120 * 00121 * @param width the number of bits for the CRC (0, 8 or 16). 00122 */ 00123 void setCrcWidth(int width = DEFAULT_NRF24L01P_CRC); 00124 00125 /** 00126 * Get the CRC width. 00127 * 00128 * @return the number of bits for the CRC (0, 8 or 16). 00129 */ 00130 int getCrcWidth(void); 00131 00132 /** 00133 * Set the Receive address. 00134 * 00135 * @param address address associated with the particular pipe 00136 * @param width width of the address in bytes (3..5) 00137 * @param pipe pipe to associate the address with (0..5, default 0) 00138 * 00139 * Note that Pipes 0 & 1 have 3, 4 or 5 byte addresses, 00140 * while Pipes 2..5 only use the lowest byte (bits 7..0) of the 00141 * address provided here, and use 2, 3 or 4 bytes from Pipe 1's address. 00142 * The width parameter is ignored for Pipes 2..5. 00143 */ 00144 void setRxAddress(unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, int pipe = NRF24L01P_PIPE_P0); 00145 00146 void setRxAddress(unsigned long msb_address, unsigned long lsb_address, int width, int pipe = NRF24L01P_PIPE_P0); 00147 00148 /** 00149 * Set the Transmit address. 00150 * 00151 * @param address address for transmission 00152 * @param width width of the address in bytes (3..5) 00153 * 00154 * Note that the address width is shared with the Receive pipes, 00155 * so a change to that address width affect transmissions. 00156 */ 00157 void setTxAddress(unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH); 00158 00159 void setTxAddress(unsigned long msb_address, unsigned long lsb_address, int width); 00160 00161 /** 00162 * Get the Receive address. 00163 * 00164 * @param pipe pipe to get the address from (0..5, default 0) 00165 * @return the address associated with the particular pipe 00166 */ 00167 unsigned long long getRxAddress(int pipe = NRF24L01P_PIPE_P0); 00168 00169 /** 00170 * Get the Transmit address. 00171 * 00172 * @return address address for transmission 00173 */ 00174 unsigned long long getTxAddress(void); 00175 00176 /** 00177 * Set the transfer size. 00178 * 00179 * @param size the size of the transfer, in bytes (1..32) 00180 * @param pipe pipe for the transfer (0..5, default 0) 00181 */ 00182 void setTransferSize(int size = DEFAULT_NRF24L01P_TRANSFER_SIZE, int pipe = NRF24L01P_PIPE_P0); 00183 00184 /** 00185 * Get the transfer size. 00186 * 00187 * @return the size of the transfer, in bytes (1..32). 00188 */ 00189 int getTransferSize(int pipe = NRF24L01P_PIPE_P0); 00190 00191 00192 /** 00193 * Get the RPD (Received Power Detector) state. 00194 * 00195 * @return true if the received power exceeded -64dBm 00196 */ 00197 bool getRPD(void); 00198 00199 /** 00200 * Put the nRF24L01+ into Receive mode 00201 */ 00202 void setReceiveMode(void); 00203 00204 /** 00205 * Put the nRF24L01+ into Transmit mode 00206 */ 00207 void setTransmitMode(void); 00208 00209 /** 00210 * Power up the nRF24L01+ into Standby mode 00211 */ 00212 void powerUp(void); 00213 00214 /** 00215 * Power down the nRF24L01+ into Power Down mode 00216 */ 00217 void powerDown(void); 00218 00219 /** 00220 * Enable the nRF24L01+ to Receive or Transmit (using the CE pin) 00221 */ 00222 void enable(void); 00223 00224 /** 00225 * Disable the nRF24L01+ to Receive or Transmit (using the CE pin) 00226 */ 00227 void disable(void); 00228 00229 /** 00230 * Transmit data 00231 * 00232 * @param pipe is ignored (included for consistency with file write routine) 00233 * @param data pointer to an array of bytes to write 00234 * @param count the number of bytes to send (1..32) 00235 * @return the number of bytes actually written, or -1 for an error 00236 */ 00237 int write(int pipe, char *data, int count); 00238 00239 /** 00240 * Receive data 00241 * 00242 * @param pipe the receive pipe to get data from 00243 * @param data pointer to an array of bytes to store the received data 00244 * @param count the number of bytes to receive (1..32) 00245 * @return the number of bytes actually received, 0 if none are received, or -1 for an error 00246 */ 00247 int read(int pipe, char *data, int count); 00248 00249 /** 00250 * Determine if there is data available to read 00251 * 00252 * @param pipe the receive pipe to check for data 00253 * @return true if the is data waiting in the given pipe 00254 */ 00255 bool readable(int pipe = NRF24L01P_PIPE_P0); 00256 00257 /** 00258 * Disable all receive pipes 00259 * 00260 * Note: receive pipes are enabled when their address is set. 00261 */ 00262 void disableAllRxPipes(void); 00263 00264 /** 00265 * Disable AutoAcknowledge function 00266 */ 00267 void disableAutoAcknowledge(void); 00268 00269 /** 00270 * Enable AutoAcknowledge function 00271 * 00272 * @param pipe the receive pipe 00273 */ 00274 void enableAutoAcknowledge(int pipe = NRF24L01P_PIPE_P0); 00275 00276 /** 00277 * Disable AutoRetransmit function 00278 */ 00279 void disableAutoRetransmit(void); 00280 00281 /** 00282 * Enable AutoRetransmit function 00283 * 00284 * @param delay the delay between restransmits, in uS (250uS..4000uS) 00285 * @param count number of retransmits before generating an error (1..15) 00286 */ 00287 void enableAutoRetransmit(int delay, int count); 00288 00289 private: 00290 00291 /** 00292 * Get the contents of an addressable register. 00293 * 00294 * @param regAddress address of the register 00295 * @return the contents of the register 00296 */ 00297 int getRegister(int regAddress); 00298 00299 /** 00300 * Set the contents of an addressable register. 00301 * 00302 * @param regAddress address of the register 00303 * @param regData data to write to the register 00304 */ 00305 void setRegister(int regAddress, int regData); 00306 00307 /** 00308 * Get the contents of the status register. 00309 * 00310 * @return the contents of the status register 00311 */ 00312 int getStatusRegister(void); 00313 00314 SPI spi_; 00315 DigitalOut nCS_; 00316 DigitalOut ce_; 00317 InterruptIn nIRQ_; 00318 00319 int mode; 00320 00321 }; 00322 00323 #endif /* __MOD24NRF_H__ */
Generated on Sat Aug 20 2022 17:43:12 by
1.7.2