nrf24l01p.h 18.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
/*
 * Copyright (C) 2014 Hamburg University of Applied Sciences
 *
 * This file is subject to the terms and conditions of the GNU Lesser General
 * Public License v2.1. See the file LICENSE in the top level directory for more
 * details.
 */

/**
 * @defgroup    drivers_nrf24l01p NRF24L01+ driver interface
 * @ingroup     drivers_netdev
 *
 * @brief       Low-level driver for nrf24l01+ transceiver
 *
 * @{
 * @file
 *
 * @author      Hauke Petersen <hauke.petersen@fu-berlin.de>
 * @author      Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
 *
 */

#ifndef NRF24L01P_H
#define NRF24L01P_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdio.h>
#include "periph/gpio.h"
#include "periph/spi.h"

/**
 * @brief   Structure that represents the hardware setup of the nrf24l01+ transceiver.
 */
typedef struct {
    spi_t spi;      /**< SPI device to initialize */
    gpio_t ce;      /**< GPIO pin to initialize as chip enable */
    gpio_t cs;      /**< GPIO pin to initialize as chip select */
    gpio_t irq;     /**< GPIO pin to initialize as interrupt request */
    int listener;   /**< Place to store an ID in */
} nrf24l01p_t;

/**
 * @brief   Defines the address width of the nrf24l01+ transceiver.
 */
typedef enum {
    NRF24L01P_AW_3BYTE, /**< address width is 3 Byte */
    NRF24L01P_AW_4BYTE, /**< address width is 4 Byte */
    NRF24L01P_AW_5BYTE  /**< address width is 5 Byte */
} nrf24l01p_aw_t;


/**
 * @brief   Defines the RF datarate.
 */
typedef enum {
    NRF24L01P_DR_250KBS,/**< datarate is 250 kbps */
    NRF24L01P_DR_1MBS,  /**< datarate is 1 Mbps */
    NRF24L01P_DR_2MBS   /**< datarate is 2 Mbps */
} nrf24l01p_dr_t;

/**
 * @brief   Defines the RF power level.
 */
typedef enum {
    NRF24L01P_PWR_N18DBM = 0,   /**< power is -18dBm */
    NRF24L01P_PWR_N12DBM,       /**< power is -12dBm */
    NRF24L01P_PWR_N6DBM,        /**< power is - 6dBm */
    NRF24L01P_PWR_0DBM          /**< power is   0dBm */
} nrf24l01p_pwr_t;

/**
 * @brief   Defines the datapipe on which the receiver searches for packets.
 */
typedef enum {
    NRF24L01P_PIPE0 = 0,/**< RX pipe 0 */
    NRF24L01P_PIPE1,    /**< RX pipe 1 */
    NRF24L01P_PIPE2,    /**< RX pipe 2 */
    NRF24L01P_PIPE3,    /**< RX pipe 3 */
    NRF24L01P_PIPE4,    /**< RX pipe 4 */
    NRF24L01P_PIPE5     /**< RX pipe 5 */
} nrf24l01p_rx_pipe_t;

/**
 * @brief   Defines the error detection encoding scheme for the nrf24l01p transceiver.
 */
typedef enum {
    NRF24L01P_CRC_1BYTE = 0,    /**< encoding scheme generates 1 Byte redundancy */
    NRF24L01P_CRC_2BYTE,        /**< encoding scheme generates 2 Bytes redundancy */
} nrf24l01p_crc_t;

/**
 * @brief   Defines the automatic retransmission delay defined from end of transmission
 * to start of next treansmission.
 */
typedef enum {
    NRF24L01P_RETR_250US = 0,   /**< retransmit delay is 250us */
    NRF24L01P_RETR_500US,       /**< retransmit delay is 500us */
    NRF24L01P_RETR_750US,       /**< retransmit delay is 750us */
    NRF24L01P_RETR_1000US,      /**< retransmit delay is 1000us */
    NRF24L01P_RETR_1250US,      /**< retransmit delay is 1250us */
    NRF24L01P_RETR_1500US,      /**< retransmit delay is 1500us */
    NRF24L01P_RETR_1750US,      /**< retransmit delay is 1750us */
    NRF24L01P_RETR_2000US,      /**< retransmit delay is 2000us */
    NRF24L01P_RETR_2250US,      /**< retransmit delay is 2250us */
    NRF24L01P_RETR_2500US,      /**< retransmit delay is 2500us */
    NRF24L01P_RETR_2750US,      /**< retransmit delay is 2750us */
    NRF24L01P_RETR_3000US,      /**< retransmit delay is 3000us */
    NRF24L01P_RETR_3250US,      /**< retransmit delay is 3250us */
    NRF24L01P_RETR_3500US,      /**< retransmit delay is 3500us */
    NRF24L01P_RETR_3750US,      /**< retransmit delay is 3750us */
    NRF24L01P_RETR_4000US,      /**< retransmit delay is 4000us */
} nrf24l01p_retransmit_delay_t;

