120 lines
3.0 KiB
C
120 lines
3.0 KiB
C
/**
|
|
* @file nrf24l01.h
|
|
* @brief NRF24L01+ driver header
|
|
*/
|
|
#pragma once
|
|
|
|
#include <stdint.h>
|
|
#include "esp_err.h"
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
/* NRF24L01 registers */
|
|
#define NRF_REG_CONFIG 0x00
|
|
#define NRF_REG_EN_AA 0x01
|
|
#define NRF_REG_EN_RXADDR 0x02
|
|
#define NRF_REG_SETUP_AW 0x03
|
|
#define NRF_REG_SETUP_RETR 0x04
|
|
#define NRF_REG_RF_CH 0x05
|
|
#define NRF_REG_RF_SETUP 0x06
|
|
#define NRF_REG_STATUS 0x07
|
|
#define NRF_REG_OBSERVE_TX 0x08
|
|
#define NRF_REG_RPD 0x09
|
|
#define NRF_REG_RX_ADDR_P0 0x0A
|
|
#define NRF_REG_RX_ADDR_P1 0x0B
|
|
#define NRF_REG_RX_ADDR_P2 0x0C
|
|
#define NRF_REG_RX_ADDR_P3 0x0D
|
|
#define NRF_REG_RX_ADDR_P4 0x0E
|
|
#define NRF_REG_RX_ADDR_P5 0x0F
|
|
#define NRF_REG_TX_ADDR 0x10
|
|
#define NRF_REG_RX_PW_P0 0x11
|
|
#define NRF_REG_RX_PW_P1 0x12
|
|
#define NRF_REG_RX_PW_P2 0x13
|
|
#define NRF_REG_RX_PW_P3 0x14
|
|
#define NRF_REG_RX_PW_P4 0x15
|
|
#define NRF_REG_RX_PW_P5 0x16
|
|
#define NRF_REG_FIFO_STATUS 0x17
|
|
#define NRF_REG_DYNPD 0x1C
|
|
#define NRF_REG_FEATURE 0x1D
|
|
|
|
/* Commands */
|
|
#define NRF_CMD_R_REGISTER 0x00
|
|
#define NRF_CMD_W_REGISTER 0x20
|
|
#define NRF_CMD_R_RX_PAYLOAD 0x61
|
|
#define NRF_CMD_W_TX_PAYLOAD 0xA0
|
|
#define NRF_CMD_FLUSH_TX 0xE1
|
|
#define NRF_CMD_FLUSH_RX 0xE2
|
|
#define NRF_CMD_REUSE_TX_PL 0xE3
|
|
#define NRF_CMD_ACTIVATE 0x50
|
|
#define NRF_CMD_R_RX_PL_WID 0x60
|
|
#define NRF_CMD_W_ACK_PAYLOAD 0xA8
|
|
#define NRF_CMD_W_TX_PAYLOAD_NOACK 0xB0
|
|
#define NRF_CMD_NOP 0xFF
|
|
|
|
/* Config register bits */
|
|
#define NRF_CONFIG_MASK_RX_DR (1<<6)
|
|
#define NRF_CONFIG_MASK_TX_DS (1<<5)
|
|
#define NRF_CONFIG_MASK_MAX_RT (1<<4)
|
|
#define NRF_CONFIG_EN_CRC (1<<3)
|
|
#define NRF_CONFIG_CRCO (1<<2)
|
|
#define NRF_CONFIG_PWR_UP (1<<1)
|
|
#define NRF_CONFIG_PRIM_RX (1<<0)
|
|
|
|
/* RF data rates */
|
|
typedef enum {
|
|
NRF_1MBPS = 0,
|
|
NRF_2MBPS = 1,
|
|
NRF_250KBPS = 2
|
|
} nrf_data_rate_t;
|
|
|
|
/* RF power levels */
|
|
typedef enum {
|
|
NRF_PA_MIN = 0,
|
|
NRF_PA_LOW = 1,
|
|
NRF_PA_HIGH = 2,
|
|
NRF_PA_MAX = 3
|
|
} nrf_pa_power_t;
|
|
|
|
/* Initialize NRF24L01 with GPIO configuration */
|
|
esp_err_t nrf24_init(int mosi, int miso, int sclk, int cs, int ce, int irq);
|
|
|
|
/* Set RF channel (0-125) */
|
|
void nrf24_set_channel(uint8_t channel);
|
|
|
|
/* Set data rate */
|
|
void nrf24_set_data_rate(nrf_data_rate_t rate);
|
|
|
|
/* Set TX power */
|
|
void nrf24_set_power(nrf_pa_power_t power);
|
|
|
|
/* Set TX/RX address (5 bytes) */
|
|
void nrf24_set_tx_addr(const uint8_t *addr);
|
|
void nrf24_set_rx_addr(const uint8_t *addr, uint8_t pipe);
|
|
|
|
/* Power up in TX or RX mode */
|
|
void nrf24_power_up_tx(void);
|
|
void nrf24_power_up_rx(void);
|
|
void nrf24_power_down(void);
|
|
|
|
/* Transmit a payload and wait for ACK (blocking) */
|
|
esp_err_t nrf24_tx_payload(const uint8_t *data, uint8_t len);
|
|
|
|
/* Read received payload */
|
|
esp_err_t nrf24_rx_payload(uint8_t *data, uint8_t *len);
|
|
|
|
/* Check if data is available in RX FIFO */
|
|
uint8_t nrf24_data_available(void);
|
|
|
|
/* Get status */
|
|
uint8_t nrf24_get_status(void);
|
|
|
|
/* Flush FIFOs */
|
|
void nrf24_flush_tx(void);
|
|
void nrf24_flush_rx(void);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|