1
WXK
2023-11-29 d3d530578cd646c83b6683439d08a80141205e46
Src/radio/Lora.c
@@ -10,10 +10,13 @@
#include "stdio.h"
#include "delay.h"
#include "main.h"
#include <stdlib.h>
static RadioEvents_t RadioEvents;
#define USE_MODEM_LORA
//#define USE_MODEM_FSK
#define REGION_CN779 
void Lora47xrx_Init(void);
void Lora433rx_Init(void);
uint32_t RxDutyCycle_RX_time   =128;
uint32_t RxDutyCycle_SLEEP_time=256;
#define LORA_FREQUENCY0 470200000
@@ -21,6 +24,11 @@
#define LORA_FREQUENCY2 472200000
#define LORA_FREQUENCY3 473200000
#define LORA_FREQUENCY4 474200000
uint16_t Lora_rece_error;
#define LORA_RX_433 1
#define LORA_TX_47X 2
#define LORA_RX_47X 3
uint8_t Lora_status=0;
#if defined( REGION_AS923 )
#define RF_FREQUENCY                                923000000 // Hz
@@ -123,7 +131,7 @@
}States_t;
//#define RX_TIMEOUT_VALUE                            1000
#define RX_TIMEOUT_VALUE                            64000
#define RX_TIMEOUT_VALUE                            6400
#define BUFFER_SIZE                                 64 // Define the payload size here
const uint8_t PingMsg[] = "PING";
@@ -153,7 +161,7 @@
}
void Lora_tx_mode( void )
{
Lora_status=LORA_TX_47X;
            Radio.SetChannel( Loratx_frequency );
            
            Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, 2,
@@ -163,49 +171,52 @@
}
void Lora_rx_open( void )
{
    if(Lora_rx_open_flag==1)
    {
    Lora_Init();
    Lora_rx_open_flag=0;
    }
    if(lora_yingda_flag&&lora_chongfuyingda_flag==0)
    {
        printf("Lora:%d  %d\r\n",lora_yingda_flag,lora_chongfuyingda_flag);
    lora_chongfuyingda_flag=1;
    RadioEvents.TxDone = OnTxDone;
    RadioEvents.RxDone = OnRxDone;
    RadioEvents.TxTimeout = OnTxTimeout;
    RadioEvents.RxTimeout = OnRxTimeout;
    RadioEvents.RxError = OnRxError;
//    if(Lora_rx_open_flag==1)
//    {
//    Lora_Init();
//    Lora_rx_open_flag=0;
//    }
//    if(lora_yingda_flag&&lora_chongfuyingda_flag==0)
//    {
//        printf("Lora:%d  %d\r\n",lora_yingda_flag,lora_chongfuyingda_flag);
//    lora_chongfuyingda_flag=1;
//    RadioEvents.TxDone = OnTxDone;
//    RadioEvents.RxDone = OnRxDone;
//    RadioEvents.TxTimeout = OnTxTimeout;
//    RadioEvents.RxTimeout = OnRxTimeout;
//    RadioEvents.RxError = OnRxError;
    Radio.Init( &RadioEvents );
    Radio.SetChannel( 500000000 );
    Radio.SetRxConfig( MODEM_LORA, 0, 7,
    LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH_R,
    LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
    0, false, 0, 0, true, 1 );
//    Radio.Init( &RadioEvents );
//    Radio.SetChannel( 500000000 );
//    Radio.SetRxConfig( MODEM_LORA, 0, 7,
//    LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH_R,
//    LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
//    0, false, 0, 0, true, 1 );
    SX126xClearIrqStatus( IRQ_RADIO_ALL );
//    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
        Radio.Rx( RX_TIMEOUT_VALUE );
//    SX126xClearIrqStatus( IRQ_RADIO_ALL );
////    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
//        Radio.Rx( RX_TIMEOUT_VALUE );
    }
//
//    }
}
void OnTxDone( void )
{
    LED_TB_OFF;
    if(lora_yingda_num>1000)
    {
    lora_yingda_num=0;
    lora_yingda_flag=1;
    }
//    if(lora_yingda_num>1000)
//    {
//    lora_yingda_num=0;
//    lora_yingda_flag=1;
//    }
    
    if(lora_yingda_flag==0)
    Lora_rx_open_flag=1;
    else
    {}
//    if(lora_yingda_flag==0)
//    Lora_rx_open_flag=1;
//    else
//    {}
    Radio.Standby();
// Lora433rx_Init();
   Lora47xrx_Init();
}
extern uint32_t lp_time;
@@ -245,9 +256,11 @@
}
uint16_t current_count;
u16 Lora_recevcount;
extern uint8_t no_data_chongqi_num;
extern uint16_t no_data_chongqi_num;
void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
{   
    if(Lora_status==LORA_RX_433)
    {
    Lora_recevcount=HAL_LPTIM_ReadCounter(&hlptim1);
    Lora_recevcount=Lora_recevcount+197;//197代表6ms
    if(Lora_recevcount>=32768)
@@ -262,25 +275,25 @@
    no_data_chongqi_num=0;
    Radio.Standby();
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
    if(lora_yingda_flag)
//    if(lora_yingda_flag)
//    {
//    memcpy(&lora_recv_devid,&RX_Buffer[4],2);
//        if(lora_recv_devid==dev_id)
//        {
//
//
//        }
//    }
//    if(lora_yingda_flag==0)
    {
    memcpy(&lora_recv_devid,&RX_Buffer[4],2);
        if(lora_recv_devid==dev_id)
        {
        }
    }
    if(lora_yingda_flag==0)
    {
        lora_chongfuyingda_flag=0;
//        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
//        loraa_flag=0;
//        HAL_GPIO_chongfuyingd_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
//       Delay_Ms(10);
//        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET);
        uint16_t i=0;
//        uint16_t i=0;
//        for(i=0;i<BufferSize;i++)
        {printf("R %d:\r\n",RX_Buffer[3]);}
        RX_TIMEOUTs++;
//        {printf("R %d:\r\n",RX_Buffer[3]);}
//        RX_TIMEOUTs++;
//        printf("RX_num :%d\r\n",RX_TIMEOUTs);
//        start_time=HAL_LPTIM_ReadCounter(&hlptim1);
//        end_time=start_time+((32768/1000)*20);
@@ -333,21 +346,51 @@
    }
    }
    if(Lora_status==LORA_RX_47X)//。
    {
        BufferSize = size;
        memcpy( RX_Buffer, payload, BufferSize );
        RssiValue = rssi;
        SnrValue = snr;
        Radio.Standby();
        if(RX_Buffer[0]==0x55&&RX_Buffer[1]==0xAA)
        {
            Lora_rece_error=0;
            Lora433rx_Init();
            Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
        }
    }
}
void OnTxTimeout( void )
{
       Radio.Standby();
        Lora_Init();
        Lora433rx_Init();
    Lora_rx_open_flag=0;
    
    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
}
uint16_t Lora_rece_error;
void OnRxTimeout( void )
{
    uint8_t i;
    Radio.Standby();
        Lora_Init();
    if(Lora_status==LORA_RX_47X) //470rx超时,先调时间发送,5次后还不行,换频段
    {
        Lora_rece_error++;
        if(Lora_rece_error>5)
        {
            Loratx_frequency=Loratx_frequency+1000000;
            Lora_rece_error=0;
        }
        else
        {
           i= (rand()%100);
        SetNextPollTime(i);
        }
    }
    Lora433rx_Init();
    Lora_rx_open_flag=0;
    
    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
@@ -359,20 +402,21 @@
{
    Radio.Standby();
        Lora_Init();
    Lora433rx_Init();
    Lora_rx_open_flag=0;
    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
}
void Lora_Init(void)
void Lora433rx_Init(void)
{
    RadioEvents.TxDone = OnTxDone;
    RadioEvents.RxDone = OnRxDone;
    RadioEvents.TxTimeout = OnTxTimeout;
    RadioEvents.RxTimeout = OnRxTimeout;
    RadioEvents.RxError = OnRxError;
//    printf("Lora_Init3\r\n");
        Radio.Init( &RadioEvents );
    Lora_status=LORA_RX_433;
//    RadioEvents.TxDone = OnTxDone;
//    RadioEvents.RxDone = OnRxDone;
//    RadioEvents.TxTimeout = OnTxTimeout;
//    RadioEvents.RxTimeout = OnRxTimeout;
//    RadioEvents.RxError = OnRxError;
////    printf("Lora_Init3\r\n");
//        Radio.Init( &RadioEvents );
        Radio.SetChannel( RF_FREQUENCY );      
//        Radio.SetStandby( STDBY_RC );
@@ -420,3 +464,40 @@
    }
       
}
void Lora47xrx_Init(void)
{   Lora_status=LORA_RX_47X;
//    RadioEvents.TxDone = OnTxDone;
//    RadioEvents.RxDone = OnRxDone;
//    RadioEvents.TxTimeout = OnTxTimeout;
//    RadioEvents.RxTimeout = OnRxTimeout;
//    RadioEvents.RxError = OnRxError;
//        Radio.Init( &RadioEvents );
        Radio.SetChannel( Loratx_frequency );
      Radio.SetRxConfig( MODEM_LORA, LORA_BANDWIDTH, 7,
                                   LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH_R,
                                   LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
                                   0, false, 0, 0, LORA_IQ_INVERSION_ON_R, 0 );
    {
       SX126xClearIrqStatus( IRQ_RADIO_ALL );
//       Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
        Radio.Rx( RX_TIMEOUT_VALUE );
//       printf("I am Slave!\r\n");
    }
}
void Lora_Init(void)
{
    RadioEvents.TxDone = OnTxDone;
    RadioEvents.RxDone = OnRxDone;
    RadioEvents.TxTimeout = OnTxTimeout;
    RadioEvents.RxTimeout = OnRxTimeout;
    RadioEvents.RxError = OnRxError;
        Radio.Init( &RadioEvents );
}