#include <math.h>
|
#include <string.h>
|
#include "sx126x.h"
|
#include "sx126x-board.h"
|
#include "delay.h"
|
#include "Lora.h"
|
#include "user.h"
|
#include "sx126x-board.h"
|
#include "string.h"
|
#include "stdio.h"
|
#include "delay.h"
|
#include "dw_mbx_anc.h"
|
#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
|
#define RF_FREQUENCY_R 500000000 // Hz
|
#define RF_FREQUENCY_T 470200000 // 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 TX_OUTPUT_POWER 22 // 22 dBm
|
|
extern bool IrqFired;
|
|
|
|
|
bool EnableMaster=true;//Ö÷Ñ¡Ôñ
|
//bool EnableMaster=false;//´ÓÑ¡Ôñ
|
|
uint16_t crc_value;
|
/*!
|
* Radio events function pointer
|
*/
|
|
static RadioEvents_t RadioEvents;
|
/**
|
* @brief Update CRC16 for input byte
|
* @param CRC input value
|
* @param input byte
|
* @retval Updated CRC value
|
*/
|
uint16_t UpdateCRC16(uint16_t crcIn, uint8_t byte)
|
{
|
uint32_t crc = crcIn;
|
uint32_t in = byte|0x100;
|
|
do
|
{
|
crc <<= 1;
|
in <<= 1;
|
|
if(in&0x100)
|
{
|
++crc;
|
}
|
|
if(crc&0x10000)
|
{
|
crc ^= 0x1021;
|
}
|
} while(!(in&0x10000));
|
|
return (crc&0xffffu);
|
}
|
|
/**
|
* @brief Cal CRC16 for YModem Packet
|
* @param data
|
* @param length
|
* @retval CRC value
|
*/
|
uint16_t Cal_CRC16(const uint8_t* data, uint32_t size)
|
{
|
uint32_t crc = 0;
|
const uint8_t* dataEnd = data+size;
|
|
while(data<dataEnd)
|
{
|
crc = UpdateCRC16(crc,*data++);
|
}
|
crc = UpdateCRC16(crc,0);
|
crc = UpdateCRC16(crc,0);
|
|
return (crc&0xffffu);
|
}
|
|
#if defined( USE_MODEM_LORA )
|
|
#define LORA_BANDWIDTH 2 // [0: 125 kHz,
|
// 1: 250 kHz,
|
// 2: 500 kHz,
|
// 3: Reserved]
|
#define LORA_SPREADING_FACTOR 5 // [SF7..SF12]
|
#define LORA_CODINGRATE 1 // [1: 4/5,
|
// 2: 4/6,
|
// 3: 4/7,
|
// 4: 4/8]
|
#define LORA_PREAMBLE_LENGTH_T 8 // 96 // Same for Tx
|
#define LORA_PREAMBLE_LENGTH_R 64 // Same for Rx
|
//#define LORA_PREAMBLE_LENGTH 990 // Same for Tx and Rx
|
#define LORA_SYMBOL_TIMEOUT 0 // Symbols
|
#define LORA_FIX_LENGTH_PAYLOAD_ON false
|
#define LORA_IQ_INVERSION_ON_T false
|
#define LORA_IQ_INVERSION_ON_R false
|
|
|
#elif defined( USE_MODEM_FSK )
|
|
#define FSK_FDEV 5e3 // Hz
|
#define FSK_DATARATE 2.4e3 // bps
|
#define FSK_BANDWIDTH 20e3 // Hz >> DSB in sx126x
|
#define FSK_AFC_BANDWIDTH 100e3 // 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;
|
|
static uint8_t flag_lora_wait_sync = 0;
|
|
#define RX_TIMEOUT_VALUE 9999
|
#define BUFFER_SIZE 64 // 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;
|
void OnTxDone( void );
|
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr );
|
void OnTxTimeout( void );
|
void OnRxTimeout( void );
|
void OnRxError( void );
|
|
|
void LedToggle(void)
|
|
{
|
// HAL_GPIO_TogglePin(GPIOB, LED1_Pin);
|
}
|
|
|
|
uint8_t LoraUp_flag;
|
void OnTxDone( void )
|
{
|
// T_R_Init(0);
|
// Radio.Standby();
|
// Radio.Rx( RX_TIMEOUT_VALUE ); //½øÈë½ÓÊÕ
|
Radio.Sleep( );
|
if(LoraUp_flag)
|
{
|
Radio.Rx( RX_TIMEOUT_VALUE );
|
}
|
//printf("TX SUCCESS2\r\n");
|
}
|
static uint8_t rxbuffer[255],rxbuff_len;
|
static uint16_t rec_lp_count;
|
uint8_t GetLoraBufferAndLen(uint8_t* buffer,uint16_t* lp_count)
|
{
|
memcpy(buffer,rxbuffer,rxbuff_len);
|
memcpy(lp_count,&rec_lp_count,2);
|
return rxbuff_len;
|
}
|
extern uint16_t tx_num;
|
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
|
{
|
rec_lp_count = HAL_LPTIM_ReadCounter(&hlptim1);
|
rxbuff_len = size;
|
memcpy( rxbuffer, payload, BufferSize );
|
RssiValue = rssi;
|
SnrValue = snr;
|
|
Radio.Standby();
|
|
flag_lora_wait_sync = 0;
|
SetFlagSyncSuccess(1);
|
}
|
|
void OnTxTimeout( void )
|
{
|
|
}
|
|
void OnRxTimeout( void )
|
{
|
Radio.Standby();
|
flag_lora_wait_sync = 0;
|
}
|
|
void OnRxError( void )
|
{
|
flag_lora_wait_sync = 0;
|
Radio.Standby();
|
}
|
|
void Lora_Init(void)
|
{
|
RadioEvents.TxDone = OnTxDone;
|
RadioEvents.RxDone = OnRxDone;
|
RadioEvents.TxTimeout = OnTxTimeout;
|
RadioEvents.RxTimeout = OnRxTimeout;
|
RadioEvents.RxError = OnRxError;
|
|
Radio.Init( &RadioEvents );
|
Radio.SetChannel( RF_FREQUENCY );
|
Radio.SetTxConfig( MODEM_LORA, 22, 0, LORA_BANDWIDTH,
|
LORA_SPREADING_FACTOR, LORA_CODINGRATE,
|
LORA_PREAMBLE_LENGTH_T, LORA_FIX_LENGTH_PAYLOAD_ON,
|
false, 0, 0, LORA_IQ_INVERSION_ON_T, 3000 );
|
|
TX_Buffer[0] = 'P';
|
TX_Buffer[1] = 'I';
|
TX_Buffer[2] = 'N';
|
TX_Buffer[3] = tx_num;
|
|
crc_value=RadioComputeCRC(TX_Buffer,4,CRC_TYPE_IBM);//¼ÆËãµÃ³öÒª·¢ËÍÊý¾Ý°üCRCÖµ
|
TX_Buffer[4]=crc_value>>8;
|
TX_Buffer[5]=crc_value;
|
|
}
|
|
|
void Lora470_Init(void)
|
{
|
RadioEvents.TxDone = OnTxDone;
|
RadioEvents.RxDone = OnRxDone;
|
RadioEvents.TxTimeout = OnTxTimeout;
|
RadioEvents.RxTimeout = OnRxTimeout;
|
RadioEvents.RxError = OnRxError;
|
|
Radio.Init( &RadioEvents );
|
Radio.SetChannel( 470000000 );
|
Radio.SetTxConfig( MODEM_LORA, 22, 0, LORA_BANDWIDTH,
|
7, LORA_CODINGRATE,
|
10, LORA_FIX_LENGTH_PAYLOAD_ON,
|
false, 0, 0, LORA_IQ_INVERSION_ON_T, 3000 );
|
|
// TX_Buffer[0] = 'P';
|
// TX_Buffer[1] = 'I';
|
// TX_Buffer[2] = 'N';
|
// TX_Buffer[3] = tx_num;
|
//
|
// crc_value=RadioComputeCRC(TX_Buffer,4,CRC_TYPE_IBM);//¼ÆËãµÃ³öÒª·¢ËÍÊý¾Ý°üCRCÖµ
|
// TX_Buffer[4]=crc_value>>8;
|
// TX_Buffer[5]=crc_value;
|
|
}
|
void SetFlagLoraWaitSync(uint8_t value)
|
{
|
flag_lora_wait_sync = value;
|
}
|
uint8_t GetFlagLoraWaitSync(void)
|
{
|
return flag_lora_wait_sync;
|
}
|
void SwitchLoraSettings(uint32_t freq,uint8_t sf)
|
{
|
RadioEvents.RxDone = OnRxDone;
|
RadioEvents.RxTimeout = OnRxTimeout;
|
RadioEvents.RxError = OnRxError;
|
Radio.Init( &RadioEvents );
|
Radio.SetChannel( freq );
|
|
Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, 2,
|
sf, LORA_CODINGRATE,LORA_PREAMBLE_LENGTH_T, LORA_FIX_LENGTH_PAYLOAD_ON,
|
false, 0, 0, LORA_IQ_INVERSION_ON_T, 3000 );
|
|
Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH,
|
sf,LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH_R,
|
LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
|
0, false, 0, 0, LORA_IQ_INVERSION_ON_R, 1 );
|
|
}
|
|
uint16_t rec_wenjian_daxiao;
|
uint16_t wangguan_up_id;
|
uint8_t send_lora_data[250];
|
extern uint8_t lora_recbuffer[255];
|
uint8_t mudeshengjibao;
|
void LoraUp_Poll()
|
{
|
uint16_t crc16;
|
rec_wenjian_daxiao=lora_recbuffer[WRITEPATE_VALUE_IDX];
|
wangguan_up_id=lora_recbuffer[WG_ID_IDX];
|
send_lora_data[MSG_TYPE_IDX]=LORA_MSGTYPE_UPDATE_CONFIRM;
|
memcpy(&send_lora_data[WANGGUAN_ID],&wangguan_up_id,2);//Íø¹ØID Õ¼ÓÃ2¸ö×Ö½Ú
|
memcpy(&send_lora_data[JIZHAN_ID],&dev_id,2);//±êÇ©»òÕß»ùÕ¾µÄÉ豸ID 2¸ö×Ö½Ú
|
crc16=Cal_CRC16(send_lora_data,5);
|
memcpy(&send_lora_data[5],&crc16,2);
|
LoraUp_flag=1;
|
Radio.Send(send_lora_data,7);//»ùÕ¾·¢ËÍÉý¼¶È·Èϻظ´
|
while(1)
|
{
|
|
send_lora_data[MSG_TYPE_IDX]=LORA_MSGTYPE_UPDATEFILE_REQUEST;
|
send_lora_data[MUQIAN_BAG]=mudeshengjibao;
|
}
|
}
|