nrf24x.h

Constant definitions and helper functions for the SPI command interface of the nRF24x series of 2.4 GHz digital transceivers.  Relies on the spihelper.h module.

NOTE: The functions provided by this module do NOT manipulate any SPI Slave Select pins.  Application developers must ensure that the commands these functions transmit via the SPI peripheral reach the intended destination device.

NOTE: This module does not deal with the CE and IRQ pins of the nRF24x IC.  Those pins must be managed by application code.

NOTE: By default, this module will use the asynchronous API of the SPI helper module (i.e.  spihelper_request), unless that API has been disabled.  To use the synchronous API (i.e.  spihelper_exchange_bytes) regardless of whether the asynchronous API is available, define the macro NRF24X_SYNCHRONOUS.

NOTE: By default, this module uses an internal command buffer when operating in asynchronous mode (i.e. when using the asynchronous API of spihelper.h).  This buffer can be disabled and left undeclared (saving NRF24X_BFR_SIZE bytes of RAM) by defining the macro NRF24X_NO_CMD_BFR.  However, doing this will also disable most of the helper functions.

Summary
nrf24x.hConstant definitions and helper functions for the SPI command interface of the nRF24x series of 2.4 GHz digital transceivers.
Configuration Macros
Macros
NRF24X_BFR_SIZESize in bytes of the buffer for asynchronous SPI operations.
SPI Interface Constants and Helper Macros
Enumerations and Macros
nRF24x Command WordsSee the IC product specification for information about the protocol of the SPI interface.
NRF24X_R_REG_CMDConstructs an R_REGISTER command word with a specified nRF24x register address.
NRF24X_W_REG_CMDConstructs a W_REGISTER command word with a specified nRF24x register address.
NRF24X_W_ACK_PAYLOAD_CMDConstructs a W_ACK_PAYLOAD command word with a specified nRF24x data pipe number.
nRF24x Register AddressesSee the IC product specification for information about register semantics.
nRF24x Data Pipe NumbersSee the IC product specification for information about data pipes.
nRF24x CONFIG Register BitsSee the IC product specification for information about the CONFIG register.
nRF24x SETUP_AW Register ValuesSee the IC product specification for information about the SETUP_AW register.
NRF24X_SETUP_RETR_VALConstructs a SETUP_RETR register value from ARD and ARC bitfield values.
NRF24X_RF_CH_VALConstructs an RF_CH register value from an RF_CH bitfield value.
nRF24x RF_SETUP Register BitsSee the IC product specification for information about the RF_SETUP register.
nRF24x STATUS Register BitsSee the IC product specification for information about the STATUS register.
NRF24X_GET_RX_P_NOExtracts the RX_P_NO bitfield value from a STATUS register value.
NRF24X_GET_PLOS_CNTExtracts the PLOS_CNT bitfield value from an OBSERVE_TX register value.
NRF24X_GET_ARC_CNTExtracts the ARC_CNT bitfield value from an OBSERVE_TX register value.
nRF24x CD/RPD Register BitsSee the IC product specification for information about the CD/RPD register.
nRF24x FIFO_STATUS Register BitsSee the IC product specification for information about the FIFO_STATUS register.
nRF24x FEATURE Register BitsSee the IC product specification for information about the FEATURE register.
Command API Variables
Variables
nrf24x_statusStorage location for STATUS values.
nrf24x_commandWhen the NRF24X_CMD_IN API macro is used, the command word of the input command to send MUST be stored here before the macro is invoked.
Command API Macros and Functions
Macros
NRF24X_CMD_OUTInitiates an output command to the nRF24x via an asynchronous SPI request.
NRF24X_CMD_INInitiates an input command to the nRF24x via an asynchronous SPI request.
Functions
nrf24x_out_0Sends a 0-byte output command (one that consists only of a command word) to the nRF24x.
nrf24x_out_1Sends a 1-byte output command (one that consists of a command word and a single output data byte) to the nRF24x.
nrf24x_out_nSends an N-byte output command (one that consists of a command word and a specified number of output data bytes) to the nRF24x.
nrf24x_in_1Sends a 1-byte input command (one that receives a single input data byte in response) to the nRF24x.
nrf24x_in_nSends an N-byte input command (one that receives a specified number of input data bytes in response) to the nRF24x.
Macros
NRF24X_NONE_PENDINGSpecial value returned by nrf24x_in_finish when no asynchronous command is pending.
Functions
nrf24x_in_finishUsed to finish an asynchronous input command sent to the nRF24x and fetch the received input data.

