nrf24L01  1.0
nrf24L01 library
 All Files Functions Variables Typedefs Enumerations Enumerator Macros
nrf24L01.c File Reference

Driver for Nordic NRF24L01p with Xmega. More...

#include "nrf24spiXM2.h"
#include "nrf24L01.h"
#include <string.h>
#include <stdio.h>

Macros

#define ERX_P5   5
 
#define ERX_P4   4
 
#define ERX_P3   3
 
#define ERX_P2   2
 
#define ERX_P1   1
 
#define ERX_P0   0
 

Functions

void nrfBegin (void)
 Begin operation of NRF24L01p. More...
 
uint8_t nrfReadRegisterMulti (uint8_t reg, uint8_t *buf, uint8_t len)
 Read multiple bytes from a register. More...
 
uint8_t nrfReadRegister (uint8_t reg)
 Read a byte from a register. More...
 
uint8_t nrfWriteRegisterMulti (uint8_t reg, const uint8_t *buf, uint8_t len)
 Write multiple bytes to a register. More...
 
uint8_t nrfWriteRegister (uint8_t reg, uint8_t value)
 Write a byte to a register. More...
 
uint8_t nrfWritePayload (const void *buf, uint8_t len, const uint8_t writeType)
 Write the transmit payload. More...
 
void nrfWriteAckPayload (uint8_t pipe, uint8_t *buf, uint8_t len)
 Write an ack payload for the specified pipe. More...
 
uint8_t nrfReadPayload (void *buf, uint8_t len)
 Write the receive payload. More...
 
uint8_t nrfRead (void *buf, uint8_t len)
 Read the payload. More...
 
uint8_t nrfFlushRx (void)
 Empty the receive fifo. More...
 
uint8_t nrfFlushTx (void)
 Empty the transmit fifo. More...
 
void nrfStartListening (void)
 Start listening on the pipes opened for reading. More...
 
void nrfStopListening (void)
 Stop listening for incoming messages. More...
 
uint8_t nrfWrite (uint8_t *buf, uint8_t len)
 Write to the open writing pipe. More...
 
uint8_t nrfWaitForAck (void)
 Wait for acknowledge. More...
 
void nrfStartWrite (const void *buf, uint8_t len, uint8_t multicast)
 Write to open writing pipe. More...
 
void nrfPowerDown (void)
 Enter low-power mode. More...
 
void nrfPowerUp (void)
 Leave low-power mode - making radio more responsive. More...
 
uint8_t nrfAvailable (uint8_t *pipe_num)
 Test whether there are bytes available to be read. More...
 
void nrfWhatHappened (uint8_t *tx_ok, uint8_t *tx_fail, uint8_t *rx_ready)
 Call this to find out which interrupt occured. More...
 
void nrfOpen64WritingPipe (uint64_t value)
 Open a pipe for writing. More...
 
void nrfOpen64ReadingPipe (uint8_t child, uint64_t address)
 Open a pipe for reading. More...
 
void nrfOpenWritingPipe (uint8_t *address)
 Open a pipe for writing. More...
 
void nrfOpenReadingPipe (uint8_t child, uint8_t *address)
 Open a pipe for reading. More...
 
void nrfToggleFeatures (void)
 Turn on or off the special features. More...
 
void nrfEnableDynamicPayloads (void)
 Enable dynamically-sized payloads. More...
 
void nrfEnableAckPayload (void)
 Enable custom payloads on the acknowledge packets. More...
 
uint8_t nrfGetDynamicPayloadSize (void)
 Get Dynamic Payload Size. More...
 
uint8_t nrfGetPayloadSize (void)
 Get fixed payload size. More...
 
uint8_t nrfGetStatus (void)
 Get fixed payload size. More...
 
void nrfSetChannel (uint8_t channel)
 
uint8_t nrfGetChannel (void)
 
void nrfSetPayloadSize (uint8_t size)
 
uint8_t nrfIsPVariant (void)
 Determine whether the hardware is an nRF24L01+ or not. More...
 
void nrfSetAutoAck (uint8_t enable)
 Enable or disable auto-acknowlede packets. More...
 
void nrfSetAutoAckPipe (uint8_t pipe, uint8_t enable)
 Enable or disable auto-acknowlede packets for a distinct pipe. More...
 
