nrf24l01+ Library
nrf24l01.h File Reference

Library for use of the nrf24l01+ radio module with a PIC32. More...

#include <plib.h>
#include "inttypes.h"

Go to the source code of this file.

Macros

#define _SUPPRESS_PLIB_WARNING   1
 
#define _csn   LATBbits.LATB9
 
#define TRIS_csn   TRISBbits.TRISB9
 
#define _ce   LATBbits.LATB8
 
#define TRIS_ce   TRISBbits.TRISB8
 
#define nrf24l01_R_REGISTER   0x00
 
#define nrf24l01_W_REGISTER   0x20
 
#define nrf24l01_R_REGISTER_WID   0x61
 
#define nrf24l01_R_RX_PL_WID   0x60
 
#define nrf24l01_R_RX_PAYLOAD   0x61
 
#define nrf24l01_W_TX_PAYLOAD   0xA0
 
#define nrf24l01_FLUSH_TX   0xE1
 
#define nrf24l01_FLUSH_RX   0xE2
 
#define nrf24l01_REUSE_TX_PL   0xE3
 
#define nrf24l01_NOP   0xFF
 
#define nrf24l01_CONFIG   0x00
 
#define nrf24l01_EN_AA   0x01
 
#define nrf24l01_EN_RXADDR   0x02
 
#define nrf24l01_SETUP_AW   0x03
 
#define nrf24l01_SETUP_RETR   0x04
 
#define nrf24l01_RF_CH   0x05
 
#define nrf24l01_RF_SETUP   0x06
 
#define nrf24l01_STATUS   0x07
 
#define nrf24l01_OBSERVE_TX   0x08
 
#define nrf24l01_RPD   0x09
 
#define nrf24l01_RX_ADDR_P0   0x0A
 
#define nrf24l01_RX_ADDR_P1   0x0B
 
#define nrf24l01_RX_ADDR_P2   0x0C
 
#define nrf24l01_RX_ADDR_P3   0x0D
 
#define nrf24l01_RX_ADDR_P4   0x0E
 
#define nrf24l01_RX_ADDR_P5   0x0F
 
#define nrf24l01_TX_ADDR   0x10
 
#define nrf24l01_RX_PW_P0   0x11
 
#define nrf24l01_RX_PW_P1   0x12
 
#define nrf24l01_RX_PW_P2   0x13
 
#define nrf24l01_RX_PW_P3   0x14
 
#define nrf24l01_RX_PW_P4   0x15
 
#define nrf24l01_RX_PW_P5   0x16
 
#define nrf24l01_FIFO_STATUS   0x17
 
#define nrf24l01_DYNPD   0x1C
 
#define nrf24l01_FEATURE   0x1D
 
#define nrf24l01_CONFIG_RESERVED   0x80
 
#define nrf24l01_CONFIG_MASK_RX_DR   0x40
 
#define nrf24l01_CONFIG_MASK_TX_DS   0x20
 
#define nrf24l01_CONFIG_MASK_MAX_RT   0x10
 
#define nrf24l01_CONFIG_EN_CRC   0x08
 
#define nrf24l01_CONFIG_CRCO   0x04
 
#define nrf24l01_CONFIG_PWR_UP   0x02
 
#define nrf24l01_CONFIG_PRIM_RX   0x01
 
#define nrf24l01_EN_AA_RESERVED   0xC0
 
#define nrf24l01_EN_AA_ENAA_ALL   0x3F
 
#define nrf24l01_EN_AA_ENAA_P5   0x20
 
#define nrf24l01_EN_AA_ENAA_P4   0x10
 
#define nrf24l01_EN_AA_ENAA_P3   0x08
 
#define nrf24l01_EN_AA_ENAA_P2   0x04
 
#define nrf24l01_EN_AA_ENAA_P1   0x02
 
#define nrf24l01_EN_AA_ENAA_P0   0x01
 
#define nrf24l01_EN_AA_ENAA_NONE   0x00
 
#define nrf24l01_EN_RXADDR_RESERVED   0xC0
 
#define nrf24l01_EN_RXADDR_ERX_ALL   0x3F
 
#define nrf24l01_EN_RXADDR_ERX_P5   0x20
 
#define nrf24l01_EN_RXADDR_ERX_P4   0x10
 
#define nrf24l01_EN_RXADDR_ERX_P3   0x08
 
