Introduction
This is a comprehensive driver library for the nRF24L01 2.4GHz wireless transceiver module, designed specifically for STM32 microcontrollers using the HAL library.
Key Features
- Full register-level control of nRF24L01 functionality
- Support for all 6 data pipes with individual configuration
- Auto-acknowledgment and auto-retransmission
- Dynamic payload lengths
- Payload with ACK capability
- Configurable RF power levels and data rates
- Comprehensive interrupt handling
- Easy-to-use high-level API functions
Basic Usage
Initialization
nrf.ce_port = GPIOA;
nrf.ce_pin = 8;
nrf.irq_port = GPIOA;
nrf.irq_pin = 10;
}
uint8_t nrf24l01_init(nrf24l01_device *device)
Initialize nRF24L01 device.
Definition nrf24l01.c:85
nrf24l01_device nrf24l01_get_default_config()
Get default device configuration.
Definition nrf24l01.c:3
nRF24L01 Wireless Transceiver Driver for STM32
Main nRF24L01 device configuration structure.
Definition nrf24l01.h:327
SPI_HandleTypeDef * spi
Definition nrf24l01.h:328
TIM_HandleTypeDef * timer
Definition nrf24l01.h:331
GPIO_TypeDef * csn_port
Definition nrf24l01.h:329
uint8_t csn_pin
Definition nrf24l01.h:330
Transmitter Setup
uint8_t tx_address[] = {0xE7, 0xE7, 0xE7, 0xE7, 0xE7};
uint8_t data[] = "Hello nRF24L01!";
uint8_t nrf24l01_transmit(nrf24l01_device *device)
Configure device for transmit mode.
Definition nrf24l01.c:544
uint8_t nrf24l01_write_tx_payload(nrf24l01_device *device, uint8_t *data, uint16_t length)
Write TX payload to FIFO.
Definition nrf24l01.c:369
uint8_t nrf24l01_data_pipe_auto_ack(nrf24l01_device *device, uint8_t pipe_number, uint8_t enable)
Enable/disable auto-acknowledgment for specific pipe.
Definition nrf24l01.c:661
uint8_t nrf24l01_data_pipe_address(nrf24l01_device *device, uint8_t pipe_number, uint8_t *address, uint8_t length)
Set receive address for specific data pipe.
Definition nrf24l01.c:679
uint8_t nrf24l01_data_pipe_payload_width(nrf24l01_device *device, uint8_t pipe_number, uint8_t width)
Set payload width for specific data pipe.
Definition nrf24l01.c:701
uint8_t nrf24l01_data_pipe_enable(nrf24l01_device *device, uint8_t pipe_number, uint8_t enable)
Enable/disable specific data pipe.
Definition nrf24l01.c:643
uint8_t nrf24l01_write_register(nrf24l01_device *device, uint8_t reg, uint8_t *data, uint16_t length)
Write to nRF24L01 register.
Definition nrf24l01.c:316
#define TX_ADDR
Definition nrf24l01.h:80
uint8_t * transmit_address
Definition nrf24l01.h:342
Receiver Setup
uint8_t rx_address[] = {0xE7, 0xE7, 0xE7, 0xE7, 0xE7};
uint8_t config_register = 0;
config_register |= PRX;
nrf24l01_wrtie_register(&nrf,
CONFIG, &config_register, 1);
do {
uint8_t received_data[32];
break;
}
}while(1);
uint8_t nrf24l01_nop(nrf24l01_device *device)
No operation command.
Definition nrf24l01.c:513
#define RX_DR
Definition nrf24l01.h:159
@ nrf24l01_irq_rx_data_ready
Definition nrf24l01.h:299
uint8_t nrf24l01_clear_interrupt_flag(nrf24l01_device *device, nrf24l01_irq irq)
Clear specific interrupt flag.
Definition nrf24l01.c:741
uint8_t nrf24l01_listen(nrf24l01_device *device)
Configure device for receive mode.
Definition nrf24l01.c:562
uint8_t nrf24l01_read_rx_payload(nrf24l01_device *device, uint8_t *data, uint16_t length)
Read RX payload from FIFO.
Definition nrf24l01.c:343
uint8_t nrf24l01_read_register(nrf24l01_device *device, uint8_t reg, uint8_t *data, uint16_t length)
Read from nRF24L01 register.
Definition nrf24l01.c:290
#define CONFIG
Definition nrf24l01.h:64
Configuration Options
The driver supports extensive configuration options:
- RF Channel: 0-125 (2.400-2.525 GHz)
- Data Rate: 1 Mbps or 2 Mbps
- Output Power: -18, -12, -6, or 0 dBm
- Address Width: 3, 4, or 5 bytes
- Auto Retransmit: Configurable delay (250µs-4ms) and count (0-15)
- Payload Length: Fixed (1-32 bytes) or dynamic
Advanced Features
Dynamic Payload Length
uint8_t nrf24l01_dynamic_payload_length(nrf24l01_device *device, uint8_t enable)
Enable/disable dynamic payload length.
Definition nrf24l01.c:578
uint8_t nrf24l01_data_pipe_dynamic_payload_length(nrf24l01_device *device, uint8_t pipe_number, uint8_t enable)
Enable/disable dynamic payload length for specific pipe.
Definition nrf24l01.c:624
ACK Payloads
uint8_t ack_data[] = "ACK";
uint8_t nrf24l01_payload_with_ack(nrf24l01_device *device, uint8_t enable)
Enable/disable payload with ACK.
Definition nrf24l01.c:594
uint8_t nrf24l01_write_ack_payload(nrf24l01_device *device, uint8_t *data, uint8_t pipe, uint16_t length)
Write ACK payload for specific pipe.
Definition nrf24l01.c:463
Troubleshooting
Common issues and solutions:
- No communication: Check SPI connections and timing
- Transmission fails: Verify addresses match between TX and RX
- High packet loss: Adjust RF power level or channel
- Interrupt not working: Ensure IRQ pin is configured and interrupt flags are cleared
License
This driver is provided as-is for educational and commercial use.
- Author
- Trần Minh Khải
- Date
- September 18, 2025
- Version
- 1.0