Configuration Macros

Summary
Macros
NRF24X_BFR_SIZESize in bytes of the buffer for asynchronous SPI operations.

Macros

NRF24X_BFR_SIZE

Size in bytes of the buffer for asynchronous SPI operations.  Configuration macro.

Default value: 33

SPI Interface Constants and Helper Macros

Summary
Enumerations and Macros
nRF24x Command WordsSee the IC product specification for information about the protocol of the SPI interface.
NRF24X_R_REG_CMDConstructs an R_REGISTER command word with a specified nRF24x register address.
NRF24X_W_REG_CMDConstructs a W_REGISTER command word with a specified nRF24x register address.
NRF24X_W_ACK_PAYLOAD_CMDConstructs a W_ACK_PAYLOAD command word with a specified nRF24x data pipe number.
nRF24x Register AddressesSee the IC product specification for information about register semantics.
nRF24x Data Pipe NumbersSee the IC product specification for information about data pipes.
nRF24x CONFIG Register BitsSee the IC product specification for information about the CONFIG register.
nRF24x SETUP_AW Register ValuesSee the IC product specification for information about the SETUP_AW register.
NRF24X_SETUP_RETR_VALConstructs a SETUP_RETR register value from ARD and ARC bitfield values.
NRF24X_RF_CH_VALConstructs an RF_CH register value from an RF_CH bitfield value.
nRF24x RF_SETUP Register BitsSee the IC product specification for information about the RF_SETUP register.
nRF24x STATUS Register BitsSee the IC product specification for information about the STATUS register.
NRF24X_GET_RX_P_NOExtracts the RX_P_NO bitfield value from a STATUS register value.
NRF24X_GET_PLOS_CNTExtracts the PLOS_CNT bitfield value from an OBSERVE_TX register value.
NRF24X_GET_ARC_CNTExtracts the ARC_CNT bitfield value from an OBSERVE_TX register value.
nRF24x CD/RPD Register BitsSee the IC product specification for information about the CD/RPD register.
nRF24x FIFO_STATUS Register BitsSee the IC product specification for information about the FIFO_STATUS register.
nRF24x FEATURE Register BitsSee the IC product specification for information about the FEATURE register.

Enumerations and Macros

nRF24x Command Words

See the IC product specification for information about the protocol of the SPI interface.

NRF24X_R_REGISTERR_REGISTER command word (register address zero)
NRF24X_W_REGISTERW_REGISTER command word (register address zero)
NRF24X_R_RX_PAYLOADR_RX_PAYLOAD command word
NRF24X_W_TX_PAYLOADW_TX_PAYLOAD command word
NRF24X_FLUSH_TXFLUSH_TX command word
NRF24X_FLUSH_RXFLUSH_RX command word
NRF24X_REUSE_TX_PLREUSE_TX_PL command word
NRF24X_ACTIVATEACTIVATE command word (not used by nRF24L01+)
NRF24X_ACTIVATE_2ACTIVATE command argument (not used by nRF24L01+)
NRF24X_R_RX_PL_WIDR_RX_PL_WID command word
NRF24X_W_ACK_PAYLOADW_ACK_PAYLOAD command word (data pipe number zero)
NRF24X_W_TX_PAYLOAD_NOACKW_TX_PAYLOAD_NOACK command word
NRF24X_NOPNOP command word

