This is for our FYDP project. 2 MPU6050s are used

Dependencies:   Servo mbed

Committer:
majik
Date:
Sat Mar 21 21:31:29 2015 +0000
Revision:
0:21019d94ad33
Both IMUs work now

Who changed what in which revision?

UserRevisionLine numberNew contents of line
majik 0:21019d94ad33 1 /**
majik 0:21019d94ad33 2 * @file nRF24L01P.h
majik 0:21019d94ad33 3 *
majik 0:21019d94ad33 4 * @author Owen Edwards
majik 0:21019d94ad33 5 *
majik 0:21019d94ad33 6 * @section LICENSE
majik 0:21019d94ad33 7 *
majik 0:21019d94ad33 8 * Copyright (c) 2010 Owen Edwards
majik 0:21019d94ad33 9 *
majik 0:21019d94ad33 10 * This program is free software: you can redistribute it and/or modify
majik 0:21019d94ad33 11 * it under the terms of the GNU General Public License as published by
majik 0:21019d94ad33 12 * the Free Software Foundation, either version 3 of the License, or
majik 0:21019d94ad33 13 * (at your option) any later version.
majik 0:21019d94ad33 14 *
majik 0:21019d94ad33 15 * This program is distributed in the hope that it will be useful,
majik 0:21019d94ad33 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
majik 0:21019d94ad33 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
majik 0:21019d94ad33 18 * GNU General Public License for more details.
majik 0:21019d94ad33 19 *
majik 0:21019d94ad33 20 * You should have received a copy of the GNU General Public License
majik 0:21019d94ad33 21 * along with this program. If not, see <http://www.gnu.org/licenses/>.
majik 0:21019d94ad33 22 *
majik 0:21019d94ad33 23 * The above copyright notice and this permission notice shall be included in
majik 0:21019d94ad33 24 * all copies or substantial portions of the Software.
majik 0:21019d94ad33 25 *
majik 0:21019d94ad33 26 * @section DESCRIPTION
majik 0:21019d94ad33 27 *
majik 0:21019d94ad33 28 * nRF24L01+ Single Chip 2.4GHz Transceiver from Nordic Semiconductor.
majik 0:21019d94ad33 29 *
majik 0:21019d94ad33 30 * Datasheet:
majik 0:21019d94ad33 31 *
majik 0:21019d94ad33 32 * http://www.nordicsemi.no/files/Product/data_sheet/nRF24L01P_Product_Specification_1_0.pdf
majik 0:21019d94ad33 33 */
majik 0:21019d94ad33 34
majik 0:21019d94ad33 35 #ifndef __NRF24L01P_H__
majik 0:21019d94ad33 36 #define __NRF24L01P_H__
majik 0:21019d94ad33 37
majik 0:21019d94ad33 38 /**
majik 0:21019d94ad33 39 * Includes
majik 0:21019d94ad33 40 */
majik 0:21019d94ad33 41 #include "mbed.h"
majik 0:21019d94ad33 42
majik 0:21019d94ad33 43 /**
majik 0:21019d94ad33 44 * Defines
majik 0:21019d94ad33 45 */
majik 0:21019d94ad33 46 #define NRF24L01P_TX_PWR_ZERO_DB 0
majik 0:21019d94ad33 47 #define NRF24L01P_TX_PWR_MINUS_6_DB -6
majik 0:21019d94ad33 48 #define NRF24L01P_TX_PWR_MINUS_12_DB -12
majik 0:21019d94ad33 49 #define NRF24L01P_TX_PWR_MINUS_18_DB -18
majik 0:21019d94ad33 50
majik 0:21019d94ad33 51 #define NRF24L01P_DATARATE_250_KBPS 250
majik 0:21019d94ad33 52 #define NRF24L01P_DATARATE_1_MBPS 1000
majik 0:21019d94ad33 53 #define NRF24L01P_DATARATE_2_MBPS 2000
majik 0:21019d94ad33 54
majik 0:21019d94ad33 55 #define NRF24L01P_CRC_NONE 0
majik 0:21019d94ad33 56 #define NRF24L01P_CRC_8_BIT 8
majik 0:21019d94ad33 57 #define NRF24L01P_CRC_16_BIT 16
majik 0:21019d94ad33 58
majik 0:21019d94ad33 59 #define NRF24L01P_MIN_RF_FREQUENCY 2400
majik 0:21019d94ad33 60 #define NRF24L01P_MAX_RF_FREQUENCY 2525
majik 0:21019d94ad33 61
majik 0:21019d94ad33 62 #define NRF24L01P_PIPE_P0 0
majik 0:21019d94ad33 63 #define NRF24L01P_PIPE_P1 1
majik 0:21019d94ad33 64 #define NRF24L01P_PIPE_P2 2
majik 0:21019d94ad33 65 #define NRF24L01P_PIPE_P3 3
majik 0:21019d94ad33 66 #define NRF24L01P_PIPE_P4 4
majik 0:21019d94ad33 67 #define NRF24L01P_PIPE_P5 5
majik 0:21019d94ad33 68
majik 0:21019d94ad33 69 /**
majik 0:21019d94ad33 70 * Default setup for the nRF24L01+, based on the Sparkfun "Nordic Serial Interface Board"
majik 0:21019d94ad33 71 * for evaluation (http://www.sparkfun.com/products/9019)
majik 0:21019d94ad33 72 */
majik 0:21019d94ad33 73 #define DEFAULT_NRF24L01P_ADDRESS ((unsigned long long) 0xE7E7E7E7E7 )
majik 0:21019d94ad33 74 #define DEFAULT_NRF24L01P_ADDRESS_WIDTH 5
majik 0:21019d94ad33 75 #define DEFAULT_NRF24L01P_CRC NRF24L01P_CRC_8_BIT
majik 0:21019d94ad33 76 #define DEFAULT_NRF24L01P_RF_FREQUENCY (NRF24L01P_MIN_RF_FREQUENCY + 2)
majik 0:21019d94ad33 77 #define DEFAULT_NRF24L01P_DATARATE NRF24L01P_DATARATE_1_MBPS
majik 0:21019d94ad33 78 #define DEFAULT_NRF24L01P_TX_PWR NRF24L01P_TX_PWR_ZERO_DB
majik 0:21019d94ad33 79 #define DEFAULT_NRF24L01P_TRANSFER_SIZE 4
majik 0:21019d94ad33 80
majik 0:21019d94ad33 81 /**
majik 0:21019d94ad33 82 * nRF24L01+ Single Chip 2.4GHz Transceiver from Nordic Semiconductor.
majik 0:21019d94ad33 83 */
majik 0:21019d94ad33 84 class nRF24L01P {
majik 0:21019d94ad33 85
majik 0:21019d94ad33 86 public:
majik 0:21019d94ad33 87
majik 0:21019d94ad33 88 /**
majik 0:21019d94ad33 89 * Constructor.
majik 0:21019d94ad33 90 *
majik 0:21019d94ad33 91 * @param mosi mbed pin to use for MOSI line of SPI interface.
majik 0:21019d94ad33 92 * @param miso mbed pin to use for MISO line of SPI interface.
majik 0:21019d94ad33 93 * @param sck mbed pin to use for SCK line of SPI interface.
majik 0:21019d94ad33 94 * @param csn mbed pin to use for not chip select line of SPI interface.
majik 0:21019d94ad33 95 * @param ce mbed pin to use for the chip enable line.
majik 0:21019d94ad33 96 * @param irq mbed pin to use for the interrupt request line.
majik 0:21019d94ad33 97 */
majik 0:21019d94ad33 98 nRF24L01P(PinName mosi, PinName miso, PinName sck, PinName csn, PinName ce, PinName irq = NC);
majik 0:21019d94ad33 99
majik 0:21019d94ad33 100 /**
majik 0:21019d94ad33 101 * Set the RF frequency.
majik 0:21019d94ad33 102 *
majik 0:21019d94ad33 103 * @param frequency the frequency of RF transmission in MHz (2400..2525).
majik 0:21019d94ad33 104 */
majik 0:21019d94ad33 105 void setRfFrequency(int frequency = DEFAULT_NRF24L01P_RF_FREQUENCY);
majik 0:21019d94ad33 106
majik 0:21019d94ad33 107 /**
majik 0:21019d94ad33 108 * Get the RF frequency.
majik 0:21019d94ad33 109 *
majik 0:21019d94ad33 110 * @return the frequency of RF transmission in MHz (2400..2525).
majik 0:21019d94ad33 111 */
majik 0:21019d94ad33 112 int getRfFrequency(void);
majik 0:21019d94ad33 113
majik 0:21019d94ad33 114 /**
majik 0:21019d94ad33 115 * Set the RF output power.
majik 0:21019d94ad33 116 *
majik 0:21019d94ad33 117 * @param power the RF output power in dBm (0, -6, -12 or -18).
majik 0:21019d94ad33 118 */
majik 0:21019d94ad33 119 void setRfOutputPower(int power = DEFAULT_NRF24L01P_TX_PWR);
majik 0:21019d94ad33 120
majik 0:21019d94ad33 121 /**
majik 0:21019d94ad33 122 * Get the RF output power.
majik 0:21019d94ad33 123 *
majik 0:21019d94ad33 124 * @return the RF output power in dBm (0, -6, -12 or -18).
majik 0:21019d94ad33 125 */
majik 0:21019d94ad33 126 int getRfOutputPower(void);
majik 0:21019d94ad33 127
majik 0:21019d94ad33 128 /**
majik 0:21019d94ad33 129 * Set the Air data rate.
majik 0:21019d94ad33 130 *
majik 0:21019d94ad33 131 * @param rate the air data rate in kbps (250, 1M or 2M).
majik 0:21019d94ad33 132 */
majik 0:21019d94ad33 133 void setAirDataRate(int rate = DEFAULT_NRF24L01P_DATARATE);
majik 0:21019d94ad33 134
majik 0:21019d94ad33 135 /**
majik 0:21019d94ad33 136 * Get the Air data rate.
majik 0:21019d94ad33 137 *
majik 0:21019d94ad33 138 * @return the air data rate in kbps (250, 1M or 2M).
majik 0:21019d94ad33 139 */
majik 0:21019d94ad33 140 int getAirDataRate(void);
majik 0:21019d94ad33 141
majik 0:21019d94ad33 142 /**
majik 0:21019d94ad33 143 * Set the CRC width.
majik 0:21019d94ad33 144 *
majik 0:21019d94ad33 145 * @param width the number of bits for the CRC (0, 8 or 16).
majik 0:21019d94ad33 146 */
majik 0:21019d94ad33 147 void setCrcWidth(int width = DEFAULT_NRF24L01P_CRC);
majik 0:21019d94ad33 148
majik 0:21019d94ad33 149 /**
majik 0:21019d94ad33 150 * Get the CRC width.
majik 0:21019d94ad33 151 *
majik 0:21019d94ad33 152 * @return the number of bits for the CRC (0, 8 or 16).
majik 0:21019d94ad33 153 */
majik 0:21019d94ad33 154 int getCrcWidth(void);
majik 0:21019d94ad33 155
majik 0:21019d94ad33 156 /**
majik 0:21019d94ad33 157 * Set the Receive address.
majik 0:21019d94ad33 158 *
majik 0:21019d94ad33 159 * @param address address associated with the particular pipe
majik 0:21019d94ad33 160 * @param width width of the address in bytes (3..5)
majik 0:21019d94ad33 161 * @param pipe pipe to associate the address with (0..5, default 0)
majik 0:21019d94ad33 162 *
majik 0:21019d94ad33 163 * Note that Pipes 0 & 1 have 3, 4 or 5 byte addresses,
majik 0:21019d94ad33 164 * while Pipes 2..5 only use the lowest byte (bits 7..0) of the
majik 0:21019d94ad33 165 * address provided here, and use 2, 3 or 4 bytes from Pipe 1's address.
majik 0:21019d94ad33 166 * The width parameter is ignored for Pipes 2..5.
majik 0:21019d94ad33 167 */
majik 0:21019d94ad33 168 void setRxAddress(unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH, int pipe = NRF24L01P_PIPE_P0);
majik 0:21019d94ad33 169
majik 0:21019d94ad33 170 void setRxAddress(unsigned long msb_address, unsigned long lsb_address, int width, int pipe = NRF24L01P_PIPE_P0);
majik 0:21019d94ad33 171
majik 0:21019d94ad33 172 /**
majik 0:21019d94ad33 173 * Set the Transmit address.
majik 0:21019d94ad33 174 *
majik 0:21019d94ad33 175 * @param address address for transmission
majik 0:21019d94ad33 176 * @param width width of the address in bytes (3..5)
majik 0:21019d94ad33 177 *
majik 0:21019d94ad33 178 * Note that the address width is shared with the Receive pipes,
majik 0:21019d94ad33 179 * so a change to that address width affect transmissions.
majik 0:21019d94ad33 180 */
majik 0:21019d94ad33 181 void setTxAddress(unsigned long long address = DEFAULT_NRF24L01P_ADDRESS, int width = DEFAULT_NRF24L01P_ADDRESS_WIDTH);
majik 0:21019d94ad33 182
majik 0:21019d94ad33 183 void setTxAddress(unsigned long msb_address, unsigned long lsb_address, int width);
majik 0:21019d94ad33 184
majik 0:21019d94ad33 185 /**
majik 0:21019d94ad33 186 * Get the Receive address.
majik 0:21019d94ad33 187 *
majik 0:21019d94ad33 188 * @param pipe pipe to get the address from (0..5, default 0)
majik 0:21019d94ad33 189 * @return the address associated with the particular pipe
majik 0:21019d94ad33 190 */
majik 0:21019d94ad33 191 unsigned long long getRxAddress(int pipe = NRF24L01P_PIPE_P0);
majik 0:21019d94ad33 192
majik 0:21019d94ad33 193 /**
majik 0:21019d94ad33 194 * Get the Transmit address.
majik 0:21019d94ad33 195 *
majik 0:21019d94ad33 196 * @return address address for transmission
majik 0:21019d94ad33 197 */
majik 0:21019d94ad33 198 unsigned long long getTxAddress(void);
majik 0:21019d94ad33 199
majik 0:21019d94ad33 200 /**
majik 0:21019d94ad33 201 * Set the transfer size.
majik 0:21019d94ad33 202 *
majik 0:21019d94ad33 203 * @param size the size of the transfer, in bytes (1..32)
majik 0:21019d94ad33 204 * @param pipe pipe for the transfer (0..5, default 0)
majik 0:21019d94ad33 205 */
majik 0:21019d94ad33 206 void setTransferSize(int size = DEFAULT_NRF24L01P_TRANSFER_SIZE, int pipe = NRF24L01P_PIPE_P0);
majik 0:21019d94ad33 207
majik 0:21019d94ad33 208 /**
majik 0:21019d94ad33 209 * Get the transfer size.
majik 0:21019d94ad33 210 *
majik 0:21019d94ad33 211 * @return the size of the transfer, in bytes (1..32).
majik 0:21019d94ad33 212 */
majik 0:21019d94ad33 213 int getTransferSize(int pipe = NRF24L01P_PIPE_P0);
majik 0:21019d94ad33 214
majik 0:21019d94ad33 215
majik 0:21019d94ad33 216 /**
majik 0:21019d94ad33 217 * Get the RPD (Received Power Detector) state.
majik 0:21019d94ad33 218 *
majik 0:21019d94ad33 219 * @return true if the received power exceeded -64dBm
majik 0:21019d94ad33 220 */
majik 0:21019d94ad33 221 bool getRPD(void);
majik 0:21019d94ad33 222
majik 0:21019d94ad33 223 /**
majik 0:21019d94ad33 224 * Put the nRF24L01+ into Receive mode
majik 0:21019d94ad33 225 */
majik 0:21019d94ad33 226 void setReceiveMode(void);
majik 0:21019d94ad33 227
majik 0:21019d94ad33 228 /**
majik 0:21019d94ad33 229 * Put the nRF24L01+ into Transmit mode
majik 0:21019d94ad33 230 */
majik 0:21019d94ad33 231 void setTransmitMode(void);
majik 0:21019d94ad33 232
majik 0:21019d94ad33 233 /**
majik 0:21019d94ad33 234 * Power up the nRF24L01+ into Standby mode
majik 0:21019d94ad33 235 */
majik 0:21019d94ad33 236 void powerUp(void);
majik 0:21019d94ad33 237
majik 0:21019d94ad33 238 /**
majik 0:21019d94ad33 239 * Power down the nRF24L01+ into Power Down mode
majik 0:21019d94ad33 240 */
majik 0:21019d94ad33 241 void powerDown(void);
majik 0:21019d94ad33 242
majik 0:21019d94ad33 243 /**
majik 0:21019d94ad33 244 * Enable the nRF24L01+ to Receive or Transmit (using the CE pin)
majik 0:21019d94ad33 245 */
majik 0:21019d94ad33 246 void enable(void);
majik 0:21019d94ad33 247
majik 0:21019d94ad33 248 /**
majik 0:21019d94ad33 249 * Disable the nRF24L01+ to Receive or Transmit (using the CE pin)
majik 0:21019d94ad33 250 */
majik 0:21019d94ad33 251 void disable(void);
majik 0:21019d94ad33 252
majik 0:21019d94ad33 253 /**
majik 0:21019d94ad33 254 * Transmit data
majik 0:21019d94ad33 255 *
majik 0:21019d94ad33 256 * @param pipe is ignored (included for consistency with file write routine)
majik 0:21019d94ad33 257 * @param data pointer to an array of bytes to write
majik 0:21019d94ad33 258 * @param count the number of bytes to send (1..32)
majik 0:21019d94ad33 259 * @return the number of bytes actually written, or -1 for an error
majik 0:21019d94ad33 260 */
majik 0:21019d94ad33 261 int write(int pipe, char *data, int count);
majik 0:21019d94ad33 262
majik 0:21019d94ad33 263 /**
majik 0:21019d94ad33 264 * Receive data
majik 0:21019d94ad33 265 *
majik 0:21019d94ad33 266 * @param pipe the receive pipe to get data from
majik 0:21019d94ad33 267 * @param data pointer to an array of bytes to store the received data
majik 0:21019d94ad33 268 * @param count the number of bytes to receive (1..32)
majik 0:21019d94ad33 269 * @return the number of bytes actually received, 0 if none are received, or -1 for an error
majik 0:21019d94ad33 270 */
majik 0:21019d94ad33 271 int read(int pipe, char *data, int count);
majik 0:21019d94ad33 272
majik 0:21019d94ad33 273 /**
majik 0:21019d94ad33 274 * Determine if there is data available to read
majik 0:21019d94ad33 275 *
majik 0:21019d94ad33 276 * @param pipe the receive pipe to check for data
majik 0:21019d94ad33 277 * @return true if the is data waiting in the given pipe
majik 0:21019d94ad33 278 */
majik 0:21019d94ad33 279 bool readable(int pipe = NRF24L01P_PIPE_P0);
majik 0:21019d94ad33 280
majik 0:21019d94ad33 281 /**
majik 0:21019d94ad33 282 * Disable all receive pipes
majik 0:21019d94ad33 283 *
majik 0:21019d94ad33 284 * Note: receive pipes are enabled when their address is set.
majik 0:21019d94ad33 285 */
majik 0:21019d94ad33 286 void disableAllRxPipes(void);
majik 0:21019d94ad33 287
majik 0:21019d94ad33 288 /**
majik 0:21019d94ad33 289 * Disable AutoAcknowledge function
majik 0:21019d94ad33 290 */
majik 0:21019d94ad33 291 void disableAutoAcknowledge(void);
majik 0:21019d94ad33 292
majik 0:21019d94ad33 293 /**
majik 0:21019d94ad33 294 * Enable AutoAcknowledge function
majik 0:21019d94ad33 295 *
majik 0:21019d94ad33 296 * @param pipe the receive pipe
majik 0:21019d94ad33 297 */
majik 0:21019d94ad33 298 void enableAutoAcknowledge(int pipe = NRF24L01P_PIPE_P0);
majik 0:21019d94ad33 299
majik 0:21019d94ad33 300 /**
majik 0:21019d94ad33 301 * Disable AutoRetransmit function
majik 0:21019d94ad33 302 */
majik 0:21019d94ad33 303 void disableAutoRetransmit(void);
majik 0:21019d94ad33 304
majik 0:21019d94ad33 305 /**
majik 0:21019d94ad33 306 * Enable AutoRetransmit function
majik 0:21019d94ad33 307 *
majik 0:21019d94ad33 308 * @param delay the delay between restransmits, in uS (250uS..4000uS)
majik 0:21019d94ad33 309 * @param count number of retransmits before generating an error (1..15)
majik 0:21019d94ad33 310 */
majik 0:21019d94ad33 311 void enableAutoRetransmit(int delay, int count);
majik 0:21019d94ad33 312
majik 0:21019d94ad33 313 private:
majik 0:21019d94ad33 314
majik 0:21019d94ad33 315 /**
majik 0:21019d94ad33 316 * Get the contents of an addressable register.
majik 0:21019d94ad33 317 *
majik 0:21019d94ad33 318 * @param regAddress address of the register
majik 0:21019d94ad33 319 * @return the contents of the register
majik 0:21019d94ad33 320 */
majik 0:21019d94ad33 321 int getRegister(int regAddress);
majik 0:21019d94ad33 322
majik 0:21019d94ad33 323 /**
majik 0:21019d94ad33 324 * Set the contents of an addressable register.
majik 0:21019d94ad33 325 *
majik 0:21019d94ad33 326 * @param regAddress address of the register
majik 0:21019d94ad33 327 * @param regData data to write to the register
majik 0:21019d94ad33 328 */
majik 0:21019d94ad33 329 void setRegister(int regAddress, int regData);
majik 0:21019d94ad33 330
majik 0:21019d94ad33 331 /**
majik 0:21019d94ad33 332 * Get the contents of the status register.
majik 0:21019d94ad33 333 *
majik 0:21019d94ad33 334 * @return the contents of the status register
majik 0:21019d94ad33 335 */
majik 0:21019d94ad33 336 int getStatusRegister(void);
majik 0:21019d94ad33 337
majik 0:21019d94ad33 338 SPI spi_;
majik 0:21019d94ad33 339 DigitalOut nCS_;
majik 0:21019d94ad33 340 DigitalOut ce_;
majik 0:21019d94ad33 341 InterruptIn nIRQ_;
majik 0:21019d94ad33 342
majik 0:21019d94ad33 343 int mode;
majik 0:21019d94ad33 344
majik 0:21019d94ad33 345 };
majik 0:21019d94ad33 346
majik 0:21019d94ad33 347 #endif /* __NRF24L01P_H__ */