Energy harvesting mobile robot. Developed at Institute of Systems and Robotics — University of Coimbra.

Fork of ISR_Mini-explorer by ISR UC

Committer:
Bisca
Date:
Wed Mar 29 12:15:03 2017 +0000
Revision:
1:9e5152aa1bb7
Parent:
0:15a30802e719
v1

Who changed what in which revision?

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