/**
 * @brief   Defines states for the nrf24l01+ transceiver
 */
typedef enum {
    RCV_PKT_NRF24L01P = 0,  /**< transceiver received data */
} nrf24l01p_rx_event_t ;


/**
* @brief   Read one register of the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
* @param[in] reg    Register address to read from.
* @param[in] answer Byte to read.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_read_reg(const nrf24l01p_t *dev, char reg, char *answer);

/**
* @brief   Write one register to the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
* @param[in] reg    Register address to write to.
* @param[in] write  Byte to write.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_write_reg(const nrf24l01p_t *dev, char reg, char write);

/**
* @brief   Initialize the nrf24l01+ transceiver.
*
* @ note
* This function initializes the transceiver so that it is ready to use.
*
* @param[in] dev    Transceiver device to use.
* @param[in] spi    SPI device to use.
* @param[in] ce     GPIO pin to use for chip enable.
* @param[in] csn    GPIO pin to use for chip select.
* @param[in] irq    GPIO pin to use for interrupt request.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_init(nrf24l01p_t *dev, spi_t spi, gpio_t ce, gpio_t csn, gpio_t irq);

/**
* @brief   Power on the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_on(const nrf24l01p_t *dev);

/**
* @brief   Power off the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_off(const nrf24l01p_t *dev);

/**
* @brief   Transmit payload laying in TX FIFO of the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
*
*/
void nrf24l01p_transmit(const nrf24l01p_t *dev);

/**
* @brief   Read payload from RX FIFO of the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
* @param[in] answer Buffer to receive bytes to.
* @param[in] size   Number of bytes to transfer. For nrf24l01+ in general 32.
*
* @return           Number of bytes that were transfered.
* @return           -1 on error.
*/
int nrf24l01p_read_payload(const nrf24l01p_t *dev, char *answer, unsigned int size);

/**
* @brief   Register a given ID to the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
* @param[in] pid    ID to register.
*
*/
void nrf24l01p_register(nrf24l01p_t *dev, unsigned int *pid);

/**
* @brief   Enable dynamic payload for the pipe on give nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
* @param[in] pipe   RX pipe for which dynamic payload is enabled
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_enable_dynamic_payload(const nrf24l01p_t *dev, nrf24l01p_rx_pipe_t pipe);

/**
* @brief   Enable dynamic ack for the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_enable_dynamic_ack(const nrf24l01p_t *dev);

/**
* @brief   Unregister the nrf24l01+ transceiver from his ID.
*
* @param[in] dev    Transceiver device to use.
* @param[in] pid    Actual ID to unregister.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_unregister(nrf24l01p_t *dev, unsigned int pid);

/**
* @brief   Get ID from the nrf24l01p transceiver.
*
* @param[in] dev    Transceiver device to use.
* @param[in] pid    Transceiver ID.
*
*/
void nrf24l01p_get_id(const nrf24l01p_t *dev, unsigned int *pid);

/**
* @brief   Start searching packets while in RX mode.
*
* @param[in] dev    Transceiver device to use.
*
*/
void nrf24l01p_start(const nrf24l01p_t *dev);

/**
* @brief   Stop searching packets while in RX mode.
*
* @param[in] dev    Transceiver device to use.
*
*/
void nrf24l01p_stop(const nrf24l01p_t *dev);

/**
* @brief   Preload TX FIFO with payload to transmit.
*
* @param[in] dev    Transceiver device to use.
* @param[in] data   Buffer to preload.
* @param[in] size   Number of bytes in buffer. For nrf24l01+ e.g. 32
*
* @return           Number of bytes that were transfered.
* @return           -1 on error.
*/
int nrf24l01p_preload(const nrf24l01p_t *dev, char *data, unsigned int size);

