FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
devices::rf::NRF24L01< CSN, CE > Class Template Reference

SPI device driver for Nordic Semiconductor nRF24L01+ support, without IRQ support. More...

#include <fastarduino/devices/nrf24l01p.h>

Inheritance diagram for devices::rf::NRF24L01< CSN, CE >:
Collaboration diagram for devices::rf::NRF24L01< CSN, CE >:

Classes

struct  addr_t
 Network address together with port. More...
 
union  fifo_status_t
 Register FIFO_STATUS data type, transmission queue status. More...
 
union  observe_tx_t
 Register OBSERVE_TX data type, performance statistics. More...
 
union  status_t
 Register STATUS data type. More...
 

Public Member Functions

 NRF24L01 (uint16_t net, uint8_t dev)
 Construct NRF transceiver with given channel and pin numbers for SPI slave select, activity enable and interrupt. More...
 
uint8_t get_channel () const
 Get driver channel. More...
 
uint16_t get_network_address () const
 Get driver network address. More...
 
uint8_t get_device_address () const
 Get driver device address. More...
 
void set_address (int16_t net, uint8_t dev)
 Set network and device address. More...
 
void set_channel (uint8_t channel)
 Set device transmission channel. More...
 
void begin ()
 Start up the device driver. More...
 
void end ()
 Shut down the device driver.
 
void powerup ()
 Set power up mode. More...
 
void standby ()
 Set standby mode.
 
void powerdown ()
 Set power down. More...
 
template<typename T , typename TREF = const T&>
int send (uint8_t dest, uint8_t port, TREF buf)
 Send message with given object reference. More...
 
int send (uint8_t dest, uint8_t port)
 Send an empty message. More...
 
template<typename T , typename TREF = T&>
int recv (uint8_t &src, uint8_t &port, TREF buf, uint32_t ms=0L)
 Receive message and store into given object reference. More...
 
int recv (uint8_t &src, uint8_t &port, uint32_t ms=0L)
 Receive an empty message. More...
 
void set_output_power_level (int8_t dBm)
 Set output power level (-30..10 dBm) More...
 
uint16_t get_trans () const
 Return number of transmitted messages. More...
 
uint16_t get_retrans () const
 Return number of retransmissions. More...
 
uint16_t get_drops () const
 Return number of dropped messages. More...
 
template<typename T , typename TREF = const T&>
int broadcast (uint8_t port, TREF buf)
 Broadcast message with given object reference. More...
 
bool is_broadcast () const
 Return true if the latest received message was a broadcast otherwise false.
 

Static Public Attributes

static const uint8_t BROADCAST = 0x00
 Broadcast device address.
 
static const size_t DEVICE_PAYLOAD_MAX = 32
 Maximum size of payload on device.
 
static const size_t PAYLOAD_MAX = DEVICE_PAYLOAD_MAX - 2
 Maximum size of payload. More...
 

Protected Types