uint8_t nrfTestCarrier (void)
 Test whether there was a carrier on the line for the previous listening period. More...
 
uint8_t nrfTestRPD (void)
 Test whether a signal (carrier or otherwise) greater than or equal to -64dBm is present on the channel. More...
 
void nrfSetPALevel (nrf_rf_setup_pwr_t level)
 Set Power Amplifier (PA) level. More...
 
nrf_rf_setup_pwr_t nrfGetPALevel (void)
 Gets the current PA level. More...
 
uint8_t nrfSetDataRate (nrf_rf_setup_rf_dr_t speed)
 Set the transmission data rate. More...
 
nrf_rf_setup_rf_dr_t nrfGetDataRate (void)
 Get the transmission data rate. More...
 
void nrfSetCRCLength (nrf_config_crc_t length)
 Sets the CRC length. More...
 
nrf_config_crc_t nrfGetCRCLength (void)
 Get the CRC length. More...
 
void nrfDisableCRC (void)
 Disbale CRC. More...
 
void nrfSetRetries (uint8_t delay, uint8_t retries)
 Sets the number of retries and the delay between the retries. More...
 
uint16_t nrfGetMaxTimeout (void)
 Calculate the maximum timeout in us based on current configuration. More...
 
void nrfClearInterruptBits (void)
 Clear Interrupt Bits. More...
 
uint8_t nrfVerifySPIConnection (void)
 Verify SPI Interface. More...
 

Variables

uint8_t p_variant = 1
 Global variables and constants. More...
 
uint8_t fixed_payload_size = NRF_MAX_PAYLOAD_SIZE
 Size of a fixed payload. More...
 
uint8_t dynamic_payloads_enabled = 0
 Whether dynamic payloads are enabled. More...
 
uint8_t pipe0_reading_address [5] = {0,0,0,0,0}
 Last address set on pipe 0 for reading. More...
 
uint8_t addr_width = 5
 The address width to use - 3,4 or 5 bytes. More...
 

Detailed Description

Driver for Nordic NRF24L01p with Xmega.

Author
Wim Dolman (w.e.dolman@hva.nl)
Date
19-02-2016
Version
1.0

This file contains the routines for interfacing a Nordic NRF24L01p and a Xmega. The accompanying files nrf24spiXM2.c and nrf24spiXM2.h comtains the driverroutines for the HvA Xmegaboard version 2.

This driver is based on:

Other references:

Macro Definition Documentation

#define ERX_P0   0
#define ERX_P1   1
#define ERX_P2   2
#define ERX_P3   3
#define ERX_P4   4
#define ERX_P5   5

Function Documentation

uint8_t nrfAvailable ( uint8_t *  pipe_num)

Test whether there are bytes available to be read.

Call nrfAvaliable(NULL) if you're not interested in the pipe number Call nrfAvaliable(&pipe) puts the pipenumber in pipe

Parameters
[out]pipe_numPointer to pipenumber, where it writes the pipenumber with a payload that is available
Returns
64 (true) if there is a payload available, 0 (false) if none is
void nrfBegin ( void  )

Begin operation of NRF24L01p.

This function is used at the inilialzation of NRF24L01p.

Returns
void
void nrfClearInterruptBits ( void  )

Clear Interrupt Bits.

void nrfDisableCRC ( void  )

Disbale CRC.

void nrfEnableAckPayload ( void  )

Enable custom payloads on the acknowledge packets.

Ack payloads are a handy way to return data back to senders without manually changing the radio modes on both units.

void nrfEnableDynamicPayloads ( void  )

Enable dynamically-sized payloads.

This way you don't always have to send large packets. It enables dynamic payloads on ALL pipes.

uint8_t nrfFlushRx ( void  )

Empty the receive fifo.

Returns
Current value of status register
uint8_t nrfFlushTx ( void  )

Empty the transmit fifo.

Returns
Current value of status register
uint8_t nrfGetChannel ( void  )
nrf_config_crc_t nrfGetCRCLength ( void  )

Get the CRC length.

The CRC correspond to the following lengths respectively:

  • Disable CRC, use NRF_CONFIG_CRC_DISABLED_gc
  • CRC 1 byte, use NRF_CONFIG_CRC_8_gc
  • CRC 2 bytes, use NRF_CONFIG_CRC_16_gc
