yincheng.zhong
2024-02-18 a05285d3b4ea5aa0609a2a23aa867c17c0611a38
Src/radio/Lora.c
@@ -11,6 +11,7 @@
#include "delay.h"
#include "main.h"
#include <stdlib.h>
#include "dw_mbx_tag.h"
#include "WS2812.h"
static RadioEvents_t RadioEvents;
#define USE_MODEM_LORA
@@ -35,7 +36,7 @@
#define RF_FREQUENCY                              433000000 // Hz
#define RF_FREQUENCY_R                              500000000 // Hz
#define RF_FREQUENCY_T                              470200000 // Hz
void LoraSendComMap(uint8_t data_length, uint8_t index);
#define TX_OUTPUT_POWER                             22        // 22 dBm
extern bool IrqFired;
@@ -97,11 +98,10 @@
}States_t;
//#define RX_TIMEOUT_VALUE                            1000
#define RX_TIMEOUT_VALUE                            100
#define BUFFER_SIZE                                 64 // Define the payload size here
#define RX_TIMEOUT_VALUE                            10
#define BUFFER_SIZE                                 255 // 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];
@@ -175,18 +175,7 @@
//}
uint32_t lora_zhuangtai;
void OnTxDone( void )
{
//    if(bat_percent>20)
//    {
//    loraled=LEDOFF;
//    Set4LEDColor(powerled,loraled,gpsled,uwbled);
//    }
//    else
//    {
//    loraled=LEDOFF;
//    Set4LEDColor(powerled,loraled,gpsled,uwbled);
//    }
{
    Radio.Standby();
    Radio.Rx( RX_TIMEOUT_VALUE ); 
}
@@ -208,29 +197,7 @@
extern uint8_t stationary_flag;
extern uint8_t anjian_flag;
uint16_t Lora_wangguanid=0;
extern uint8_t GPSchangdu;
extern  uint8_t GPS_GGAdate[200];
void Lora_tx_zubao( void )
{
    uint16_t checksum;
                    usart_send[2] = 0x22;//正常模式
               usart_send[3] = 11+Lora_tx_ancnum*4;//数据段长度
                    memcpy(&usart_send[4],&dev_id,2);
               usart_send[6] = lora_seq_nb2;//包序
                    power_and_key=stationary_flag<<1;//把按键跟静止2合1,发出去
                    power_and_key=power_and_key+anjian_flag;;
                    memcpy(usart_send+7,&bat_percent,1);
                    memcpy(usart_send+8,&power_and_key,1);
                    usart_send[9] = lora_yingda_flag;//lora等待应答位
                    usart_send[10] = Lora_tx_ancnum;
//                    usart_send[9] = lora_yingda_flag;
               memcpy(&usart_send[11],&Lora_tx_ancid,2*Lora_tx_ancnum);
               memcpy(&usart_send[11+Lora_tx_ancnum*2],&Lora_tx_ancdist,2*Lora_tx_ancnum);
                    memcpy(&usart_send[11+Lora_tx_ancnum*4],&Lora_wangguanid,2);
                    memcpy(&usart_send[13+Lora_tx_ancnum*4],GPS_GGAdate,GPSchangdu);
               checksum = Checksum_u16(&usart_send[2],11+4*Lora_tx_ancnum+GPSchangdu);
               memcpy(&usart_send[13+4*Lora_tx_ancnum+GPSchangdu],&checksum,2);
}
extern uint8_t lora_tx_flag;
uint16_t current_count;
u16 Lora_recevcount;
@@ -251,72 +218,19 @@
        RssiValue = rssi;
        SnrValue = snr;
        Radio.Standby();
printf("收到lora\r\n");
//printf("收到lora\r\n");
//    if(Lora_status==LORA_RX_433)
    no_data_chongqi_num=0;
    if(RX_Buffer[0]==0x50&&RX_Buffer[1]==0x49)
    if(RX_Buffer[MSG_TYPE_IDX]==LORA_MSGTYPE_RANGEPOLL)
    {
    Lora_recevcount=HAL_LPTIM_ReadCounter(&hlptim1);
    Lora_recevcount=Lora_recevcount+197;//197代表6ms
    if(Lora_recevcount>=32768)
    {
    Lora_recevcount -=32768;
    }
//    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
//    BufferSize = size;
//    memcpy( RX_Buffer, payload, BufferSize );
//    RssiValue = rssi;
//    SnrValue = snr;
//    no_data_chongqi_num=0;
//    Radio.Standby();
//    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
        HAL_GPIO_WritePin(RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, GPIO_PIN_SET); //片选lora无效
           id =  dwt_readdevid() ;
   while (0xDECA0130!=id)
    {
        u8 iderror_count = 0;
        id =  dwt_readdevid() ;
        if(iderror_count++>100)
        {
////////            printf("UWB芯片ID错误");
            break;
        }
    }
////////            printf("Anchor_App\r\n");
        current_count=HAL_LPTIM_ReadCounter(&hlptim1);
      while(current_count<Lora_recevcount||current_count>Lora_recevcount+16384)
      {
         current_count=HAL_LPTIM_ReadCounter(&hlptim1);
         if(current_count<Lora_recevcount-300)
         {
            break;
         }
      }
//    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
            Anchor_App();
//    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_RESET);
            UWB_Wkup();
            MbxTagUwbRec();
            HAL_GPIO_WritePin(RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, GPIO_PIN_RESET); //片选lora有效
            delay_ms(1);
//            Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
        Radio.Rx( 9999 );
        printf("接收到uwb测距:%d",Lora_tx_ancdist[0]);
    }
    if(RX_Buffer[0]==0x55&&RX_Buffer[1]==0xEE)
    if(RX_Buffer[MSG_TYPE_IDX]==LORA_MSGTYPE_WGRESPTAG)
    {
//        BufferSize = size;
//        memcpy( RX_Buffer, payload, BufferSize );
//        RssiValue = rssi;
//        SnrValue = snr;
//        Radio.Standby();
        if(RX_Buffer[0]==0x55&&RX_Buffer[1]==0xEE&&!memcmp(&dev_id,&RX_Buffer[4],2))
        if(!memcmp(&dev_id,&RX_Buffer[DEST_ID_IDX],2))
        {
        /*RX_Buffer[6]:都还是写*/
        /*RX_Buffer[4,5]:发过来的ID*/
        /*RX_Buffer[7]:地址*/
        /*RX_Buffer[8]:长度*/
        /*RX_Buffer[9-后边]:具体数值*/
            if(RX_Buffer[6]==0)//不需要操作,正常应答
            {
                Lora_rece_error=0;
@@ -335,17 +249,12 @@
            if(RX_Buffer[6]==1)//写操作
            {
                 Lora_rece_error=0;
//                uint16_t rxbuff1;
//                xie_flag=1;
//                memcpy(&rxbuff1, &RX_Buffer[9], 2);
//                memcpy((uint8_t*)&g_com_map + RX_Buffer[7], &rxbuff1, RX_Buffer[8]);
//                no_yingda_time=5;
                 if(no_yingdaflag==0)
                 {
                     if(RX_Buffer[7]==0x10)
                     {
//                     Beep_On();
//                     memcpy(&Beepchixutime, &RX_Buffer[9], 2);
                     }
                     else
                     {
@@ -385,97 +294,69 @@
                 }
            }
        }
        Lora433_change();
        SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],UWB_CHANNEL_SF,0);
    }
    Radio.Rx(0);
}
void OnTxTimeout( void )
{
       Radio.Standby();
    Lora433_change();
    SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],UWB_CHANNEL_SF,0);
}
    uint8_t tm;