NRF24X_R_REG_CMD

Constructs an R_REGISTER command word with a specified nRF24x register address.  Function-like macro.

Parameters

A5-bit register address

Returns

An R_REGISTER command word containing the specified register address.

NRF24X_W_REG_CMD

Constructs a W_REGISTER command word with a specified nRF24x register address.  Function-like macro.

Parameters

A5-bit register address

Returns

A W_REGISTER command word containing the specified register address.

NRF24X_W_ACK_PAYLOAD_CMD

Constructs a W_ACK_PAYLOAD command word with a specified nRF24x data pipe number.  Function-like macro.

Parameters

P3-bit data pipe number

Returns

A W_ACK_PAYLOAD command word containing the specified data pipe number.

nRF24x Register Addresses

See the IC product specification for information about register semantics.

NRF24X_CONFIGCONFIG register address
NRF24X_EN_AAEN_AA register address
NRF24X_EN_RXADDREN_RXADDR register address
NRF24X_SETUP_AWSETUP_AW register address
NRF24X_SETUP_RETRSETUP_RETR register address
NRF24X_RF_CHRF_CH register address
NRF24X_RF_SETUPRF_SETUP register address
NRF24X_STATUSSTATUS register address
NRF24X_OBSERVE_TXOBSERVE_TX register address
NRF24X_CDCD register address (old name used for nRF24L01)
NRF24X_RPDRPD register address (new name used for nRF24L01+)
NRF24X_RX_ADDR_P0RX_ADDR_P0 register address
NRF24X_RX_ADDR_P1RX_ADDR_P1 register address
NRF24X_RX_ADDR_P2RX_ADDR_P2 register address
NRF24X_RX_ADDR_P3RX_ADDR_P3 register address
NRF24X_RX_ADDR_P4RX_ADDR_P4 register address
NRF24X_RX_ADDR_P5RX_ADDR_P5 register address
NRF24X_TX_ADDRTX_ADDR register address
NRF24X_RX_PW_P0RX_PW_P0 register address
NRF24X_RX_PW_P1RX_PW_P1 register address
NRF24X_RX_PW_P2RX_PW_P2 register address
NRF24X_RX_PW_P3RX_PW_P3 register address
NRF24X_RX_PW_P4RX_PW_P4 register address
NRF24X_RX_PW_P5RX_PW_P5 register address
NRF24X_FIFO_STATUSFIFO_STATUS register address
NRF24X_DYNPDDYNPD register address
NRF24X_FEATUREFEATURE register address

nRF24x Data Pipe Numbers

See the IC product specification for information about data pipes.

NRF24X_P0data pipe number 0
NRF24X_P1data pipe number 1
NRF24X_P2data pipe number 2
NRF24X_P3data pipe number 3
NRF24X_P4data pipe number 4
NRF24X_P5data pipe number 5

nRF24x CONFIG Register Bits

See the IC product specification for information about the CONFIG register.

NRF24X_MASK_RX_DRMASK_RX_DR bit number
NRF24X_MASK_TX_DSMASK_TX_DS bit number
NRF24X_MASK_MAX_RTMASK_MAX_RT bit number
NRF24X_EN_CRCEN_CRC bit number
NRF24X_CRCOCRCO bit number
NRF24X_PWR_UPPWR_UP bit number
NRF24X_PRIM_RXPRIM_RX bit number

nRF24x SETUP_AW Register Values

See the IC product specification for information about the SETUP_AW register.

NRF24X_AW_3AW register value specifying 3-byte RX/TX addresses
NRF24X_AW_4AW register value specifying 4-byte RX/TX addresses
NRF24X_AW_5AW register value specifying 5-byte RX/TX addresses

NRF24X_SETUP_RETR_VAL

Constructs a SETUP_RETR register value from ARD and ARC bitfield values.  Function-like macro.

Parameters

ARD4-bit ARD bitfield value
ARC4-bit ARC bitfield value

Returns