#define nrf24l01_EN_RXADDR_ERX_P2   0x04
 
#define nrf24l01_EN_RXADDR_ERX_P1   0x02
 
#define nrf24l01_EN_RXADDR_ERX_P0   0x01
 
#define nrf24l01_EN_RXADDR_ERX_NONE   0x00
 
#define nrf24l01_SETUP_AW_RESERVED   0xFC
 
#define nrf24l01_SETUP_AW   0x03
 
#define nrf24l01_SETUP_AW_5BYTES   0x03
 
#define nrf24l01_SETUP_AW_4BYTES   0x02
 
#define nrf24l01_SETUP_AW_3BYTES   0x01
 
#define nrf24l01_SETUP_AW_ILLEGAL   0x00
 
#define nrf24l01_SETUP_RETR_ARD   0xF0
 
#define nrf24l01_SETUP_RETR_ARD_4000   0xF0
 
#define nrf24l01_SETUP_RETR_ARD_3750   0xE0
 
#define nrf24l01_SETUP_RETR_ARD_3500   0xD0
 
#define nrf24l01_SETUP_RETR_ARD_3250   0xC0
 
#define nrf24l01_SETUP_RETR_ARD_3000   0xB0
 
#define nrf24l01_SETUP_RETR_ARD_2750   0xA0
 
#define nrf24l01_SETUP_RETR_ARD_2500   0x90
 
#define nrf24l01_SETUP_RETR_ARD_2250   0x80
 
#define nrf24l01_SETUP_RETR_ARD_2000   0x70
 
#define nrf24l01_SETUP_RETR_ARD_1750   0x60
 
#define nrf24l01_SETUP_RETR_ARD_1500   0x50
 
#define nrf24l01_SETUP_RETR_ARD_1250   0x40
 
#define nrf24l01_SETUP_RETR_ARD_1000   0x30
 
#define nrf24l01_SETUP_RETR_ARD_750   0x20
 
#define nrf24l01_SETUP_RETR_ARD_500   0x10
 
#define nrf24l01_SETUP_RETR_ARD_250   0x00
 
#define nrf24l01_SETUP_RETR_ARC   0x0F
 
#define nrf24l01_SETUP_RETR_ARC_15   0x0F
 
#define nrf24l01_SETUP_RETR_ARC_14   0x0E
 
#define nrf24l01_SETUP_RETR_ARC_13   0x0D
 
#define nrf24l01_SETUP_RETR_ARC_12   0x0C
 
#define nrf24l01_SETUP_RETR_ARC_11   0x0B
 
#define nrf24l01_SETUP_RETR_ARC_10   0x0A
 
#define nrf24l01_SETUP_RETR_ARC_9   0x09
 
#define nrf24l01_SETUP_RETR_ARC_8   0x08
 
#define nrf24l01_SETUP_RETR_ARC_7   0x07
 
#define nrf24l01_SETUP_RETR_ARC_6   0x06
 
#define nrf24l01_SETUP_RETR_ARC_5   0x05
 
#define nrf24l01_SETUP_RETR_ARC_4   0x04
 
#define nrf24l01_SETUP_RETR_ARC_3   0x03
 
#define nrf24l01_SETUP_RETR_ARC_2   0x02
 
#define nrf24l01_SETUP_RETR_ARC_1   0x01
 
#define nrf24l01_SETUP_RETR_ARC_0   0x00
 
#define nrf24l01_RF_CH_RESERVED   0x80
 
#define nrf24l01_RD_SETUP_CONT_WAVE   0x80
 
#define nrf24l01_RF_SETUP_RESERVED   0xE0
 
#define nrf24l01_RF_SETUP_PLL_LOCK   0x10
 
#define nrf24l01_RF_SETUP_RF_DR   0x08
 
#define nrf24l01_RF_SETUP_RF_PWR   0x06
 
#define nrf24l01_RF_SETUP_RF_PWR_0   0x06
 
#define nrf24l01_RF_SETUP_RF_PWR_6   0x04
 
#define nrf24l01_RF_SETUP_RF_PWR_12   0x02
 
#define nrf24l01_RF_SETUP_RF_PWR_18   0x00
 
#define nrf24l01_RF_SETUP_LNA_HCURR   0x01
 
#define nrf24l01_STATUS_RESERVED   0x80
 
#define nrf24l01_STATUS_RX_DR   0x40
 
