151 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			151 lines
		
	
	
		
			2.8 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| #ifndef NRF_H__
 | ||
| #define NRF_H__
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| #include "stm32f4xx.h"
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| /* ++++++++++++++++++++++ 重要提示 +++++++++++++++++++++ */
 | ||
| /* ++++++++++本协议假设通信终端都是小端模式+++++++++++++ */
 | ||
| /* ++++++++++++++++没有做大小端适配处理+++++++++++++++++ */
 | ||
| /* ++++++++++++++++++++ 重要提示End ++++++++++++++++++++ */
 | ||
| 
 | ||
| 
 | ||
| /* 通信协议相关数据结构定义 */
 | ||
| 
 | ||
| // 负载数据结构体
 | ||
| typedef struct __packed
 | ||
| {
 | ||
|     u8 len;
 | ||
|     u8 magic_number;
 | ||
|     u8 type;
 | ||
|     u8 load[29];
 | ||
| }nrf_load_struct;
 | ||
| 
 | ||
| 
 | ||
| // 定义控制帧类型
 | ||
| 
 | ||
| #define NRF_TYPE_NULL      0x00    //空操作,可用来维持心跳
 | ||
| #define NRF_TYPE_CONN      0x01    //请求连接
 | ||
| #define NRF_TYPE_CHAN      0x02    //更改信道
 | ||
| #define NRF_TYPE_TIME      0x03    //设置超时时间
 | ||
| #define NRF_TYPE_ASK       0x04    //主机向从机询问是否有数据
 | ||
| #define NRF_TYPE_ANSWER    0x05    //操作或数据回复
 | ||
| #define NRF_TYPE_DATA      0x06    //此帧是数据帧
 | ||
| #define NRF_TYPE_BROAD     0x07    //广播
 | ||
| 
 | ||
| 
 | ||
| // 定义错误类型
 | ||
| #define NRF_OK                  0x00    //无错误
 | ||
| #define NRF_ERR                 0x01    //一般错误
 | ||
| #define NRF_REFUSE              0x02    //拒绝
 | ||
| #define NRF_TIMEOUT             0x03    //超时
 | ||
| #define NRF_MISMATCH            0x04    //魔数不匹配
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| // 定义操作回复数据
 | ||
| #define NRF_ANSWER_OK           (u8 []){NRF_OK},1     
 | ||
| #define NRF_ANSWER_ERR          (u8 []){NRF_ERR},1 
 | ||
| #define NRF_ANSWER_REFUSE       (u8 []){NRF_REFUSE},1
 | ||
| 
 | ||
| 
 | ||
| // 定义连接请求的数据域
 | ||
| typedef struct __packed
 | ||
| {
 | ||
|     u8 addr_src[5];     //请求源的addr
 | ||
|     u8 addr_dst[5];     //请求的目的addr
 | ||
|     u8 chan;
 | ||
| }nrf_conn_struct;
 | ||
| 
 | ||
| // 定义切换信道的数据域
 | ||
| typedef struct __packed
 | ||
| {
 | ||
|     u8 chan;
 | ||
|     u8 times;
 | ||
| }nrf_chan_struct;
 | ||
| 
 | ||
| // 定义设置超时时间的数据域
 | ||
| typedef struct __packed
 | ||
| {
 | ||
|     int connect_time_out;
 | ||
|     int interaction_time_out;
 | ||
| }nrf_time_struct;
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| // 无线通信初始化
 | ||
| int nrf_init(void);
 | ||
| 
 | ||
| // 去初始化
 | ||
| int nrf_deinit(void);
 | ||
| 
 | ||
| // 设置连接状态
 | ||
| int nrf_set_connect_state(int s);
 | ||
| 
 | ||
| // 获取连接状态
 | ||
| int nrf_get_connect_state(void);
 | ||
| 
 | ||
| // 设置通信地址
 | ||
| int nrf_set_addr(const u8 *my,const u8 *dst);
 | ||
| 
 | ||
| // 设置重试次数
 | ||
| int nrf_set_retry_times(int times);
 | ||
| 
 | ||
| // 设置信道
 | ||
| int nrf_set_chan(u8 chan);
 | ||
| 
 | ||
| // 获取当前信道
 | ||
| int nrf_get_chan(void);
 | ||
| 
 | ||
| // 获取发送数据包情况
 | ||
| int nrf_get_packet_num(int *all,int *failed);
 | ||
| 
 | ||
| // 清空数据包发送记录
 | ||
| int nrf_clear_packet_num(void);
 | ||
| 
 | ||
| // 设置超时时间
 | ||
| int nrf_set_time_out(int connect_time_ms,int interaction_time_us);
 | ||
| 
 | ||
| // 读取一个字节数据,NRF_OK成功
 | ||
| int nrf_read_byte(uint8_t *data);
 | ||
| 
 | ||
| // 清空接收区
 | ||
| int nrf_clear(void);
 | ||
| 
 | ||
| // 发送任意长度的数据,成功返回NRF_OK
 | ||
| int nrf_send(void *data,int size,int *rs);
 | ||
| 
 | ||
| // 通过协议发送负载数据,成功返回 NRF_OK
 | ||
| int nrf_send_load(nrf_load_struct *load);
 | ||
| 
 | ||
| // 在接收到数据之后回复对方
 | ||
| int nrf_respond(nrf_load_struct *load);
 | ||
| 
 | ||
| // 设置是否回应,1,不回应,0,回应
 | ||
| int nrf_set_no_respond(int power);
 | ||
| 
 | ||
| // 打包帧,返回NRF_OK成功
 | ||
| int nrf_packet_pack(nrf_load_struct *load,u8 type,void *data,int data_len);
 | ||
| 
 | ||
| // 同步通信信道
 | ||
| int nrf_ctrl_chan(u8 chan);
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| 
 | ||
| #endif
 | ||
| 
 | ||
| 
 |