Versie 0.2 Het versturen via de NRF werkt nog niet helemaal omdat er per 4 bytes verstuurd moet worden. Wordt gefixt d.m.v. dynamic stuff!

Dependencies:   RTOS mbed

Fork of rtos_basic by mbed official

Committer:
Reynier
Date:
Fri Apr 29 15:27:26 2016 +0000
Revision:
7:88ba5138f810
Versie 0.2 Het versturen via de NRF werkt nog niet helemaal omdat er per 4 bytes verstuurd moet worden. Wordt gefixt d.m.v. dynamic stuff!

Who changed what in which revision?

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