A SETUP_RETR register value containing the specified ARD and ARC values.

NRF24X_RF_CH_VAL

Constructs an RF_CH register value from an RF_CH bitfield value.  Function-like macro.

Parameters

CH7-bit RF_CH bitfield value

Returns

An RF_CH register value containing the specified bitfield value.

nRF24x RF_SETUP Register Bits

See the IC product specification for information about the RF_SETUP register.

NRF24X_PLL_LOCKPLL_LOCK bit number
NRF24X_RF_DRRF_DR bit number
NRF24X_RF_PWR1RF_PWR bit number 1
NRF24X_RF_PWR0RF_PWR bit number 0
NRF24X_LNA_HCURRLNA_HCURR bit number

nRF24x STATUS Register Bits

See the IC product specification for information about the STATUS register.

NRF24X_RX_DRRX_DR bit number
NRF24X_TX_DSTX_DS bit number
NRF24X_MAX_RTMAX_RT bit number
NRF24X_RX_P_NO2RX_P_NO bit number 2
NRF24X_RX_P_NO1RX_P_NO bit number 1
NRF24X_RX_P_NO0RX_P_NO bit number 0
NRF24X_ST_TX_FULLST_TX_FULL bit number

NRF24X_GET_RX_P_NO

Extracts the RX_P_NO bitfield value from a STATUS register value.  Function-like macro.

Parameters

STSTATUS register value

Returns

The 3-bit RX_P_NO bitfield value in the specified register value.

NRF24X_GET_PLOS_CNT

Extracts the PLOS_CNT bitfield value from an OBSERVE_TX register value.  Function-like macro.

Parameters

OTXOBSERVE_TX register value

Returns

The 4-bit PLOS_CNT bitfield value in the specified register value.

NRF24X_GET_ARC_CNT

Extracts the ARC_CNT bitfield value from an OBSERVE_TX register value.  Function-like macro.

Parameters

OTXOBSERVE_TX register value

Returns

The 4-bit ARC_CNT bitfield value in the specified register value.

nRF24x CD/RPD Register Bits

See the IC product specification for information about the CD/RPD register.

NRF24X_CD0CD bit number 0
NRF24X_RPD0RPD bit number 0

nRF24x FIFO_STATUS Register Bits

See the IC product specification for information about the FIFO_STATUS register.

NRF24X_TX_REUSETX_REUSE bit number
NRF24X_TX_FULLTX_FULL bit number
NRF24X_TX_EMPTYTX_EMPTY bit number
NRF24X_RX_FULLRX_FULL bit number
NRF24X_RX_EMPTYRX_EMPTY bit number

nRF24x FEATURE Register Bits

See the IC product specification for information about the FEATURE register.

NRF24X_EN_DPLEN_DPL bit number
NRF24X_EN_ACK_PAYEN_ACK_PAY bit number
NRF24X_EN_DYN_ACKEN_DYN_ACK bit number

Command API Variables

Summary
Variables
nrf24x_statusStorage location for STATUS values.
nrf24x_commandWhen the NRF24X_CMD_IN API macro is used, the command word of the input command to send MUST be stored here before the macro is invoked.

Variables

nrf24x_status

extern volatile uint8_t nrf24x_status

Storage location for STATUS values.  The nRF24x IC returns the value of its STATUS register when it receives a command word via SPI.  Some of the API macros and functions in this module will store the returned STATUS value here when they receive it.

nrf24x_command

extern volatile uint8_t nrf24x_command

When the NRF24X_CMD_IN API macro is used, the command word of the input command to send MUST be stored here before the macro is invoked.  That is generally the only reason to touch this variable application code will have.

NOTE: Not defined in synchronous mode.

Command API Macros and Functions