enum  Command : uint8_t {
  Command::R_REGISTER = 0x00,
  Command::W_REGISTER = 0x20,
  Command::REG_MASK = 0x1f,
  Command::R_RX_PAYLOAD = 0x61,
  Command::W_TX_PAYLOAD = 0xa0,
  Command::FLUSH_TX = 0xe1,
  Command::FLUSH_RX = 0xe2,
  Command::REUSE_TX_PL = 0xe3,
  Command::R_RX_PL_WID = 0x60,
  Command::W_ACK_PAYLOAD = 0xa8,
  Command::PIPE_MASK = 0x07,
  Command::W_TX_PAYLOAD_NO_ACK = 0xb0,
  Command::NOP = 0xff
}
 SPI Commands (See chap. More...
 
enum  Register : uint8_t {
  Register::CONFIG = 0x00,
  Register::EN_AA = 0x01,
  Register::EN_RXADDR = 0x02,
  Register::SETUP_AW = 0x03,
  Register::SETUP_RETR = 0x04,
  Register::RF_CH = 0x05,
  Register::RF_SETUP = 0x06,
  Register::STATUS = 0x07,
  Register::OBSERVE_TX = 0x08,
  Register::RPD = 0x09,
  Register::RX_ADDR_P0 = 0x0a,
  Register::RX_ADDR_P1 = 0x0b,
  Register::RX_ADDR_P2 = 0x0c,
  Register::RX_ADDR_P3 = 0x0d,
  Register::RX_ADDR_P4 = 0x0e,
  Register::RX_ADDR_P5 = 0x0f,
  Register::TX_ADDR = 0x10,
  Register::RX_PW_P0 = 0x11,
  Register::RX_PW_P1 = 0x12,
  Register::RX_PW_P2 = 0x13,
  Register::RX_PW_P3 = 0x14,
  Register::RX_PW_P4 = 0x15,
  Register::RX_PW_P5 = 0x16,
  Register::FIFO_STATUS = 0x17,
  Register::DYNPD = 0x1c,
  Register::FEATURE = 0x1d
}
 NRF transceiver registers map (See chap. More...
 
enum  State : uint8_t {
  POWER_DOWN_STATE = 0,
  STANDBY_STATE,
  RX_STATE,
  TX_STATE
}
 NRF transceiver states (See chap. More...
 

Additional Inherited Members

- Protected Member Functions inherited from spi::SPIDevice< CSN >
 SPIDevice () INLINE=default
 Create a new SPIDevice; this sets up the CS pin for later use during transfers.
 
void start_transfer ()
 Start an SPI transfer to this device. More...
 
void end_transfer () INLINE
 End the current SPI ransfer tot hsi device. More...
 
- Protected Member Functions inherited from spi::AbstractSPIDevice
 AbstractSPIDevice (const AbstractSPIDevice &)=delete
 
AbstractSPIDeviceoperator= (const AbstractSPIDevice &)=delete
 
uint8_t transfer (uint8_t data)
 Transfer one byte to the currently selected SPI slave device through MOSI pin, and get the byte returned by the device through MISO pin. More...
 
void transfer (uint8_t *data, uint16_t size)
 Transfer an array of payload data to the currently selected SPI slave device through MOSI pin, and get all data bytes simultaneously received from that device through MISO pin. More...
 
void transfer (const uint8_t *data, uint16_t size)
 Transfer an array of payload data to the currently selected SPI slave device through MOSI pin; any data bytes simultaneously received from that device through MISO pin are lost. More...
 
void transfer (uint8_t *data, uint16_t size, uint8_t sent)
 Transfer the provided byte sent several times to the currently selected SPI slave device through MOSI pin, and get all data bytes simultaneously received from that device through MISO pin. More...
 
void transfer (uint16_t size, uint8_t sent)
 Transfer the provided byte sent several times to the currently selected SPI slave device through MOSI pin; any data bytes simultaneously received from that device through MISO pin are lost. More...
 

Detailed Description

template<board::DigitalPin CSN, board::DigitalPin CE>
class devices::rf::NRF24L01< CSN, CE >

SPI device driver for Nordic Semiconductor nRF24L01+ support, without IRQ support.

nRF24L01+ is a cheap 2.4GHz RX/TX chip.

It must be powered at 3.3V maximum but all its input pins are 5V tolerant, hence no level shifting is needed to operate it.

                     NRF24L01P
                  +------------+

(GND)------------—1-|GND | (3V3)------------—2-|VCC | (Dn)-------------—3-|CE | (Dn)-------------—4-|CSN | (SCK)------------—5-|SCK | (MOSI)-----------—6-|MOSI | (MISO)-----------—7-|MISO | –8-|IRQ | +---------—+ Notes:

  • CSN is the usual CS pin used by SPI to select the device and can be set to any AVR pin
Template Parameters
CSNthe board::DigitalPin connected to the CSN pin
CEthe board::DigitalPin connected to the CE pin
See also
IRQ_NRF24L01

Definition at line 85 of file nrf24l01p.h.

Member Enumeration Documentation

◆ Command

template<board::DigitalPin CSN, board::DigitalPin CE>
enum devices::rf::NRF24L01::Command : uint8_t
strongprotected

SPI Commands (See chap.

8.3.1, tab. 20, pp. 51).

Enumerator
R_REGISTER 

Read command and status register.

W_REGISTER 

Write command and status register.

REG_MASK 

Mask register address (5b).

R_RX_PAYLOAD 

Read RX payload.

W_TX_PAYLOAD 

Write TX payload.

FLUSH_TX 

Flush TX FIFO.

FLUSH_RX 

Flush RX FIFO.

REUSE_TX_PL 

Reuse last transmitted payload.

R_RX_PL_WID 

Read RX payload width.

W_ACK_PAYLOAD 

Write TX payload with ACK (3 bit addr).

PIPE_MASK 

Mask pipe address.

W_TX_PAYLOAD_NO_ACK 

Disable AUTOACK on this specific packet.

NOP 

No operation, return status.

Definition at line 352 of file nrf24l01p.h.

◆ Register

template<board::DigitalPin CSN, board::DigitalPin CE>
enum devices::rf::NRF24L01::Register : uint8_t
strongprotected

NRF transceiver registers map (See chap.

9, tab. 28, pp. 57).

Enumerator
CONFIG 

Configuration register.

EN_AA 

Enable auto acknowledgement.

EN_RXADDR 

Enable rx addresses.

SETUP_AW 

Setup of address width.

SETUP_RETR 

Setup of auto retransmission.

RF_CH 

RF channel.

RF_SETUP 

RF setup register.

STATUS 

Status register.

OBSERVE_TX 

Transmit observe register.

RPD 

Received power detector.

RX_ADDR_P0 

Receive address data pipe 0.

RX_ADDR_P1 
  • data pipe 1.
RX_ADDR_P2 
  • data pipe 2.
RX_ADDR_P3 
  • data pipe 3.
RX_ADDR_P4 
  • data pipe 4.
RX_ADDR_P5 
  • data pipe 5.
TX_ADDR 

Transmit address.

RX_PW_P0 

Number of bytes in RX payload in data pipe 0.

RX_PW_P1 
  • data pipe 1.
RX_PW_P2 
  • data pipe 2.
RX_PW_P3 
  • data pipe 3.
RX_PW_P4 
  • data pipe 4.
RX_PW_P5 
  • data pipe 5.
FIFO_STATUS 

FIFO status register.

DYNPD 

Enable dynamic payload length.

FEATURE 

Feature register.

Definition at line 378 of file nrf24l01p.h.

◆ State

template<board::DigitalPin CSN, board::DigitalPin CE>
enum devices::rf::NRF24L01::State : uint8_t
strongprotected

NRF transceiver states (See chap.

6.1.1, fig. 4, pp. 22).

Definition at line 467 of file nrf24l01p.h.

Constructor & Destructor Documentation

◆ NRF24L01()

template<board::DigitalPin CSN, board::DigitalPin CE>
devices::rf::NRF24L01< CSN, CE >::NRF24L01 ( uint16_t  net,
uint8_t  dev 
)

Construct NRF transceiver with given channel and pin numbers for SPI slave select, activity enable and interrupt.

Default in parenthesis (Standard/Mega Arduino/TinyX4).

Parameters
[in]netnetwork address
[in]devdevice address

Definition at line 664 of file nrf24l01p.h.

Member Function Documentation

◆ get_channel()

template<board::DigitalPin CSN, board::DigitalPin CE>
uint8_t devices::rf::NRF24L01< CSN, CE >::get_channel ( ) const
inline

Get driver channel.

Returns
channel

Definition at line 115 of file nrf24l01p.h.

◆ get_network_address()

template<board::DigitalPin CSN, board::DigitalPin CE>
uint16_t devices::rf::NRF24L01< CSN, CE >::get_network_address ( ) const
inline

Get driver network address.

Returns
network address

Definition at line 124 of file nrf24l01p.h.

◆ get_device_address()

template<board::DigitalPin CSN, board::DigitalPin CE>
uint8_t devices::rf::NRF24L01< CSN, CE >::get_device_address ( ) const
inline

Get driver device address.

Returns
device address

Definition at line 133 of file nrf24l01p.h.

◆ set_address()

template<board::DigitalPin CSN, board::DigitalPin CE>
void devices::rf::NRF24L01< CSN, CE >::set_address ( int16_t  net,
uint8_t  dev 
)
inline

Set network and device address.

Do not use the broadcast address(0). Should be used before calling begin().

Parameters
[in]netnetwork address
[in]devdevice address

Definition at line 144 of file nrf24l01p.h.

◆ set_channel()

template<board::DigitalPin CSN, board::DigitalPin CE>
void devices::rf::NRF24L01< CSN, CE >::set_channel ( uint8_t  channel)
inline

Set device transmission channel.

Should be used before calling begin().

Parameters
[in]channel

Definition at line 155 of file nrf24l01p.h.

◆ begin()

template<board::DigitalPin CSN, board::DigitalPin CE>
void devices::rf::NRF24L01< CSN, CE >::begin

Start up the device driver.

This must be called before any transmission or reception can take place.

Definition at line 666 of file nrf24l01p.h.

◆ powerup()

template<board::DigitalPin CSN, board::DigitalPin CE>
void devices::rf::NRF24L01< CSN, CE >::powerup

Set power up mode.

Will initiate radio with necessary settings after power on reset.

Definition at line 691 of file nrf24l01p.h.

◆ powerdown()

template<board::DigitalPin CSN, board::DigitalPin CE>
void devices::rf::NRF24L01< CSN, CE >::powerdown

Set power down.

Turn off radio and go into low power mode.

Definition at line 715 of file nrf24l01p.h.

◆ send() [1/2]

template<board::DigitalPin CSN, board::DigitalPin CE>
template<typename T , typename TREF = const T&>
int devices::rf::NRF24L01< CSN, CE >::send ( uint8_t  dest,
uint8_t  port,
TREF  buf 
)
inline

Send message with given object reference.

Template Parameters
Tthe type of buf (the object to transfer)
TREFthe type passed for buf, typically a const reference to type T
Parameters
[in]destdestination network address
[in]portdevice port (or message type)
[in]bufreference of object to transmit
Returns
number of bytes send or negative error code
Return values
errors::EMSGSIZEif len > PAYLOAD_MAX
errors::EIOif a transmission failure happened
See also
errors::EMSGSIZE
errors::EIO

Definition at line 208 of file nrf24l01p.h.

◆ send() [2/2]

template<board::DigitalPin CSN, board::DigitalPin CE>
int devices::rf::NRF24L01< CSN, CE >::send ( uint8_t  dest,
uint8_t  port 
)
inline

Send an empty message.

Parameters
[in]destdestination network address
[in]portdevice port (or message type)
Returns
number of bytes send or negative error code
Return values
errors::EMSGSIZEif len > PAYLOAD_MAX
errors::EIOif a transmission failure happened
See also
errors::EMSGSIZE
errors::EIO

Definition at line 225 of file nrf24l01p.h.

◆ recv() [1/2]

template<board::DigitalPin CSN, board::DigitalPin CE>
template<typename T , typename TREF = T&>
int devices::rf::NRF24L01< CSN, CE >::recv ( uint8_t &  src,
uint8_t &  port,
TREF  buf,
uint32_t  ms = 0L 
)
inline

Receive message and store into given object reference.

The source network address is returned in the parameter src.

Template Parameters
Tthe type of buf (the object to be received)
TREFthe type passed for buf, typically a reference to type T
Parameters
[out]srcsource network address
[out]portdevice port (or message type)
[out]bufreference to object to fill with received payload; note that no constructor will get called during this operation, it is best to use simple struct for type T.
[in]msmaximum time out period
Returns
number of bytes received or negative error code
Return values
errors::ETIMEif nothing was received and a timeout occurred after ms elapsed
errors::EMSGSIZEif a payload error occurred from the chip (Tab. 20, pp. 51, R_RX_PL_WID) or the received payload size is bigger than the requested size
See also
errors::ETIME
errors::EMSGSIZE

Definition at line 255 of file nrf24l01p.h.

◆ recv() [2/2]

template<board::DigitalPin CSN, board::DigitalPin CE>
int devices::rf::NRF24L01< CSN, CE >::recv ( uint8_t &  src,
uint8_t &  port,
uint32_t  ms = 0L 
)
inline

Receive an empty message.

The source network address is returned in the parameter src.

Parameters
[out]srcsource network address
[out]portdevice port (or message type)
[in]msmaximum time out period
Returns
number of bytes received or negative error code
Return values
errors::ETIMEif nothing was received and a timeout occurred after ms elapsed
errors::EMSGSIZEif a payload error occurred from the chip (Tab. 20, pp. 51, R_RX_PL_WID) or the received payload size is bigger than the requested size
See also
errors::ETIME
errors::EMSGSIZE

Definition at line 277 of file nrf24l01p.h.

◆ set_output_power_level()

template<board::DigitalPin CSN, board::DigitalPin CE>
void devices::rf::NRF24L01< CSN, CE >::set_output_power_level ( int8_t  dBm)

Set output power level (-30..10 dBm)

Parameters
[in]dBm

Definition at line 805 of file nrf24l01p.h.

◆ get_trans()

template<board::DigitalPin CSN, board::DigitalPin CE>
uint16_t devices::rf::NRF24L01< CSN, CE >::get_trans ( ) const
inline

Return number of transmitted messages.

Returns
transmit count

Definition at line 292 of file nrf24l01p.h.

◆ get_retrans()

template<board::DigitalPin CSN, board::DigitalPin CE>
uint16_t devices::rf::NRF24L01< CSN, CE >::get_retrans ( ) const
inline

Return number of retransmissions.

Returns
retransmit count

Definition at line 301 of file nrf24l01p.h.

◆ get_drops()

template<board::DigitalPin CSN, board::DigitalPin CE>
uint16_t devices::rf::NRF24L01< CSN, CE >::get_drops ( ) const
inline

Return number of dropped messages.

Returns
drop count

Definition at line 310 of file nrf24l01p.h.

◆ broadcast()

template<board::DigitalPin CSN, board::DigitalPin CE>
template<typename T , typename TREF = const T&>
int devices::rf::NRF24L01< CSN, CE >::broadcast ( uint8_t  port,
TREF  buf 
)
inline

Broadcast message with given object reference.

Returns number of bytes sent if successful otherwise a negative error code.

Template Parameters
Tthe type of buf (the object to transfer)
TREFthe type passed for buf, typically a const reference to type T
Parameters
[in]portdevice port (or message type)
[in]bufreference of object to transmit
Returns
number of bytes send or negative error code

Definition at line 329 of file nrf24l01p.h.

Member Data Documentation

◆ PAYLOAD_MAX

template<board::DigitalPin CSN, board::DigitalPin CE>
const size_t devices::rf::NRF24L01< CSN, CE >::PAYLOAD_MAX = DEVICE_PAYLOAD_MAX - 2
static

Maximum size of payload.

The device allows 32 bytes payload. The source address one byte and port one byte as header.

Definition at line 100 of file nrf24l01p.h.


The documentation for this class was generated from the following file: