277 lines
7.2 KiB
C
277 lines
7.2 KiB
C
#ifndef __RF_2_4G_H__
|
|
#define __RF_2_4G_H__
|
|
|
|
#ifdef __cplusplus
|
|
extern "C"
|
|
{
|
|
#endif
|
|
|
|
#include "xc6xxx.h"
|
|
|
|
// <<< Use Configuration Wizard in Context Menu >>>\n
|
|
|
|
// <h> XINCX_RF_ADDR - 设置地址/同步字
|
|
//==========================================================
|
|
// <o> XINCX_RF_ADDR_L - 地址/同步字高4字节
|
|
#ifndef XINCX_RF_ADDR_L
|
|
#define XINCX_RF_ADDR_L 0x1AB51376 //EMN地址
|
|
#endif
|
|
|
|
// <o> XINCX_RF_ADDR_H - 地址/同步字低1字节
|
|
#ifndef XINCX_RF_ADDR_H
|
|
#define XINCX_RF_ADDR_H 0x00
|
|
#endif
|
|
|
|
// <o> XINCX_RF_TX_ADDR_WIDTH - 地址/同步字宽度
|
|
// <3=> 3 Bytes
|
|
// <4=> 4 Bytes
|
|
// <5=> 5 Bytes
|
|
#ifndef XINCX_RF_TX_ADDR_WIDTH
|
|
#define XINCX_RF_TX_ADDR_WIDTH 4
|
|
#endif
|
|
//==========================================================
|
|
// </h>
|
|
|
|
// <o> XINCX_RF_CHANNEL - 设置频率/信道
|
|
// <2402=> ADV_CHANNEL_37
|
|
// <2426=> ADV_CHANNEL_38
|
|
// <2480=> ADV_CHANNEL_39
|
|
#ifndef XINCX_RF_CHANNEL
|
|
#define XINCX_RF_CHANNEL 2402 //EMN工作在37信道
|
|
#endif
|
|
|
|
// <o> XINCX_RF_POWER - 设置发射功率
|
|
//4 - 17对应-4dBm - 8dBm
|
|
#ifndef XINCX_RF_POWER
|
|
#define XINCX_RF_POWER 8 //3.6dBm
|
|
#endif
|
|
|
|
// <o> XINCX_RF_RATE - 设置传输速率
|
|
// <0x02=> 1M
|
|
// <0x0A=> 2M
|
|
// <0x22=> 250k
|
|
// <0x2A=> 125k
|
|
#ifndef XINCX_RF_RATE
|
|
#define XINCX_RF_RATE 0x02
|
|
#endif
|
|
|
|
// <q> XINCX_RF_WHITEN - 使能2.4g白化
|
|
#ifndef XINCX_RF_WHITEN
|
|
#define XINCX_RF_WHITEN 0 //0-禁止2.4g白化, BLE使用软件白化, 1-使能2.4G白化
|
|
#endif
|
|
|
|
// <q> XINCX_RF_FEC - 使能前向纠错
|
|
#ifndef XINCX_RF_FEC
|
|
#define XINCX_RF_FEC 0 //0-蓝牙广播不使能, 1-2.4G使能
|
|
#endif
|
|
|
|
// <q> XINCX_RF_LONG_PLD - 动态长包类型
|
|
#ifndef XINCX_RF_LONG_PLD_TYPE
|
|
#define XINCX_RF_LONG_PLD_TYPE 1 //0-最大支持128byte, 1-最大64字节
|
|
#endif
|
|
|
|
// <q> XINCX_RF_LONG_PLD - 使能动态长包
|
|
#ifndef XINCX_RF_LONG_PLD
|
|
#define XINCX_RF_LONG_PLD 1
|
|
#endif
|
|
|
|
// <e> XINCX_RF_DPL - 使能动态负载长度
|
|
#ifndef XINCX_RF_DPL
|
|
#define XINCX_RF_DPL 0 //不带ACK模式,不使用动态负载长度
|
|
#endif
|
|
// <o> XINCX_RF_DYNPD - 使能各个PIPE动态负载长度
|
|
#ifndef XINCX_RF_DYNPD
|
|
#define XINCX_RF_DYNPD 0x00//0x3f
|
|
#endif
|
|
// </e>
|
|
|
|
// <e> XINCX_RF_CRC_ENABLE - 使能硬件CRC
|
|
#ifndef XINCX_RF_CRC_ENABLE
|
|
#define XINCX_RF_CRC_ENABLE 0 //BLE模式不使能硬件CRC,使用软件CRC
|
|
#endif
|
|
|
|
// <o> XINCX_RF_CRC_BYTE - CRC字节长度
|
|
// <1=> 1 Byte
|
|
// <2=> 2 Bytes
|
|
#ifndef XINCX_RF_CRC_BYTE
|
|
#define XINCX_RF_CRC_BYTE 2
|
|
#endif
|
|
|
|
// <q> XINCX_RF_CRC_SCOPE_HEADER - 2.4g CRC Scope Header
|
|
#ifndef XINCX_RF_CRC_SCOPE_HEADER
|
|
#define XINCX_RF_CRC_SCOPE_HEADER 1
|
|
#endif
|
|
|
|
// <q> XINCX_RF_CRC_SCOPE_ADDR - 2.4g CRC Scope Addr
|
|
#ifndef XINCX_RF_CRC_SCOPE_ADDR
|
|
#define XINCX_RF_CRC_SCOPE_ADDR 1
|
|
#endif
|
|
// </e>
|
|
|
|
// <e> XINCX_RF_Auto ACK - 使能ACK负载
|
|
#ifndef XINCX_RF_ACK_PAY
|
|
#define XINCX_RF_ACK_PAY 0
|
|
#endif
|
|
|
|
// <q> XINCX_RF_DYN_ACK - 使能命令W_TX_PAYLOAD_NOACK
|
|
#ifndef XINCX_RF_DYN_ACK
|
|
#define XINCX_RF_DYN_ACK 1 //使能不带ACK的TX包
|
|
#endif
|
|
|
|
// <o> XINCX_RF_ARC - 设置自动重发次数
|
|
#ifndef XINCX_RF_ARC
|
|
#define XINCX_RF_ARC 0
|
|
#endif
|
|
|
|
// <o> XINCX_RF_ARD - 设置自动重发延时
|
|
#ifndef XINCX_RF_ARD
|
|
#define XINCX_RF_ARD 0
|
|
#endif
|
|
// </e>
|
|
|
|
// <h> XINCX_RF_PIPE - 设置数据PIPE
|
|
//==========================================================
|
|
// <o> XINCX_RF_PIPE_LEN - 数据PIPE长度
|
|
#ifndef XINCX_RF_PIPE_LEN
|
|
#define XINCX_RF_PIPE_LEN 32
|
|
#endif
|
|
|
|
// <q> XINCX_RF_PIPE_ENAA - 使能PIPE自动ACK
|
|
#ifndef XINCX_RF_PIPE_ENAA
|
|
#define XINCX_RF_PIPE_ENAA 0
|
|
#endif
|
|
//==========================================================
|
|
// </h>
|
|
|
|
// <h> XINCX_RF_PREAMBLE - 设置前导码
|
|
//==========================================================
|
|
// <o> XINCX_RF_PREAMBLE - 设置前导码内容
|
|
#ifndef XINCX_RF_PREAMBLE
|
|
#define XINCX_RF_PREAMBLE 0x710f5555 //BLE前导码为10101010或01010101
|
|
#endif
|
|
|
|
// <o> XINCX_RF_PREAMBLE_NUM - 设置前导码长度
|
|
// <0=> 1 Byte
|
|
// <1=> 2 Bytes
|
|
// <2=> 3 Bytes
|
|
// <3=> 4 Bytes
|
|
#ifndef XINCX_RF_PREAMBLE_NUM
|
|
#define XINCX_RF_PREAMBLE_NUM 0
|
|
#endif
|
|
|
|
// <o> XINCX_RF_PREAMBLE_TYPE - 设置前导码类型
|
|
#ifndef XINCX_RF_PREAMBLE_TYPE
|
|
#define XINCX_RF_PREAMBLE_TYPE 0
|
|
#endif
|
|
//==========================================================
|
|
// </h>
|
|
|
|
// <h> XINCX_RF_GUARD - 设置保护字
|
|
//==========================================================
|
|
// <o> XINCX_RF_GUARD - 保护字内容
|
|
#ifndef XINCX_RF_GUARD
|
|
#define XINCX_RF_GUARD 0x8fc9
|
|
#endif
|
|
|
|
// <q> XINCX_RF_GUARD_CFG - 保护字配置
|
|
#ifndef XINCX_RF_GUARD_CFG
|
|
#define XINCX_RF_GUARD_CFG 0
|
|
#endif
|
|
//==========================================================
|
|
// </h>
|
|
|
|
// <h> XINCX_RF_CRY_CPT_ARRAY - 设置晶振电容
|
|
//==========================================================
|
|
// <o> XINCX_RF_CRY_CPT_ARRAY - 设置晶振电容
|
|
#ifndef XINCX_RF_CRY_CPT_ARRAY
|
|
#define XINCX_RF_CRY_CPT_ARRAY 0x36
|
|
#endif
|
|
//==========================================================
|
|
// </h>
|
|
|
|
// <q> XINCX_RF_NVIC_MODE - 使能中断
|
|
#ifndef XINCX_RF_NVIC_MODE
|
|
#define XINCX_RF_NVIC_MODE 1
|
|
#endif
|
|
|
|
// <<< end of configuration section >>>
|
|
|
|
|
|
/**************************** RF Command Code *******************************/
|
|
#define R_REG 0x00 //寄存器读操作
|
|
#define W_REG 0x20 //寄存器写操作
|
|
#define R_RX_PL_WID 0x60 //从FIFO读取收到的数据长度
|
|
#define R_RX_PLOAD 0x61 //从FIFO读取收到的数据
|
|
#define W_TX_PLOAD 0xA0 //向FIFO写入发送的数据
|
|
#define W_TX_PLOAD_NOACK 0xB0 //向FIFO写入发送的数据,不带ACK
|
|
#define W_ACK_PLOAD 0xA8 //向FIFO写入发送ACK数据
|
|
#define FLUSH_TX 0xE1 //清空TX FIFO
|
|
#define FLUSH_RX 0xE2 //清空RX FIFO
|
|
#define REUSE_TX_PL 0xE3
|
|
|
|
#define CMD_NOP 0xFF //空操作
|
|
#define CMD_DONE 0x100
|
|
|
|
/* 状态寄存器 */
|
|
#define STAT_TX_REUSE (1 << 6)
|
|
#define STAT_TX_FULL (1 << 5) //TX_FIFO满标志
|
|
#define STAT_TX_EMPTY (1 << 4) //TX_FIFO空标志
|
|
#define STAT_RX_FULL (1 << 1) //RX_FIFO满标志
|
|
#define STAT_RX_EMPTY (1 << 0) //RX_FIFO满标志
|
|
|
|
/* 中断状态位 */
|
|
#define MASK_RX_DR (1 << 6) //RX_FIFO有效标志位,写1清除
|
|
#define MASK_TX_DS (1 << 5) //发射完成中断,写1清除
|
|
#define MASK_MAX_RT (1 << 4) //达到最大重发次数,写1清除
|
|
#define MASK_ALL_INTER (MASK_RX_DR | MASK_TX_DS| MASK_MAX_RT)
|
|
|
|
/* CRC校验位 */
|
|
#define CRC_1BIT 1
|
|
#define CRC_2BIT 2
|
|
|
|
/* 工作模式 */
|
|
#define TX_MODE 0
|
|
#define RX_MODE 1
|
|
|
|
/* 速率设置 */
|
|
#define RATE_1M 0x02
|
|
#define RATE_2M 0x0A
|
|
#define RATE_250K 0x22
|
|
#define RATE_125K 0x2A
|
|
|
|
#define SEND_CMD_CNT 6
|
|
#define RECV_CMD_CNT 4
|
|
|
|
#define RF_BUFF_LEN XINCX_RF_PIPE_LEN
|
|
|
|
#define RF_BLE_ADDR 0x1AB51376
|
|
|
|
#define RF_BLE_CHN37 2402
|
|
#define RF_BLE_CHN38 2426
|
|
#define RF_BLE_CHN39 2480
|
|
|
|
|
|
//BLE广播Head
|
|
#define BLE_ADV_IND 0x00 //通用广播指示
|
|
#define BLE_ADV_DIRECT_IND 0x01 //定向连接指示
|
|
#define BLE_ADV_NONCONN_IND 0x02 //不可连接指示
|
|
#define BLE_SCAN_REQ 0x03 //主动扫描请求
|
|
#define BLE_SCAN_RSP 0x04 //主动扫描响应
|
|
#define BLE_CONNECT_REQ 0x05 //连接请求
|
|
#define BLE_ADV_SCAN_IND 0x06 //可扫描指示
|
|
|
|
#define LOGI(...) printf(__VA_ARGS__)
|
|
|
|
extern void nrf_24g_init(uint32_t addr, uint16_t chan);
|
|
extern void nrf_ble_init(uint32_t addr, uint16_t chan);
|
|
extern void nrf_set_power(uint8_t tx_pwr);
|
|
extern void nrf_set_channel(uint16_t channel);
|
|
extern uint8_t nrf_ble_send(uint8_t *data, uint8_t txlen);
|
|
extern uint8_t nrf_ble_recv(uint8_t *data, uint8_t *rssi);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif /* __RF_2_4G__ */
|