#define nrf24l01_STATUS_TX_DS   0x20
 
#define nrf24l01_STATUS_MAX_RT   0x10
 
#define nrf24l01_STATUS_RX_P_NO   0x0E
 
#define nrf24l01_STATUS_RX_P_NO_RX_FIFO_NOT_EMPTY   0x0E
 
#define nrf24l01_STATUS_RX_P_NO_UNUSED   0x0C
 
#define nrf24l01_STATUS_RX_P_NO_5   0x0A
 
#define nrf24l01_STATUS_RX_P_NO_4   0x08
 
#define nrf24l01_STATUS_RX_P_NO_3   0x06
 
#define nrf24l01_STATUS_RX_P_NO_2   0x04
 
#define nrf24l01_STATUS_RX_P_NO_1   0x02
 
#define nrf24l01_STATUS_RX_P_NO_0   0x00
 
#define nrf24l01_STATUS_TX_FULL   0x01
 
#define nrf24l01_OBSERVE_TX_PLOS_CNT   0xF0
 
#define nrf24l01_OBSERVE_TX_ARC_CNT   0x0F
 
#define nrf24l01_CD_RESERVED   0xFE
 
#define nrf24l01_CD_CD   0x01
 
#define nrf24l01_RX_PW_P0_RESERVED   0xC0
 
#define nrf24l01_RX_PW_P0_RESERVED   0xC0
 
#define nrf24l01_RX_PW_P1_RESERVED   0xC0
 
#define nrf24l01_RX_PW_P2_RESERVED   0xC0
 
#define nrf24l01_RX_PW_P3_RESERVED   0xC0
 
#define nrf24l01_RX_PW_P4_RESERVED   0xC0
 
#define nrf24l01_RX_PW_P5_RESERVED   0xC0
 
#define nrf24l01_FIFO_STATUS_RESERVED   0x8C
 
#define nrf24l01_FIFO_STATUS_TX_REUSE   0x40
 
#define nrf24l01_FIFO_STATUS_TX_FULL   0x20
 
#define nrf24l01_FIFO_STATUS_TX_EMPTY   0x10
 
#define nrf24l01_FIFO_STATUS_RX_FULL   0x02
 
#define nrf24l01_FIFO_STATUS_RX_EMPTY   0x01
 
#define nrf24l01_DR_LOW   0x20
 
#define nrf24l01_DR_MED   0x00
 
#define nrf24l01_DR_HIGH   0x08
 
#define nrf24l01_SEND_CLOCK   0x00
 
#define PWR_DOWN   0
 
#define STANDBY_1   1
 
#define RX_MODE   2
 
#define TX_MODE   3
 
#define PBCLK   40000000
 
#define dTime_ms   PBCLK/2000
 
#define dTime_us   PBCLK/2000000
 

Functions

void init_SPI ()
 Set up SPI for the radio.
 
void nrf_setup ()
 Sets up the radio, SPI, and interrupts. Also resets all radio registers to their default values.
 
void nrf_read_reg (char reg, char *buff, int len)
 Read a register and store the data in an array. Can be multiple bytes of data. More...
 
void nrf_write_reg (char reg, char *data, char len)
 Write to a register from an array. Can be multiple bytes of data. More...
 
void nrf_flush_tx ()
 Flush the TX FIFO.
 
void nrf_flush_rx ()
 Flush the RX FIFO.
 
int nrf_get_payload_width ()
 Get the width of the top payload in the RX FIFO. More...
 
void nrf_write_payload (char *data, char len)
 Write a payload to the TX FIFO. More...
 
void nrf_read_payload (char *buff)
 Read a payload from the RX FIFO. More...
 
int nrf_get_payload (char *buff, char len)
 Read a received payload. More...
 
int nrf_payload_available ()
 Check if a payload is available to be read. More...
 
int nrf_get_pipe ()
 Get the number of the pipe the most recent payload was received on. More...
 
int nrf_get_width ()
 Get the width of the most recently received payload. More...
 
void nrf_pwrup ()
 Sets the power up bit in the status register in order to leave the power down state.
 
void nrf_pwrdown ()
 Clears the power up bit in the status register in order to enter the power down state.
 
void nrf_state_pwr_down ()
 Put the radio in the power down state.
 
void nrf_state_standby_1 ()
 Put the radio in the standby 1 state.
 
void nrf_state_rx_mode ()
 Put the radio in the rx mode state.
 
