ISR UC / ISR_Mini-explorer

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

Dependents:   Mapping VirtualForces_debug OneFileToRuleThemAll VirtualForces_with_class

Committer:
ISR
Date:
Thu Feb 02 12:21:11 2017 +0000
Revision:
0:15a30802e719
Initial commit.

Who changed what in which revision?

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