Flight control with nRF and MPU6050

Dependencies:   mbed

Committer:
Ucial
Date:
Fri Aug 31 10:17:41 2018 +0000
Revision:
1:23afaebeed15
mpu & nRF

Who changed what in which revision?

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