Returns
The CRC-configuration (is one of the group configurations)
nrf_rf_setup_rf_dr_t nrfGetDataRate ( void  )

Get the transmission data rate.

The data rates correspond to the following speeds respectively:

  • 250kbps, use NRF_RF_SETUP_RF_DR_250K_gc (only for nRF24L01+)
  • 1Mpbs, use NRF_RF_SETUP_RF_DR_1M_gc
  • 2Mpbs, use NRF_RF_SETUP_RF_DR_2M_gc
Warning
Setting RF24_250KBPS will fail for a non nRF24L01+
Returns
Speed (is one of the group configurations)
uint8_t nrfGetDynamicPayloadSize ( void  )

Get Dynamic Payload Size.

For dynamic payloads, this returns the size of the payload

Returns
Payload length of last-received dynamic payload
uint16_t nrfGetMaxTimeout ( void  )

Calculate the maximum timeout in us based on current configuration.

This depends on the number of retries en the delays betweeen them. The maximum timeout is delay * (retries+1) us. It is a value between 250 us and 64000 us.

Returns
maximum timeout in us
nrf_rf_setup_pwr_t nrfGetPALevel ( void  )

Gets the current PA level.

The power levels correspond to the following output levels respectively:

  • minimum is -18dBm, use NRF_RF_SETUP_PWR_18DBM_gc
  • low is -12dBm, use NRF_RF_SETUP_PWR_12DBM_gc
  • high is -6dBm, use NRF_RF_SETUP_PWR_6DBM_gc
  • maximum is -0dBm, use NRF_RF_SETUP_PWR_0DBM_gc
Returns
Level (is one of the group configurations)
uint8_t nrfGetPayloadSize ( void  )

Get fixed payload size.

Returns
Fixed payload size
uint8_t nrfGetStatus ( void  )

Get fixed payload size.

Returns
Fixed payload size
uint8_t nrfIsPVariant ( void  )

Determine whether the hardware is an nRF24L01+ or not.

Returns
1 (true) if the hardware is nRF24L01+ (or compatible) and 0 (false) if its not.
void nrfOpen64ReadingPipe ( uint8_t  child,
uint64_t  address 
)

Open a pipe for reading.

Obsolete, replaced by

See Also
nrfOpenWritingPipe
void nrfOpen64WritingPipe ( uint64_t  value)

Open a pipe for writing.

Obsolete, replaced by

See Also
nrfOpenWritingPipe
void nrfOpenReadingPipe ( uint8_t  child,
uint8_t *  address 
)

Open a pipe for reading.

Up to 6 pipes can be open for reading at once. Open all the required reading pipes, and then call startListening().

Parameters
childPipe number (0-5) to read from.
addressPointer to address of the pipe to open.
void nrfOpenWritingPipe ( uint8_t *  address)

Open a pipe for writing.

The address is a pointer to byte array with 3-5 bytes. Only one writing pipe can be open at once, but you can change the address you'll write to.

Parameters
addressPointer to address of the pipe to open.
void nrfPowerDown ( void  )

Enter low-power mode.

To return to normal power mode, either nrfWrite() some data or nrfStartListening(), or nrfPowerUp().

void nrfPowerUp ( void  )

Leave low-power mode - making radio more responsive.

To return to low power mode, call nrfPowerDown().

uint8_t nrfRead ( void *  buf,
uint8_t  len 
)

Read the payload.

Get the last payload received

Parameters
bufBuffer where the data should be written to
lenMaximum number of bytes to read into the buffer
Returns
1 if all data is read else 0
uint8_t nrfReadPayload ( void *  buf,
uint8_t  len 
)

Write the receive payload.

The size of data written is the fixed payload size, see getPayloadSize()

Parameters
bufBuffer to write the data to
lenMaximum number of bytes to receive
Returns
Current value of status register
uint8_t nrfReadRegister ( uint8_t  reg)

Read a byte from a register.

Parameters
regRegister address, see also tabel 28 of datasheet
Returns
Current value of register reg
uint8_t nrfReadRegisterMulti ( uint8_t  reg,
uint8_t *  buf,
uint8_t  len 
)