uint16_t Lora_rece_error;
void OnRxTimeout( void )
{
    Radio.Standby();
    if(Lora_status==LORA_RX_47X) //470rx超时,先调时间发送,5次后还不行,换频段
    {
        Lora_rece_error++;
        if(Lora_rece_error>5)
        {
            Loratx_frequency=Loratx_frequency+1000000;
            if(Loratx_frequency>474000000)
            {Loratx_frequency=470000000;}
            Lora_rece_error=0;
            Lora_wangguanid=0;
        }
        else
        {
           tm= (rand()%100);
//        SetNextPollTime(tm);
        }
    }
    Lora433_change();
    SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],UWB_CHANNEL_SF,0);
}
void OnRxError( void )
{
    Radio.Standby();
    Lora433_change();
    SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],UWB_CHANNEL_SF,0);
}
void Lora47X_Init(void)
void SwitchLoraSettings(uint32_t freq,uint8_t sf,uint8_t power)
{
    Lora_status=LORA_RX_47X;
//    RadioEvents.TxDone = OnTxDone;
//    RadioEvents.RxDone = OnRxDone;
//    RadioEvents.TxTimeout = OnTxTimeout;
//    RadioEvents.RxTimeout = OnRxTimeout;
//    RadioEvents.RxError = OnRxError;
    //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_SET);
//    Radio.Init( &RadioEvents );
    Radio.Standby();
  //  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);
    Radio.SetChannel( freq*1000000 );
    Radio.SetTxConfig( MODEM_LORA, 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, 0 );
}
void LoraInit(void)
{
    RadioEvents.TxDone = OnTxDone;
    RadioEvents.RxDone = OnRxDone;
    RadioEvents.TxTimeout = OnTxTimeout;
    RadioEvents.RxTimeout = OnRxTimeout;
    RadioEvents.RxError = OnRxError;
        Radio.Init( &RadioEvents );
        Radio.SetChannel( Loratx_frequency );
        Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, 2,
        7, 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, 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 );
}
void Lora433_change(void)
{
    Lora_status=LORA_RX_433;
//            Radio.SetChannel( RF_FREQUENCY );
//            SX126x.ModulationParams.Params.LoRa.SpreadingFactor=5;
//            SX126x.ModulationParams.Params.LoRa.Bandwidth=2;
//            SX126x.ModulationParams.Params.LoRa.CodingRate=1;
//            SX126xSetModulationParams( &SX126x.ModulationParams );
//    Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
//
//        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( RF_FREQUENCY );
        Radio.SetTxConfig( MODEM_LORA, TX_OUTPUT_POWER, 0, 2,
        5, 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, LORA_SPREADING_FACTOR,
                                   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 );
          Radio.Rx( 9999 );
    Radio.Init( &RadioEvents );
    SwitchLoraSettings(UWB_CHANNEL_FRQ+g_com_map[GROUP_ID],5,0); //切换lora接收频点
    Radio.Rx( 9999 );
//        Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
                                   
}