Summary
Macros
NRF24X_CMD_OUTInitiates an output command to the nRF24x via an asynchronous SPI request.
NRF24X_CMD_INInitiates an input command to the nRF24x via an asynchronous SPI request.
Functions
nrf24x_out_0Sends a 0-byte output command (one that consists only of a command word) to the nRF24x.
nrf24x_out_1Sends a 1-byte output command (one that consists of a command word and a single output data byte) to the nRF24x.
nrf24x_out_nSends an N-byte output command (one that consists of a command word and a specified number of output data bytes) to the nRF24x.
nrf24x_in_1Sends a 1-byte input command (one that receives a single input data byte in response) to the nRF24x.
nrf24x_in_nSends an N-byte input command (one that receives a specified number of input data bytes in response) to the nRF24x.
Macros
NRF24X_NONE_PENDINGSpecial value returned by nrf24x_in_finish when no asynchronous command is pending.
Functions
nrf24x_in_finishUsed to finish an asynchronous input command sent to the nRF24x and fetch the received input data.

Macros

NRF24X_CMD_OUT

Initiates an output command to the nRF24x via an asynchronous SPI request.  Function-like macro.

NOTE: Not defined in synchronous mode.

NOTE: Writes the STATUS register value returned by the nRF24x to nrf24x_status.

Parameters

NNumber of output bytes to transmit, including the command word.
BPointer to the output bytes to transmit, including the command word.

Returns

SPI_ACTIVE if and only if the SPI request was successfully started, otherwise an error code.

NRF24X_CMD_IN

Initiates an input command to the nRF24x via an asynchronous SPI request.  Function-like macro.

NOTE: Not defined in synchronous mode.

CAUTION: The command word of the input command to send to the nRF24x MUST be stored in nrf24x_command before this macro is invoked.

Parameters

NNumber of input bytes to receive.
ONumber of received bytes to discard before getting the input bytes.  Note that the sequence of received bytes includes the STATUS register value that the nRF24x returns when the command word is transmitted.
BPointer to memory to copy the received input bytes into.

Returns

SPI_ACTIVE if and only if the SPI request was successfully started, otherwise an error code.

Functions

nrf24x_out_0

uint8_t nrf24x_out_0(uint8_t cmd)

Sends a 0-byte output command (one that consists only of a command word) to the nRF24x.

NOTE: Writes the STATUS register value returned by the nRF24x to nrf24x_status.

Parameters

cmdCommand word of the output command.

Returns

A true value if and only if the SPI request was successfully performed (or started, in asynchronous mode).

nrf24x_out_1

uint8_t nrf24x_out_1(uint8_t cmd,
uint8_t data)

Sends a 1-byte output command (one that consists of a command word and a single output data byte) to the nRF24x.

NOTE: Not defined when NRF24X_NO_CMD_BFR is defined in asynchronous mode.

NOTE: Writes the STATUS register value returned by the nRF24x to nrf24x_status when called in synchronous mode.

Parameters

cmdCommand word of the output command.
dataData byte of the output command.

Returns

A true value if and only if the SPI request was successfully performed (or started, in asynchronous mode).

nrf24x_out_n

uint8_t nrf24x_out_n(uint8_t cmd,
uint8_t n_out,
uint8_t *bfr_out)

Sends an N-byte output command (one that consists of a command word and a specified number of output data bytes) to the nRF24x.

NOTE: Not defined when NRF24X_NO_CMD_BFR is defined in asynchronous mode.

NOTE: Writes the STATUS register value returned by the nRF24x to nrf24x_status when called in synchronous mode.

Parameters

cmdCommand word of the output command.
n_outNumber of data bytes to transmit.
bfr_outPointer to the data bytes to transmit.

Returns

A true value if and only if the SPI request was successfully performed (or started, in asynchronous mode).

nrf24x_in_1

uint8_t nrf24x_in_1(uint8_t cmd,
uint8_t *data_p)

Sends a 1-byte input command (one that receives a single input data byte in response) to the nRF24x.

NOTE: Not defined when NRF24X_NO_CMD_BFR is defined in asynchronous mode.

NOTE: Writes the STATUS register value returned by the nRF24x to nrf24x_status when called in synchronous mode.

