#include "app_sx1278.h"
DrvSx1278Struct g_sDrvSx1278={0};
/*
================================================================================
描述 : 硬件复位
输入 :
输出 :
================================================================================
*/
static void app_sx1278_reset(void)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_15);
delay_ms(10);
GPIO_SetBits(GPIOA, GPIO_Pin_15);
delay_ms(10);
}
/*
================================================================================
描述 : 片选0
输入 :
输出 :
================================================================================
*/
static void app_sx1278_cs0(void)
{
GPIO_ResetBits(GPIOB, GPIO_Pin_12);
}
/*
================================================================================
描述 : 片选1
输入 :
输出 :
================================================================================
*/
static void app_sx1278_cs1(void)
{
GPIO_SetBits(GPIOB, GPIO_Pin_12);
}
/*
================================================================================
描述 : SPI字节读写
输入 :
输出 :
================================================================================
*/
static u8 app_sx1278_spi_rw_byte(u8 byte)
{
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
SPI_I2S_SendData(SPI2,byte);
while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);
return SPI_I2S_ReceiveData(SPI2);
}
/*
================================================================================
描述 :应用层sx1278初始化,注册
输入 :
输出 :
================================================================================
*/
static void app_sx1278_hal_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
//复位引脚初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//SPI初始化
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);//SPI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure); //CS
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB,&GPIO_InitStructure); //CLK MOSI
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure); //MISO
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;//
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;//
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2,&SPI_InitStructure);
SPI_Cmd(SPI2,ENABLE);
g_sDrvSx1278.tag_hal_sx1278.sx1278_reset = app_sx1278_reset;
g_sDrvSx1278.tag_hal_sx1278.sx1278_cs_0 = app_sx1278_cs0;
g_sDrvSx1278.tag_hal_sx1278.sx1278_cs_1 = app_sx1278_cs1;
g_sDrvSx1278.tag_hal_sx1278.sx1278_spi_rw_byte = app_sx1278_spi_rw_byte;
drv_sx1278_init(&g_sDrvSx1278);//初始化
printf("app_sx1278_hal_init ok!\n");
/* 接线
* PA15--复位reset
* PB12--片选NSS
* PB13--时钟SCK
* PB14--MISO
* PB15--MOSI
* 3.3V--电源
* GND --GND
*/
}
/*
================================================================================
描述 :
输入 :
输出 :
================================================================================
*/
void app_sx1278_init(void)
{
app_sx1278_hal_init();
bsp_sx1278_set_param(434000000, 12, 9);//无线参数初始化
}
/*
================================================================================
描述 : sx1278线程
输入 :
输出 :
================================================================================
*/
void app_sx1278_thread_entry(void *parameter)
{
app_sx1278_init();
while(1)
{
if(SX1278_ROLE==SX1278_MASTER) //主机模式
{
app_sx1278_master_req_data(); //轮询从机数据
}
bsp_sx1278_recv_check();//数据接收
delay_os(20);
}
}
/*
================================================================================
描述 : 主机请求数据
输入 :
输出 :
================================================================================
*/
void app_sx1278_master_req_data(void)
{
static u32 last_sec_time=0;
u32 now_sec_time=drv_get_sec_counter();
if(now_sec_time-last_sec_time>=5)//5秒请求一次
{
u8 make_buff[50]={0};
u16 make_len=0;
make_len=drv_modbus_send_fun03(1, 0x0000, 2, make_buff, sizeof(make_buff));//组合请求数据
bsp_sx1278_send_level(434000000, 12, 9, make_buff, make_len);//根据无线参数发送modbus数据
last_sec_time=now_sec_time;
}
}
/*
================================================================================
描述 : 主机接收解析
输入 :
输出 :
================================================================================
*/
void app_sx1278_master_recv_parse(u8 *recv_buff, u16 recv_len)
{
u8 data_buff[20]={0};
u16 data_len=drv_modbus_parse_fun03(1, recv_buff, recv_len, data_buff, sizeof(data_buff));//modbus解析
if(data_len>0)
{
printf_hex("master recv= ", data_buff, data_len);//打印应用层数据
}
}
/*
================================================================================
描述 : 从机回复数据
输入 :
输出 :
================================================================================
*/
void app_sx1278_slaver_ack_data(void)
{
u8 make_buff[50]={0};
u16 make_len=0;
//在这里只是模拟测试,我们发送一个固定数据包就行了
make_buff[make_len++]=0x01; //地址码
make_buff[make_len++]=0x03; //功能码
make_buff[make_len++]=0x04; //数据长度
make_buff[make_len++]=0x02;
make_buff[make_len++]=0x92;
make_buff[make_len++]=0xFF;
make_buff[make_len++]=0x9B; //4字节数据
make_buff[make_len++]=0x5A;
make_buff[make_len++]=0x3D; //CRC校验
bsp_sx1278_send_level(434000000, 12, 9, make_buff, make_len);//根据无线参数发送回复数据
}
/*
================================================================================
描述 : 从机接收解析
输入 :
输出 :
================================================================================
*/
void app_sx1278_slaver_recv_parse(u8 *recv_buff, u16 recv_len)
{
printf_hex("slaver recv= ", recv_buff, recv_len);//打印应用层数据
app_sx1278_slaver_ack_data();//回复到主机
}
/*
================================================================================
描述 : 设置无线参数
输入 :
输出 :
================================================================================
*/
void bsp_sx1278_set_param(u32 freq, u8 sf, u8 bw)
{
drv_sx1278_set_rf_freq(&g_sDrvSx1278, freq);
drv_sx1278_set_sf(&g_sDrvSx1278, sf);
drv_sx1278_set_bw(&g_sDrvSx1278, bw);
}
/*
================================================================================
描述 : LORA底层发送函数
输入 :
输出 :
================================================================================
*/
u8 bsp_sx1278_send_level(u32 freq, u8 sf, u8 bw, u8 *buff, u16 le