Read multiple bytes from a register.

Prototypes of functions.

Parameters
regRegister address, see also tabel 28 of datasheet
bufPointer to buffer to put data in
lenNumber of bytes to be received
Returns
Current value of status register
void nrfSetAutoAck ( uint8_t  enable)

Enable or disable auto-acknowlede packets.

Auto acknowledge is enabled by default, so it's only needed if you want to turn it off for some reason.

Parameters
enableWhether to enable (true, non 0) or disable (false, 0).
void nrfSetAutoAckPipe ( uint8_t  pipe,
uint8_t  enable 
)

Enable or disable auto-acknowlede packets for a distinct pipe.

Auto acknowledge is enabled by default, so it's only needed if you want to turn it off for some reason.

Parameters
pipePipe number which you want to enable or disable
enableWhether to enable (true, non 0) or disable (false, 0).
void nrfSetChannel ( uint8_t  channel)
void nrfSetCRCLength ( nrf_config_crc_t  length)

Sets the CRC length.

The CRC correspond to the following lengths respectively:

  • Disable CRC, use NRF_CONFIG_CRC_DISABLED_gc
  • CRC 1 byte, use NRF_CONFIG_CRC_8_gc
  • CRC 2 bytes, use NRF_CONFIG_CRC_16_gc
uint8_t nrfSetDataRate ( nrf_rf_setup_rf_dr_t  speed)

Set the transmission data rate.

The data rates correspond to the following speeds respectively:

  • 250kbps, use NRF_RF_SETUP_RF_DR_250K_gc (only for nRF24L01+)
  • 1Mpbs, use NRF_RF_SETUP_RF_DR_1M_gc
  • 2Mpbs, use NRF_RF_SETUP_RF_DR_2M_gc
Warning
Setting RF24_250KBPS will fail for a non nRF24L01+
Parameters
speed(is one of the group configurations)
Returns
1 (true) if successfull, 0 (false) if not
void nrfSetPALevel ( nrf_rf_setup_pwr_t  level)

Set Power Amplifier (PA) level.

The power levels correspond to the following output levels respectively:

  • minimum is -18dBm, use NRF_RF_SETUP_PWR_18DBM_gc
  • low is -12dBm, use NRF_RF_SETUP_PWR_12DBM_gc
  • high is -6dBm, use NRF_RF_SETUP_PWR_6DBM_gc
  • maximum is -0dBm, use NRF_RF_SETUP_PWR_0DBM_gc
Parameters
levelDesired level (is one of the group configurations)
void nrfSetPayloadSize ( uint8_t  size)
void nrfSetRetries ( uint8_t  delay,
uint8_t  retries 
)

Sets the number of retries and the delay between the retries.

The number of retries is 0 to 15 The delay between the retries is 250 us to 4000 us with with a 250 us step size.

The groupsconfiguration for the retries is NRF_SETUP_ARC_::RETRANSMIT_gc where # is NO or 1 to 15.

The groupsconfiguration for the delays is NRF_SETUP_ARD_::US_gc where # is 250 to 4000 with a 250 step size.

