/**
|
******************************************************************************
|
* @file : KWireLess.c
|
* @brief : K-WireLess Protocol program body
|
******************************************************************************
|
*/
|
#include "KWireLess.h"
|
#include "functions.h"
|
#include "string.h"
|
#include "PLCFunctions.h"
|
#include "stm32f0xx_hal.h"
|
|
|
/**************************************************************************************************************************************
|
Demo 程序流程 RadioEnableMaster=true 为主机端,主机端发送一个"PING"数据后切换到接收,等待从机返回的应答"PONG"数据LED闪烁
|
|
RadioEnableMaster=false 为从机端,从机端接收到主机端发过来的"PING"数据后LED闪烁并发送一个"PONG"数据作为应答
|
***************************************************************************************************************************************/
|
|
//#define USE_MODEM_LORA
|
#define USE_MODEM_FSK
|
|
#define REGION_CN779
|
|
#if defined( REGION_AS923 )
|
|
#define RF_FREQUENCY 923000000 // Hz
|
|
#elif defined( REGION_AU915 )
|
|
#define RF_FREQUENCY 915000000 // Hz
|
|
#elif defined( REGION_CN779 )
|
|
#define RF_FREQUENCY 433000000 // Hz
|
|
#elif defined( REGION_EU868 )
|
|
#define RF_FREQUENCY 868000000 // Hz
|
|
#elif defined( REGION_KR920 )
|
|
#define RF_FREQUENCY 920000000 // Hz
|
|
#elif defined( REGION_IN865 )
|
|
#define RF_FREQUENCY 865000000 // Hz
|
|
#elif defined( REGION_US915 )
|
|
#define RF_FREQUENCY 915000000 // Hz
|
|
#elif defined( REGION_US915_HYBRID )
|
|
#define RF_FREQUENCY 915000000 // Hz
|
|
#else
|
|
#error "Please define a frequency band in the compiler options."
|
|
#endif
|
|
#define nChannelBandwidth 100000 // Hz
|
#define TX_OUTPUT_POWER 22 // 22 dBm
|
|
extern bool IrqFired;
|
|
bool RadioEnableMaster=true;//主从选择
|
uint32_t nRadioChannel=0;
|
uint16_t crc_value;
|
/*!
|
* Radio events function pointer
|
*/
|
|
static RadioEvents_t RadioEvents;
|
|
#if defined( USE_MODEM_LORA )
|
|
#define LORA_BANDWIDTH 0 // [0: 125 kHz,
|
// 1: 250 kHz,
|
// 2: 500 kHz,
|
// 3: Reserved]
|
#define LORA_SPREADING_FACTOR 9 // [SF7..SF12]
|
#define LORA_CODINGRATE 4 // [1: 4/5,
|
// 2: 4/6,
|
// 3: 4/7,
|
// 4: 4/8]
|
#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
|
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
|
#define LORA_FIX_LENGTH_PAYLOAD_ON false
|
#define LORA_IQ_INVERSION_ON false
|
|
#elif defined( USE_MODEM_FSK )
|
|
#define FSK_FDEV 12e3 // Hz
|
#define FSK_DATARATE 19.2e3 // bps
|
#define FSK_BANDWIDTH 50e3 // Hz >> DSB in sx126x
|
#define FSK_AFC_BANDWIDTH 200e3 // Hz
|
#define FSK_PREAMBLE_LENGTH 5 // Same for Tx and Rx
|
#define FSK_FIX_LENGTH_PAYLOAD_ON false
|
|
#else
|
#error "Please define a modem in the compiler options."
|
#endif
|
|
typedef enum
|
{
|
LOWPOWER,
|
RX,
|
RX_TIMEOUT,
|
RX_ERROR,
|
TX,
|
TX_TIMEOUT,
|
}States_t;
|
|
#define RX_TIMEOUT_VALUE 100
|
#define BUFFER_SIZE 32 // Define the payload size here
|
|
const uint8_t PingMsg[] = "PING";
|
const uint8_t PongMsg[] = "PONG";
|
|
uint16_t BufferSize = BUFFER_SIZE;
|
uint8_t TX_Buffer[BUFFER_SIZE];
|
uint8_t RX_Buffer[BUFFER_SIZE];
|
|
|
States_t State = LOWPOWER;
|
|
int8_t RssiValue = 0;
|
int8_t SnrValue = 0;
|
|
//KWStates KW_State=KW_PON;
|
|
void LedToggle(void)
|
{
|
// GPIO_WriteBit( LED1_PORT, LED1_PIN,Bit_RESET);//LED闪烁
|
// HAL_Delay_nMS(10);
|
// GPIO_WriteBit( LED1_PORT, LED1_PIN,Bit_SET);
|
|
LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_13);
|
}
|
|
int KWireLessInit(bool bRadioEnableMaster, uint32_t nChn)
|
{
|
|
RadioEnableMaster = bRadioEnableMaster;
|
// Radio initialization
|
RadioEvents.TxDone = OnTxDone;
|
RadioEvents.RxDone = OnRxDone;
|
RadioEvents.TxTimeout = OnTxTimeout;
|
RadioEvents.RxTimeout = OnRxTimeout;
|
RadioEvents.RxError = OnRxError;
|
|
|
Radio.Init( &RadioEvents );
|
nRadioChannel = nChn;
|
Radio.SetChannel( RF_FREQUENCY + nRadioChannel * nChannelBandwidth );
|
|
// Radio.WriteBuffer(0x06C0,data,2);
|
// Radio.ReadBuffer(0x06C0,test,2);
|
|
#if defined( USE_MODEM_LORA )
|
|
Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
|
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
|
LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
|
true, 0, 0, LORA_IQ_INVERSION_ON, 3000 );
|
|
Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
|
LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
|
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
|
0, true, 0, 0, LORA_IQ_INVERSION_ON, false );
|
|
#elif defined( USE_MODEM_FSK )
|
|
Radio.SetTxConfig( MODEM_FSK, TX_OUTPUT_POWER, FSK_FDEV, 0,
|
FSK_DATARATE, 0,
|
FSK_PREAMBLE_LENGTH, FSK_FIX_LENGTH_PAYLOAD_ON,
|
true, 0, 0, 0, 3000 );
|
|
Radio.SetRxConfig( MODEM_FSK, FSK_BANDWIDTH, FSK_DATARATE,
|
0, FSK_AFC_BANDWIDTH, FSK_PREAMBLE_LENGTH,
|
0, FSK_FIX_LENGTH_PAYLOAD_ON, 0, true,
|
0, 0,false, false );
|
#else
|
#error "Please define a frequency band in the compiler options."
|
#endif
|
SX126xSetRxTxFallbackMode(0x40); // 0x40-> FS 0x30 -> SD_XOSC 0x20 -> SD_RC
|
return 0;
|
}
|
int KWireLessStart(void)
|
{
|
if(RadioEnableMaster)
|
{
|
|
SendPingMsg();
|
/*
|
TX_Buffer[0] = 'P';
|
TX_Buffer[1] = 'I';
|
TX_Buffer[2] = 'N';
|
TX_Buffer[3] = 'G';
|
TX_Buffer[4] = 'G';
|
TX_Buffer[5] = 'G';
|
TX_Buffer[6] = 'G';
|
TX_Buffer[7] = 'G';
|
|
|
|
crc_value=RadioComputeCRC(TX_Buffer,8,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
|
TX_Buffer[8]=crc_value>>8;
|
TX_Buffer[9]=crc_value;
|
Radio.Send( TX_Buffer, 10);
|
*/
|
}
|
else
|
{
|
Radio.Rx( RX_TIMEOUT_VALUE );
|
}
|
|
// while( 1 )
|
// {
|
// Radio.IrqProcess( ); // Process Radio IRQ
|
// }
|
return 0;
|
}
|
|
void SendPingMsg()
|
{
|
int len1=12;
|
TX_Buffer[0] = 'P';
|
TX_Buffer[1] = 'I';
|
TX_Buffer[2] = 'N';
|
TX_Buffer[3] = 'G';
|
TX_Buffer[4] = KMem.WY[1]>>8;
|
TX_Buffer[5] = KMem.WY[1];
|
TX_Buffer[6] = KMem.WY[2]>>8;
|
TX_Buffer[7] = KMem.WY[2];
|
|
crc_value=RadioComputeCRC(TX_Buffer,len1,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
|
TX_Buffer[len1]=crc_value>>8;
|
TX_Buffer[len1+1]=crc_value;
|
Radio.Send( TX_Buffer, len1+2);
|
}
|
void SendPongMsg(int len1)
|
{
|
TX_Buffer[0] = 'P';
|
TX_Buffer[1] = 'O';
|
TX_Buffer[2] = 'N';
|
TX_Buffer[3] = 'G';
|
TX_Buffer[4] = KMem.WX[0]>>8;
|
TX_Buffer[5] = KMem.WX[0];
|
TX_Buffer[6] = KMem.WX[1]>>8;
|
TX_Buffer[7] = KMem.WX[1];
|
|
crc_value=RadioComputeCRC(TX_Buffer,len1,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
|
TX_Buffer[len1]=crc_value>>8;
|
TX_Buffer[len1+1]=crc_value;
|
Radio.Send( TX_Buffer, len1+2);
|
|
}
|
void OnTxDone( void )
|
{
|
Radio.Standby();
|
Radio.Rx( RX_TIMEOUT_VALUE ); //进入接收
|
LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);
|
int us2=GetuS();
|
KMem.ScanTimeuS=us2-KMem.LastScanTime;
|
KMem.LastScanTime = us2;
|
if (KMem.ScanTimeuS < KMem.MinScanTimeuS) {KMem.MinScanTimeuS = KMem.ScanTimeuS;}
|
if (KMem.ScanTimeuS > KMem.MaxScanTimeuS) {KMem.MaxScanTimeuS = KMem.ScanTimeuS;}
|
|
}
|
|
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
|
{
|
BufferSize = size;
|
memcpy( RX_Buffer, payload, BufferSize );
|
RssiValue = rssi;
|
SnrValue = snr;
|
KMem.WDT[40]=rssi; //Radio.Rssi(MODEM_FSK);;
|
KMem.WDT[41]=snr;
|
KMem.WDT[42]++;
|
|
Radio.Standby();
|
|
if(RadioEnableMaster)
|
{
|
if(memcmp(RX_Buffer,PongMsg,4)==0)
|
{
|
LedToggle();//LED闪烁
|
KMem.WX[1]=(RX_Buffer[4]<<8) + RX_Buffer[5];
|
KMem.WX[2]=(RX_Buffer[6]<<8) + RX_Buffer[7];
|
}
|
SendPingMsg();
|
}
|
else
|
{
|
if(memcmp(RX_Buffer,PingMsg,4)==0)
|
{
|
LedToggle();//LED闪烁
|
SendPongMsg(size-2);
|
KMem.WX[1]=(RX_Buffer[4]<<8) + RX_Buffer[5];
|
KMem.WX[2]=(RX_Buffer[6]<<8) + RX_Buffer[7];
|
|
// KMem.WY[0]=(RX_Buffer[4]<<8) + RX_Buffer[5];
|
// KMem.WY[1]=(RX_Buffer[6]<<8) + RX_Buffer[7];
|
}
|
else
|
{
|
Radio.Rx( RX_TIMEOUT_VALUE );
|
}
|
}
|
}
|
|
void OnTxTimeout( void )
|
{
|
KMem.WDT[43]++;
|
|
}
|
|
void OnRxTimeout( void )
|
{
|
Radio.Standby();
|
KMem.WDT[44]++;
|
KMem.ErrStat=200;
|
if(RadioEnableMaster)
|
{
|
SendPingMsg();
|
}
|
else
|
{
|
Radio.Rx( RX_TIMEOUT_VALUE );
|
}
|
}
|
|
void OnRxError( void )
|
{
|
|
Radio.Standby();
|
KMem.WDT[45]++;
|
if(RadioEnableMaster)
|
{
|
SendPingMsg();
|
}
|
else
|
{
|
Radio.Rx( RX_TIMEOUT_VALUE );
|
}
|
|
}
|