Add core firmware modules: input, menu, NRF24L01, OLED, protocol, PWM, storage + UI assets
This commit is contained in:
114
Core/Inc/nrf24l01.h
Normal file
114
Core/Inc/nrf24l01.h
Normal file
@@ -0,0 +1,114 @@
|
||||
/**
|
||||
* NRF24L01+ 2.4G 无线模块驱动
|
||||
* 硬件 SPI1: PB3=SCK, PB4=MISO, PB5=MOSI
|
||||
* CE=PB7, CSN=PB6, IRQ=PD2
|
||||
*/
|
||||
#ifndef NRF24L01_H
|
||||
#define NRF24L01_H
|
||||
|
||||
#include "main.h"
|
||||
#include "spi.h"
|
||||
#include "config.h"
|
||||
|
||||
/* NRF24L01 寄存器地址 */
|
||||
#define NRF_CONFIG 0x00
|
||||
#define NRF_EN_AA 0x01
|
||||
#define NRF_EN_RXADDR 0x02
|
||||
#define NRF_SETUP_AW 0x03
|
||||
#define NRF_SETUP_RETR 0x04
|
||||
#define NRF_RF_CH 0x05
|
||||
#define NRF_RF_SETUP 0x06
|
||||
#define NRF_STATUS 0x07
|
||||
#define NRF_OBSERVE_TX 0x08
|
||||
#define NRF_RPD 0x09
|
||||
#define NRF_RX_ADDR_P0 0x0A
|
||||
#define NRF_RX_ADDR_P1 0x0B
|
||||
#define NRF_RX_ADDR_P2 0x0C
|
||||
#define NRF_RX_ADDR_P3 0x0D
|
||||
#define NRF_RX_ADDR_P4 0x0E
|
||||
#define NRF_RX_ADDR_P5 0x0F
|
||||
#define NRF_TX_ADDR 0x10
|
||||
#define NRF_RX_PW_P0 0x11
|
||||
#define NRF_RX_PW_P1 0x12
|
||||
#define NRF_RX_PW_P2 0x13
|
||||
#define NRF_RX_PW_P3 0x14
|
||||
#define NRF_RX_PW_P4 0x15
|
||||
#define NRF_RX_PW_P5 0x16
|
||||
#define NRF_FIFO_STATUS 0x17
|
||||
#define NRF_DYNPD 0x1C
|
||||
#define NRF_FEATURE 0x1D
|
||||
|
||||
/* 命令 */
|
||||
#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_STATUS_RX_DR 0x40
|
||||
#define NRF_STATUS_TX_DS 0x20
|
||||
#define NRF_STATUS_MAX_RT 0x10
|
||||
|
||||
/* 数据速率 */
|
||||
#define NRF_RATE_250K 0x20
|
||||
#define NRF_RATE_1M 0x00
|
||||
#define NRF_RATE_2M 0x08
|
||||
|
||||
/* 发射功率 */
|
||||
#define NRF_PA_MIN 0x00
|
||||
#define NRF_PA_LOW 0x02
|
||||
#define NRF_PA_HIGH 0x04
|
||||
#define NRF_PA_MAX 0x06
|
||||
|
||||
/* 地址宽度 */
|
||||
#define NRF_ADDR_WIDTH 5
|
||||
|
||||
/* 最大通道数 */
|
||||
#define NRF_MAX_CHANNEL 125
|
||||
|
||||
/* 宏: 引脚操作 */
|
||||
#define NRF_CE_H() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_SET)
|
||||
#define NRF_CE_L() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_7, GPIO_PIN_RESET)
|
||||
#define NRF_CSN_H() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_SET)
|
||||
#define NRF_CSN_L() HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET)
|
||||
#define NRF_IRQ_READ() HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_2)
|
||||
|
||||
/* 函数声明 */
|
||||
void NRF24L01_Init(void);
|
||||
uint8_t NRF24L01_Check(void); /* 检测模块是否存在 */
|
||||
|
||||
/* 基本配置 */
|
||||
void NRF24L01_SetChannel(uint8_t ch);
|
||||
void NRF24L01_SetRate(uint8_t rate);
|
||||
void NRF24L01_SetPower(uint8_t power);
|
||||
void NRF24L01_SetTXAddr(const uint8_t *addr);
|
||||
void NRF24L01_SetRXAddr(uint8_t pipe, const uint8_t *addr);
|
||||
|
||||
/* 发送/接收 */
|
||||
void NRF24L01_TXMode(void);
|
||||
void NRF24L01_RXMode(void);
|
||||
uint8_t NRF24L01_TxPacket(const uint8_t *data, uint8_t len);
|
||||
uint8_t NRF24L01_RxPacket(uint8_t *data);
|
||||
uint8_t NRF24L01_IsDataReady(void);
|
||||
|
||||
/* 对频相关 */
|
||||
void NRF24L01_BindMode(void); /* 进入对频模式 */
|
||||
uint8_t NRF24L01_BindScan(uint8_t *found_addr); /* 扫描对频设备 */
|
||||
|
||||
/* 低层 SPI 操作 */
|
||||
uint8_t NRF24L01_ReadReg(uint8_t reg);
|
||||
void NRF24L01_WriteReg(uint8_t reg, uint8_t value);
|
||||
void NRF24L01_ReadBuf(uint8_t reg, uint8_t *buf, uint8_t len);
|
||||
void NRF24L01_WriteBuf(uint8_t reg, const uint8_t *buf, uint8_t len);
|
||||
|
||||
/* 全局状态 */
|
||||
extern uint8_t nrf_connected;
|
||||
extern uint8_t nrf_bind_phrase[7]; /* 对频短语, 最多6字符+'\0' */
|
||||
extern uint8_t nrf_channel;
|
||||
extern uint8_t nrf_tx_addr[NRF_ADDR_WIDTH];
|
||||
|
||||
#endif /* NRF24L01_H */
|
||||
Reference in New Issue
Block a user