RX TX Flush methods added

Fork of nRF24L01P by Owen Edwards

Committer:
Christilut
Date:
Thu Mar 07 09:28:09 2013 +0000
Revision:
1:054a50936ab6
Parent:
0:8ae48233b4e4
Added rx and tx buffer flush methods

Who changed what in which revision?

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