/**
* @brief   Set the RF channel for the nrf24l01+ transceiver.
*
* @note
* To ensure non-overlapping channels in 2Mbps mode, don't use directly
* neighbouring channels in this mode.
*
* @param[in] dev    Transceiver device to use.
* @param[in] chan   Buffer to preload.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_set_channel(const nrf24l01p_t *dev, uint8_t chan);

/**
* @brief   Set the address width for the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
* @param[in] aw     Address width (type nrf24l01p_aw_t).
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_set_address_width(const nrf24l01p_t *dev, nrf24l01p_aw_t aw);

/**
* @brief   Set the RX payload width for the nrf24l01+ transceiver
*
* @ note
* This function sets the payload width for one packet. If the maximum of 32 bytes is
* exeeded, this value is set to 32.
*
* @param[in] dev    Transceiver device to use.
* @param[in] pipe   RX pipe to set the payload width.
* @param[in] width  Numer of bytes per packet in RX payload.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_set_payload_width(const nrf24l01p_t *dev, nrf24l01p_rx_pipe_t pipe, char width);

/**
* @brief   Set the TX address for the nrf24l01+ transceiver (byte array).
*
* @note
* You can either use this function and give it a pointer to a byte array which
* holds the address to set, or use "nrf24l01p_set_tx_address_long" which requires
* a uint64_t which holds the address in the LSBs.
*
* @param[in] dev    Transceiver device to use.
* @param[in] saddr  Byte array which holds the TX address.
* @param[in] length Number of bytes in address array.
*
* @return           Address length on success.
* @return           -1 on error.
*/
int nrf24l01p_set_tx_address(const nrf24l01p_t *dev, const char *saddr, unsigned int length);

/**
* @brief   Set the TX address for the nrf24l01+ transceiver (long int).
*
* @param[in] dev    Transceiver device to use.
* @param[in] saddr  Long integer which holds the TX address in LSBs.
* @param[in] length Number of relevant bytes in uint64_t.
*
* @return           Address length on success.
* @return           -1 on error.
*/
int nrf24l01p_set_tx_address_long(const nrf24l01p_t *dev, uint64_t saddr, unsigned int length);

/**
* @brief   Set the RX address for the nrf24l01+ transceiver (byte array).
*
* @note
* You can either use this function and give it a pointer to a byte array which
* holds the address to set, or use "nrf24l01p_set_rx_address_long" which requires
* a uint64_t which holds the address in the LSBs.
*
* @param[in] dev    Transceiver device to use.
* @param[in] pipe   RX pipe to set the address.
* @param[in] saddr  Byte array which holds the RX address.
* @param[in] length Number of bytes in address array.
*
* @return           Address length on success.
* @return           -1 on error.
*/
int nrf24l01p_set_rx_address(const nrf24l01p_t *dev, nrf24l01p_rx_pipe_t pipe, const char *saddr, unsigned int length);

/**
* @brief   Set the RX address for the nrf24l01+ transceiver (long int).
*
* @param[in] dev    Transceiver device to use.
* @param[in] pipe   RX pipe to set the address.
* @param[in] saddr  Long integer which holds the RX address in LSBs.
* @param[in] length Number of relevant bytes in uint64_t.
*
* @return           Address length on success.
* @return           -1 on error.
*/
int nrf24l01p_set_rx_address_long(const nrf24l01p_t *dev, nrf24l01p_rx_pipe_t pipe, uint64_t saddr, unsigned int length);

/**
* @brief   Get the TX address for the nrf24l01+ transceiver (long int).
*
* @param[in] dev    Transceiver device to use.
*
* @return           TX address of the nrf24l01+ transceiver.
* @return           -1 on error.
*/
uint64_t nrf24l01p_get_tx_address_long(const nrf24l01p_t *dev);

/**
* @brief   Get the RX address for the nrf24l01+ transceiver (long int).
*
* @param[in] dev    Transceiver device to use.
* @param[in] pipe   RX pipe to get the address from.
*
* @return           RX address of the nrf24l01+ transceiver.
* @return           -1 on error.
*/
uint64_t nrf24l01p_get_rx_address_long(const nrf24l01p_t *dev, nrf24l01p_rx_pipe_t pipe);

/**
* @brief   Get the TX address for the nrf24l01+ transceiver (long int).
*
* @note
* If you chose 2Mbps you should not allocate directly neighboring RF channels.
*
* @param[in] dev    Transceiver device to use.
* @param[in] dr     Datarate (of type nrf24l01p_dr_t).
*
* @return           1 on success.
* @return           -1 on error.
*/
int nrf24l01p_set_datarate(const nrf24l01p_t *dev, nrf24l01p_dr_t dr);

/**
* @brief   Get the status (register) of the nrf24l01+ transceiver device.
*
* @param[in] dev    Transceiver device to use.s of the.
*
* @return           Value of the status register.
*/
int nrf24l01p_get_status(const nrf24l01p_t *dev);

/**
* @brief   Set the transmit power for the nrf24l01+ transceiver device.
*
* @note
* This function rounds the input values to the nearest possible setting.
*
* @param[in] dev    Transceiver device to use.
* @param[in] pwr    TX power for the nrf24l01p transceiver.
*
* @return           1 on success.
* @return           -1 on error.
*/
int nrf24l01p_set_power(const nrf24l01p_t *dev, int pwr);

