71 lines
2.1 KiB
C
71 lines
2.1 KiB
C
/**
|
|
* @file nrf24l01.h
|
|
* @brief NRF24L01 驱动 - 接收机端 (RX 模式 + ACK 发送)
|
|
*/
|
|
#ifndef __NRF24L01_H
|
|
#define __NRF24L01_H
|
|
|
|
#include "stc8h.h"
|
|
#include <stdint.h>
|
|
#include "config.h"
|
|
|
|
/* SPI 命令 */
|
|
#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_NOP 0xFF
|
|
#define NRF_CMD_W_ACK_PAYLOAD 0xA8 /* P0: 0xA8, P1: 0xA9 ... */
|
|
|
|
/* 寄存器 */
|
|
#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_TX_ADDR 0x10
|
|
#define NRF_REG_RX_PW_P0 0x11
|
|
#define NRF_REG_FIFO_STATUS 0x17
|
|
#define NRF_REG_DYNPD 0x1C
|
|
#define NRF_REG_FEATURE 0x1D
|
|
|
|
#define NRF_STATUS_RX_DR (1 << 6)
|
|
#define NRF_STATUS_TX_DS (1 << 5)
|
|
#define NRF_STATUS_MAX_RT (1 << 4)
|
|
|
|
/* 操作结果 */
|
|
#define NRF_OK 0
|
|
#define NRF_ERR_CHECK 1
|
|
|
|
/* 运行时状态 */
|
|
typedef struct {
|
|
uint8_t last_status;
|
|
uint8_t rx_count; /* 接收帧计数 */
|
|
uint8_t bind_state; /* 0=正常,1=对频中 */
|
|
uint8_t ack_pending; /* 有待发送的 ACK payload */
|
|
uint8_t ack_payload[NRF_PAYLOAD_LEN];
|
|
} NRF_Status_t;
|
|
|
|
extern NRF_Status_t __xdata g_nrf_status;
|
|
|
|
void nrf24_init(void);
|
|
uint8_t nrf24_check(void);
|
|
void nrf24_set_channel(uint8_t ch);
|
|
void nrf24_set_address(const uint8_t *addr5);
|
|
void nrf24_set_rx_mode(void); /* 进入 RX 模式 */
|
|
uint8_t nrf24_data_ready(void); /* 检查是否有数据,有则读出到 buf */
|
|
void nrf24_write_ack_payload(const uint8_t *buf); /* 写入 ACK payload */
|
|
|
|
/* IRQ 中断处理 (NRF IRQ 接 P1.7) */
|
|
void nrf24_irq_handler(void);
|
|
|
|
#endif |