STM32連接射頻si4438模塊
SI4438射頻模塊參數(shù):
本文引用地址:http://m.butianyuan.cn/article/201807/384188.htm1、頻率范圍:425-525 MHz
2、數(shù)字接收信號強(qiáng)度指示(RSSI)
3、64字節(jié)收發(fā)數(shù)據(jù)寄存器(FIFO)
4、跳頻功能
等!
使用SI的WDS工具生成代碼
1、 選擇仿真模式
2、 芯片選擇si4438 B1模式
3、 Radio Configuration Application
4、 Select Application
1、 Select Project
選擇Bidirectional packet ,雙向通信模式
2、 Configure project 配置工程
Frequency and power: 頻率和功率的設(shè)置,
base freq基頻,中心頻率,
Channel spacing 通道空間,某個(gè)通道回憶 base freq+ channel spacin*num 為頻率通信,當(dāng)然會有小浮動,但是浮動不會超過 Channel spacing。
計(jì)算通道號數(shù)量:
(Base freq + channel spacin*num) >=425MHz
(Base freq + channel spacin*num) =525MHz
所以Base freq的設(shè)置以及channel spacing的設(shè)置會影響到通道的數(shù)量。
Crystal:晶振默認(rèn)!
其他的不動
RF parameter
這里設(shè)置的射頻參數(shù),包括調(diào)制模式、數(shù)據(jù)速率等參數(shù),RSSI threshold設(shè)置信號閾值。數(shù)據(jù)速率射頻之間的距離有關(guān)系,速度越快,對應(yīng)的距離要求越短。所以這應(yīng)該按照自己的需求來選。
Pakect數(shù)據(jù)包的設(shè)置,包括TX和RX緩沖區(qū)的長度、前導(dǎo)碼的配置Preamble、同步字的配置SyncWord、Field對應(yīng)負(fù)載的字節(jié)數(shù)據(jù),注意總的負(fù)載字節(jié)數(shù)為TX和RX閾值,具體分幾個(gè)fields看個(gè)人需求。
NIRQ配置成RX data output,即NIRQ和單片機(jī)引腳相連單片機(jī)可以通過該引腳判斷是否有數(shù)據(jù)接收。低電平有效!然后即可生成代碼!
生成的代碼是基于C8051F910單片機(jī)的,我們所用的是,所以必須做好移植。
SPI移植:
不需要生成spi.c,建立STM32 SPI配置文件:
#include
#include stm32f10x_spi.h
#include STM32SPI2.h
u8 STM32SPI2_ReadWriteByte(u8 TxData)
{
u8 retry=0;
while((SPI2->SR11) == 0) {
retry++;
if(retry>250)
return 0;
}
SPI2->DR=TxData;
retry=0;
while((SPI2->SR10) == 0)//
{
retry++;
if(retry>250)
return 0;
}
return SPI2->DR;
}
//APB2=72M/8=9M
void STM32SPI2_Config(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE );
/* Configure SPI2 pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, GPIO_InitStructure);
/* Configure NSEL pins */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOB, GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_12);
/* SPI2 configuration */
SPI_I2S_DeInit(SPI2);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);
SPI_Cmd(SPI2, DISABLE);
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_128;//SPI_BaudRatePrescaler_64;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI2, SPI_InitStructure);
/* Enable SPI2 */
SPI_Cmd(SPI2, ENABLE);
STM32SPI2_ReadWriteByte(0xff);//啟動傳輸
}
//í?ò?ê±?????üê1?üò???SPIéè±?,2?êyTYPE_SPI_ALL?TD§
void STM32SPI2_Enable(TYPE_SPI type)
{
/*
if(type == TYPE_SPI_FLASH) //這其實(shí)沒啥用
{
GPIO_SetBits(GPIOA,GPIO_Pin_4);//ê§?üRF
GPIO_ResetBits(GPIOC,GPIO_Pin_4);//ê1?üFLASH
}
else
{
*/
// GPIO_SetBits(GPIOC,GPIO_Pin_4);//ê§?üFLASH
GPIO_ResetBits(GPIOB,GPIO_Pin_12);//
/*
}
*/
}
void STM32SPI2_Disable(TYPE_SPI type)
{
if(type == TYPE_SPI_FLASH)
{
GPIO_SetBits(GPIOC,GPIO_Pin_4);//ê§?üFLASH
}
else if(type == TYPE_SPI_RF)
{
GPIO_SetBits(GPIOB,GPIO_Pin_12);//ê§?üRF
}
else
{
GPIO_SetBits(GPIOC,GPIO_Pin_4);//ê§?üFLASH
GPIO_SetBits(GPIOA,GPIO_Pin_4);//ê§?üRF
}
}
radio.c radio hal層 spi接口修改處
void radio_hal_SpiWriteByte(u8 byteToWrite)
{
STM32SPI2_ReadWriteByte(byteToWrite);
}
u8 radio_hal_SpiReadByte(void)
{
return STM32SPI2_ReadWriteByte(0xFF);
}
void radio_hal_SpiWriteData(u8 byteCount, u8* pData)
{
while(byteCount--)
{
STM32SPI2_ReadWriteByte(*pData++);
}
}
void radio_hal_SpiReadData(u8 byteCount, u8* pData)
{
while(byteCount--)
{
*pData++ = STM32SPI2_ReadWriteByte(0xFF);
}
}
Radio_Config:配置SDN power IRQ引腳
void Radio_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//oíFLASH12ó?ò???SPI,SPIò??-?úFLASHμ?3?ê??ˉ?Dμ÷ó?
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);
//RF_POWER
GPIO_InitStructure.GPIO_Pin = RF_POWER_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(RF_POWER_PORT, GPIO_InitStructure);
GPIO_SetBits(RF_POWER_PORT, RF_POWER_PIN);
//RF_ON
GPIO_InitStructure.GPIO_Pin = RF_
GPIO_InitStructure.GPIO_Pin = RF_ON_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(RF_ON_PORT, GPIO_InitStructure);
GPIO_SetBits(RF_ON_PORT, RF_ON_PIN);
//RF_SDN
GPIO_InitStructure.GPIO_Pin = RF_SDN_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(RF_SDN_PORT, GPIO_InitStructure);
GPIO_SetBits(RF_SDN_PORT, RF_SDN_PIN);
//RF_IRQ
GPIO_InitStructure.GPIO_Pin = RF_IRQ_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//????ê?è?
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(RF_IRQ_PORT, GPIO_InitStructure);
}
接收信號:
u8 radio_hal_NirqLevel(void)
{
return GPIO_ReadInputDataBit(RF_IRQ_PORT, RF_IRQ_PIN);
}
void radio_hal_AssertShutdown(void)
{
GPIO_SetBits(RF_SDN_PORT, RF_SDN_PIN);
}
void radio_hal_DeassertShutdown(void)
{
GPIO_ResetBits(RF_SDN_PORT, RF_SDN_PIN);
}
底層配置完畢,配置bsh頭文件:
#include stdio.h
#include compiler_defs.h
//#include platform_defs.h
//#include hardware_defs.h
//#include application_defs.h
//#include cdd_common.h
#include radio_config.h
#include radio.h
//#include sample_code_func.h
#include radio_hal.h
#define SILABS_RADIO_SI446X
#include radio_comm.h
#include si446x_api_lib.h
#include si446x_defs.h
//#include si446x_nirq.h
#include
//#include driversradioSi446xsi446x_patch.h
把不是自己的平臺的屏蔽了!
Main接收端
接收函數(shù):
int SI4338RecvData(void* buf,u32 len){
u16 i,crc16;
u8* ptr;
SEGMENT_VARIABLE(bMain_IT_Status, U8, SEG_XDATA);
ptr = (u8*)buf;
if(ptr == NULL) return -1;
bMain_IT_Status = bRadio_Check_Tx_RX();
switch (bMain_IT_Status)
{
case SI446X_CMD_GET_INT_STATUS_REP_PH_PEND_PACKET_SENT_PEND_BIT:{
vRadio_StartRX(pRadioConfiguration->Radio_ChannelNumber, 64);
///* Clear Packet Sending flag */
}
break;
case SI446X_CMD_GET_INT_STATUS_REP_PH_PEND_PACKET_RX_PEND_BIT:{
memset(ptr,0,len);
memcpy(ptr,SI4338RecvData,SI4338RecvLen);
//recv OK ,you must start RX!
vRadio_StartRX(pRadioConfiguration->Radio_ChannelNumber,pRadioConfiguration->Radio_PacketLength);
return SI4338RecvLen;
}
break;
default:
break;
} /* switch */
return -1;
}
//注意:需要在U8 bRadio_Check_Tx_RX(void)函數(shù)把接收的數(shù)據(jù)拷貝出來,然后再RECV函數(shù)memcpy過來就可以了。
U8 bRadio_Check_Tx_RX(void){
……………………………………….
if(Si446xCmd.GET_INT_STATUS.PH_PEND SI446X_CMD_GET_INT_STATUS_REP_PH_PEND_PACKET_RX_PEND_BIT)
{
/* Packet RX */
/* Get payload length */
si446x_fifo_info(0x00);
si446x_read_rx_fifo(Si446xCmd.FIFO_INFO.RX_FIFO_COUNT, rxInformation[0]);
SI4338RecvLen =Si446xCmd.FIFO_INFO.RX_FIFO_COUNT;
memcpy(SI4338RecvData,rxInformation,Si446xCmd.FIFO_INFO.RX_FIFO_COUNT);
return SI446X_CMD_GET_INT_STATUS_REP_PH_PEND_PACKET_RX_PEND_BIT;
}
….
}
unsigned char buf[64];
int recvLen;
vRadio_StartRX(pRadioConfiguration->Radio_ChannelNumber,0u); 啟動接收
while(1){
if((recvLen = SI4338RecvData(void*( buf),64)) >0){
//處理接收的數(shù)據(jù)
}
}
發(fā)送端:使用這個(gè)函數(shù)發(fā)送既可以!
int SI4338SendData(void* buf,u32 len){
u8* ptr;
int ret = -1;
u16 i;
SEGMENT_VARIABLE(bMain_IT_Status, U8, SEG_XDATA);
ptr = (u8*)buf;
if(buf == NULL) return -1;
vRadio_StartTx_Variable_Packet(pRadioConfiguration->Radio_ChannelNumber,ptr, len);
#if 1
bMain_IT_Status = bRadio_Check_Tx_RX();
switch (bMain_IT_Status)
{
case SI446X_CMD_GET_INT_STATUS_REP_PH_PEND_PACKET_SENT_PEND_BIT:
//vRadio_StartTx_Variable_Packet(pRadioConfiguration->Radio_ChannelNumber,ptr, len);
vRadio_StartRX(pRadioConfiguration->Radio_ChannelNumber, pRadioConfiguration->Radio_PacketLength);
ret = 0;
break;
case SI446X_CMD_GET_INT_STATUS_REP_PH_PEND_PACKET_RX_PEND_BIT:{
vRadio_StartRX(pRadioConfiguration->Radio_ChannelNumber, pRadioConfiguration->Radio_PacketLength);
return SI4338RecvLen;
}
default: ;break;
}
#endif
return ret;
}
- STM32單片機(jī)中文官網(wǎng)
- STM32單片機(jī)官方開發(fā)工具
- STM32單片機(jī)參考設(shè)計(jì)
評論