Parameters
delay(groupsconfiguration NRF_SETUP_ARD_::US_gc for delay #)
retries(groupsconfiguration NRF_SETUP_ARC_::RETRANSMIT_gc for retries #)
void nrfStartListening ( void  )

Start listening on the pipes opened for reading.

Be sure to call openReadingPipe() first. Do not call write() while in this mode, without first calling stopListening(). Call isAvailable() to check for incoming traffic, and read() to get it.

void nrfStartWrite ( const void *  buf,
uint8_t  len,
uint8_t  multicast 
)

Write to open writing pipe.

Same as write() but doesn't wait for acknowledge

Parameters
bufPointer to the data to be sent
lenNumber of bytes to be sent
multicast?? NRF_W_TX_PAYLOAD or NRF_W_TX_PAYLOAD_NO_ACK.
void nrfStopListening ( void  )

Stop listening for incoming messages.

Do this before calling write().

uint8_t nrfTestCarrier ( void  )

Test whether there was a carrier on the line for the previous listening period.

Valid only on nRF24L01. On nRF24L01, use nrfTestRPD().

Returns
1 (true) if there was a carrier 0 (false) if not.
uint8_t nrfTestRPD ( void  )

Test whether a signal (carrier or otherwise) greater than or equal to -64dBm is present on the channel.

Valid only on nRF24L01+(p). On nRF24L01, use nrfTestCarrier(). Useful to check for interference on the current channel and channel hopping strategies.

Returns
1 (true) if signal is greater or equal to -64dBm, 0 (false) if not.
void nrfToggleFeatures ( void  )

Turn on or off the special features.

The nrf2401(+) has certain 'features' which are only available when the 'features' are enabled.

uint8_t nrfVerifySPIConnection ( void  )

Verify SPI Interface.

It sends and reads back a random value to the SETUP register It restores the original setup

Returns
1 (true) if SPI interface is correct 0 (false) if not
uint8_t nrfWaitForAck ( void  )

Wait for acknowledge.

It waits for interrupt on TX complete, maximum retransmits reached or timer expired. The time depends on the number of retries and delay time. This time calculated with nrfGetMaxTimeout();

only write with an acknowledge is implemented

Returns
32 (true) if the payload was delivered successfully 0 if not
void nrfWhatHappened ( uint8_t *  tx_ok,
uint8_t *  tx_fail,
uint8_t *  rx_ready 
)

Call this to find out which interrupt occured.

Tells you what caused the interrupt, and clears the state of interrupts. The retuend values are not equal to 0 if the interrupt occured.

Parameters
[out]tx_okThe send was successful (TX_DS)
[out]tx_failThe send failed, too many retries (MAX_RT)
[out]rx_readyThere is a message waiting to be read (RX_DR)
uint8_t nrfWrite ( uint8_t *  buf,
uint8_t  len 
)

Write to the open writing pipe.

Be sure to call openWritingPipe() first to set the destination of where to write to.

only write with an acknowledge is implemented

Parameters
bufPointer to the data to be sent
lenNumber of bytes to be sent
Returns
32 (true) if the payload was delivered successfully 0 if not
void nrfWriteAckPayload ( uint8_t  pipe,
uint8_t *  buf,
uint8_t  len 
)

Write an ack payload for the specified pipe.

The next time a message is received on pipe, the data in buf will be sent back in the acknowledgement.

Warning
Do note, multicast payloads will not trigger ack payloads.
According to the data sheet, only three of these can be pending at any time.
Parameters
pipePipe number
bufBuffer to get the data from
lenNumber of bytes to be written
Returns
Current value of status register
uint8_t nrfWritePayload ( const void *  buf,
uint8_t  len,
const uint8_t  writeType 
)

Write the transmit payload.

The size of data written has a fixed payload size or a dynamic payload size. It uses the command W_TX_PAYLOAD (fixed size) or W_TX_PAYLOAD_NO_ACK (dynamic size).

Parameters
bufBuffer to get the data from
lenNumber of bytes to be written
writeTypeW_TX_PAYLOAD: fixed data size W_TX_PAYLOAD_NO_ACK: variable data size (multicast)
Returns
Current value of status register
uint8_t nrfWriteRegister ( uint8_t  reg,
uint8_t  value 
)

Write a byte to a register.

Parameters
regRegister address, see also tabel 28 of datasheet
valueThe new value of the register
Returns
Current value of status register
uint8_t nrfWriteRegisterMulti ( uint8_t  reg,
const uint8_t *  buf,
uint8_t  len 
)

Write multiple bytes to a register.

Parameters
regRegister address, see also tabel 28 of datasheet
bufPointer to buffer with data to be sent
lenNumber of bytes to be sent

This function is used at the inilialzation of NRF24L01p.

Returns
Current value of status register

Variable Documentation

uint8_t addr_width = 5

The address width to use - 3,4 or 5 bytes.

uint8_t dynamic_payloads_enabled = 0

Whether dynamic payloads are enabled.

uint8_t fixed_payload_size = NRF_MAX_PAYLOAD_SIZE

Size of a fixed payload.

uint8_t p_variant = 1

Global variables and constants.

1 for NRF24L01p 0 for NRF24L01

uint8_t pipe0_reading_address[5] = {0,0,0,0,0}

Last address set on pipe 0 for reading.