void nrf_set_prim_rx ()
 Set the PRIM_RX bit in the CONFIG register.
 
void nrf_clear_prim_rx ()
 Clear the PRIM_RX bit in the CONFIG register.
 
void nrf_set_transmit_pwr (char power)
 Set power of transmitter. More...
 
void nrf_set_transmit_rate (char rate)
 Set data rate. More...
 
void nrf_set_ard (char ard)
 Set the auto retransmit delay. More...
 
void nrf_set_arc (char arc)
 Set the auto retransmit count. More...
 
void nrf_set_rf_ch (char ch)
 Set the RF frequency the radio will operate at. More...
 
char nrf_received_pipe_num ()
 Returns the pipe data is available in. More...
 
void nrf_set_address_width (char width)
 Set the address width of RX and TX pipes. More...
 
void nrf_start_cont_wave (char pwr)
 Send a constant carrier wave out at specified power. More...
 
void nrf_stop_cont_wave ()
 Stop sending the carrier wave. More...
 
char nrf_received_pwr ()
 Check the power of the signal the nrf42l01 is receiving. More...
 
void nrf_en_aa (int pipe)
 Enable auto-acknowledge for a pipe. More...
 
void nrf_dis_aa (int pipe)
 Disable auto-acknowledge for a pipe. More...
 
void nrf_en_rxaddr (int pipe)
 Enable a pipe to receive packets. More...
 
void nrf_dis_rxaddr (int pipe)
 Disable a pipe from receiving packets. More...
 
void nrf_set_pw (char width, int pipe)
 Set the width received static payloads should be. More...
 
void nrf_en_dpl (int pipe)
 Enable dynamic payload length for a pipe. More...
 
void nrf_dis_dpl (int pipe)
 Disable dynamic payload length for a pipe. More...
 
void nrf_en_dyn_ack ()
 Enable dynamic auto-acknowledgements. More...
 
void nrf_dis_dyn_ack ()
 Disable dynamic auto-acknowledgements. More...
 
int nrf_set_rx_addr (int pipe, uint64_t address, int len)
 Set the address of a pipe. More...
 
void nrf_set_tx_addr (uint64_t address)
 Set the address for transmitting. More...
 
void nrf_reset ()
 Resets all registers to their default values as listed on the datasheet. More...
 
int nrf_send_payload (char *data, char len)
 Send a payload over the radio. More...
 

Detailed Description

Library for use of the nrf24l01+ radio module with a PIC32.

Author
Douglas Katz and Frederick Kummer
Date
February 7 2016

Function Documentation

void nrf_dis_dyn_ack ( )

Disable dynamic auto-acknowledgements.

Disables sending payloads without using auto-acknowlegment without disabling the auto-acknowledge setting on the transmitter or receiver.

void nrf_en_dyn_ack ( )

Enable dynamic auto-acknowledgements.

Enables sending payloads without using auto-acknowlegment without disabling the auto-acknowledge setting on the transmitter or receiver. The SENDNOACK function can then be used to do this.

void nrf_read_payload ( char *  buff)

Read a payload from the RX FIFO.

Parameters
buffPointer to array where data will be written.
void nrf_read_reg ( char  reg,
char *  buff,
int  len 
)

Read a register and store the data in an array. Can be multiple bytes of data.

Parameters
regThe register to read from. Use constants in nrf24l01.h
buffPointer to the array the data will be stored in. LSB first.
lenHow many bytes of data need to be read. (1-5 bytes)
void nrf_set_ard ( char  ard)

Set the auto retransmit delay.

Set how long the nrf24l01 should wait between retransmitting packets after not receiving an acknowledgement packet. Delay is defined as the end of one transmission to the start of the next. The delay is set according to the equation, delay = 250 + ard * 250 (us).

Parameters
ardThe length of auto retransmit delay to be set.
void nrf_write_payload ( char *  data,
char  len 
)

Write a payload to the TX FIFO.

Parameters
dataPointer to data to be written.
lenHow many bytes of data will be written. (1-32 bytes)
void nrf_write_reg ( char  reg,
char *  data,
char  len 
)

Write to a register from an array. Can be multiple bytes of data.

Parameters
regThe register to read from. Use constants in nrf24l01.h
buffPointer to the array data will be read from. LSB first.
lenHow many bytes of data to be written. (1-5 bytes)