Parameters

cmdCommand word of the input command.
data_pPointer to location to copy the received data byte into.  Note that this argument is ignored in asynchronous mode, where the input buffer pointer is given as an argument to nrf24x_in_finish.

Returns

A true value if and only if the SPI request was successfully performed (or started, in asynchronous mode).

nrf24x_in_n

uint8_t nrf24x_in_n(uint8_t cmd,
uint8_t n_in,
uint8_t *bfr_in)

Sends an N-byte input command (one that receives a specified number of input data bytes in response) to the nRF24x.

NOTE: Not defined when NRF24X_NO_CMD_BFR is defined in asynchronous mode.

NOTE: Writes the STATUS register value returned by the nRF24x to nrf24x_status when called in synchronous mode.

Parameters

cmdCommand word of the input command.
n_inNumber of data bytes to receive.
bfr_inPointer to memory to copy received data bytes into.  Note that this argument is ignored in asynchronous mode, where the input buffer pointer is given as an argument to nrf24x_in_finish.

Returns

A true value if and only if the SPI request was successfully performed (or started, in asynchronous mode).

Macros

NRF24X_NONE_PENDING

Special value returned by nrf24x_in_finish when no asynchronous command is pending.

CAUTION: This is equal to 255/0xff, so that value MUST NOT be used as an actual data byte count.  There will generally not be any need for byte counts that large, anyway.

Constant macro.

Functions

nrf24x_in_finish

uint8_t nrf24x_in_finish(uint8_t *bfr_in)

Used to finish an asynchronous input command sent to the nRF24x and fetch the received input data.

NOTE: Not defined if the module is in synchronous mode or NRF24X_NO_CMD_BFR is defined.

NOTE: Writes the STATUS register value returned by the nRF24x to nrf24x_status.

Parameters

bfr_inPointer to memory to copy received data bytes into.

Returns

The number of data bytes received, or NRF24X_NONE_PENDING if no asynchronous command is pending.

extern volatile uint8_t nrf24x_status
Storage location for STATUS values.
extern volatile uint8_t nrf24x_command
When the NRF24X_CMD_IN API macro is used, the command word of the input command to send MUST be stored here before the macro is invoked.
Initiates an input command to the nRF24x via an asynchronous SPI request.
uint8_t nrf24x_out_0(uint8_t cmd)
Sends a 0-byte output command (one that consists only of a command word) to the nRF24x.
uint8_t nrf24x_out_1(uint8_t cmd,
uint8_t data)
Sends a 1-byte output command (one that consists of a command word and a single output data byte) to the nRF24x.
uint8_t nrf24x_out_n(uint8_t cmd,
uint8_t n_out,
uint8_t *bfr_out)
Sends an N-byte output command (one that consists of a command word and a specified number of output data bytes) to the nRF24x.
uint8_t nrf24x_in_1(uint8_t cmd,
uint8_t *data_p)
Sends a 1-byte input command (one that receives a single input data byte in response) to the nRF24x.
uint8_t nrf24x_in_n(uint8_t cmd,
uint8_t n_in,
uint8_t *bfr_in)
Sends an N-byte input command (one that receives a specified number of input data bytes in response) to the nRF24x.
uint8_t nrf24x_in_finish(uint8_t *bfr_in)
Used to finish an asynchronous input command sent to the nRF24x and fetch the received input data.
Helper module for SPI communication.
spi_state spihelper_request(uint8_t n_out,
volatile const uint8_t *bfr_out,
uint8_t n_in,
uint8_t offset_in,
volatile uint8_t *bfr_in)
Initiates an SPI request operation consisting of a transmit phase followed by a receive phase.
uint8_t spihelper_exchange_bytes(uint8_t data_out)
Synchronously exchanges a single pair of bytes via the SPI peripheral.
module is currently performing an operation
Special value returned by nrf24x_in_finish when no asynchronous command is pending.
Close