/**
* @brief   Get the transmit power for the nrf24l01+ transceiver device.
*
* @param[in] dev    Transceiver device to use.
*
* @return           TX power value of the nrf24l01+ transceiver.
*/
int nrf24l01p_get_power(const nrf24l01p_t *dev);

/**
* @brief   Set the nrf24l01+ into TX mode.
*
* @param[in] dev    Transceiver device to use.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_set_txmode(const nrf24l01p_t *dev);

/**
* @brief   Set the nrf24l01+ into RX mode.
*
* @param[in] dev    Transceiver device to use.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_set_rxmode(const nrf24l01p_t *dev);

/**
* @brief   Reset all interrupts on the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
*
* @return           1 on success.
* @return           -1 on error.
*/
int nrf24l01p_reset_all_interrupts(const nrf24l01p_t *dev);

/**
* @brief   Reset interrupts on the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
* @param[in] intrs  Interrupt mask to reset
*
* @return           1 on success.
* @return           -1 on error.
*/
int nrf24l01p_reset_interrupts(const nrf24l01p_t *dev, char intrs);

/**
* @brief   Mask one interrupt on the nrf24l01+ transceiver.
*
* @note
* There are three interrupts on the nrf24l01+ which can be masked:
* "MASK_RX_DR", "MASK_TX_DS" and "MASK_MAX_RT". Theay are defined
* in "include/nrf24l01p_settings.h".
*
* @param[in] dev    Transceiver device to use.
* @param[in] intr   Transceiver device to use.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_mask_interrupt(const nrf24l01p_t *dev, char intr);

/**
* @brief   Unmask one interrupt on the nrf24l01+ transceiver.
*
* @note
* There are three interrupts on the nrf24l01+ which can be unmasked:
* "MASK_RX_DR", "MASK_TX_DS" and "MASK_MAX_RT". Theay are defined
* in "include/nrf24l01p_settings.h".
*
* @param[in] dev    Transceiver device to use.
* @param[in] intr   Transceiver device to use.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_unmask_interrupt(const nrf24l01p_t *dev, char intr);

/**
* @brief   Enable RX datapipe on the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
* @param[in] pipe   RX pipe to enable.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_enable_pipe(const nrf24l01p_t *dev, nrf24l01p_rx_pipe_t pipe);

/**
* @brief   Disable RX datapipe on the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
* @param[in] pipe   RX pipe to disable.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_disable_pipe(const nrf24l01p_t *dev, nrf24l01p_rx_pipe_t pipe);

/**
* @brief   Disable CRC error detection on the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
*
* @return           0.
*/
int nrf24l01p_disable_crc(const nrf24l01p_t *dev);

/**
* @brief   Enable CRC error detection on the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
* @param[in] crc    Length of cyclic redundancy check (type nrf24l01p_crc_t).
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_enable_crc(const nrf24l01p_t *dev, nrf24l01p_crc_t crc);

/**
* @brief   Setup and enable automatic ACK and retransmission on the nrf24l01+ transceiver.
*
* @note
* This function enables automatic acknowledgement for a given RX data pipe and also sets up the
* mautomatic retransmission behavior.
*
* @param[in] dev    Transceiver device to use.
* @param[in] pipe   RX pipe to setup auto ack.
* @param[in] delay_retrans    Automatic retransmission delay
                              (type nrf24l01p_retransmit_delay_t)
* @param[in] count_retrans    Auto retransmit count.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_setup_auto_ack(const nrf24l01p_t *dev, nrf24l01p_rx_pipe_t pipe, nrf24l01p_retransmit_delay_t delay_retrans, char count_retrans);

/**
* @brief   Disable automatic ACK on the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_disable_all_auto_ack(const nrf24l01p_t *dev);

/**
* @brief   Flush TX FIFO on the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_flush_tx_fifo(const nrf24l01p_t *dev);

/**
* @brief   Flush RX FIFO on the nrf24l01+ transceiver.
*
* @param[in] dev    Transceiver device to use.
*
* @return           0 on success.
* @return           -1 on error.
*/
int nrf24l01p_flush_rx_fifo(const nrf24l01p_t *dev);

/**
* @brief   Callback that is called when interrupt occurs on interrupt
* pin from the nrf24l01+ transceiver.
*
* @param[in] arg    Used to pass transceiver device "dev".
*/
void nrf24l01p_rx_cb(void *arg);

#ifdef __cplusplus
}
#endif

#endif /* NRF24L01P_H */
/** @} */