From a05285d3b4ea5aa0609a2a23aa867c17c0611a38 Mon Sep 17 00:00:00 2001
From: yincheng.zhong <634916154@qq.com>
Date: 星期日, 18 二月 2024 10:17:57 +0800
Subject: [PATCH] 配合网关调试标签上传回复部分,测距部分已经调通

---
 Src/radio/Lora.c |  260 ++++++++++++++++++---------------------------------
 1 files changed, 91 insertions(+), 169 deletions(-)

diff --git a/Src/radio/Lora.c b/Src/radio/Lora.c
index 9cbe85e..4c1101d 100644
--- a/Src/radio/Lora.c
+++ b/Src/radio/Lora.c
@@ -11,6 +11,8 @@
 #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
 //#define USE_MODEM_FSK
@@ -30,11 +32,11 @@
 #define LORA_TX_47X 2
 #define LORA_RX_47X 3
 uint8_t Lora_status=0;
-
+extern uint32_t uwbled,gpsled,loraled,powerled;
 #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;
@@ -63,8 +65,8 @@
                                                               //  2: 4/6,
                                                               //  3: 4/7,
                                                               //  4: 4/8]
-#define LORA_PREAMBLE_LENGTH_T                      10         // Same for Tx 
-#define LORA_PREAMBLE_LENGTH_R                      64        // Same for Rx
+#define LORA_PREAMBLE_LENGTH_T                      8         // Same for Tx 
+#define LORA_PREAMBLE_LENGTH_R                      8        // 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
@@ -96,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];
@@ -174,14 +175,9 @@
 //}
 uint32_t lora_zhuangtai;
 void OnTxDone( void )
-{
-    if(bat_percent>20)
-    {LED_TR_OFF;}
-    else
-    {LED_TB_OFF;}
-    
+{  
     Radio.Standby();
-     Radio.Rx( RX_TIMEOUT_VALUE ); 
+    Radio.Rx( RX_TIMEOUT_VALUE ); 
 }
 
 extern uint32_t lp_time;
@@ -201,26 +197,7 @@
 extern uint8_t stationary_flag;
 extern uint8_t anjian_flag;
 uint16_t Lora_wangguanid=0;
-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);
-					checksum = Checksum_u16(&usart_send[2],11+4*Lora_tx_ancnum);
-					memcpy(&usart_send[13+4*Lora_tx_ancnum],&checksum,2);
-}
+
 extern uint8_t lora_tx_flag;
 uint16_t current_count;
 u16 Lora_recevcount;
@@ -229,6 +206,11 @@
 uint8_t xie_flag;
 extern uint8_t no_yingdaflag;
 uint16_t Beepchixutime;
+uint8_t xiafayuyin_data[200];
+uint8_t yuyin_sendflag;
+uint8_t yuyinzidongguan_time;
+uint8_t  yuyin_no_sleep_flag;
+extern uint8_t usart5_state;
 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
 {   
         BufferSize = size;
@@ -236,64 +218,18 @@
         RssiValue = rssi;
         SnrValue = snr;
         Radio.Standby();
-printf("收到lora\r\n");
+//printf("收到lora\r\n");
 //    if(Lora_status==LORA_RX_433)
-    if(RX_Buffer[0]==0x50&&RX_Buffer[1]==0x49)
+    no_data_chongqi_num=0;
+    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);
-        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))
         {
             if(RX_Buffer[6]==0)//不需要操作,正常应答
             {
@@ -313,128 +249,114 @@
             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
                      {
+                        if(RX_Buffer[7]==0xDD)//下发语音
+                         {
+                             if(usart5_state==1)
+                             {
+                             Usart5Init();
+                             usart5_state=0;    
+                             }
+                        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_7, GPIO_PIN_SET);
+                        HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);    
+                        memcpy(&xiafayuyin_data,&RX_Buffer[9],RX_Buffer[8]);
+                        yuyin_sendflag=1;           
+                        yuyinzidongguan_time=0;             
+                        yuyin_no_sleep_flag=1;                             
+                         }
+                         else
+                         {
+                             uint16_t pinlv1;
+                             if(RX_Buffer[7]==0x06)
+                             {
+                             memcpy(&pinlv1, &RX_Buffer[9], RX_Buffer[8]);
+                             pinlv1=(1000/pinlv1)*1000;
+                            memcpy((uint8_t*)&g_com_map + RX_Buffer[7], &pinlv1, RX_Buffer[8]);
+                            save_com_map_to_flash();
+                            delay_ms(100);
+                            NVIC_SystemReset();
+                             }
+                         
                 memcpy((uint8_t*)&g_com_map + RX_Buffer[7], &RX_Buffer[9], RX_Buffer[8]);
                 save_com_map_to_flash();
                 delay_ms(100);
                 NVIC_SystemReset();
+                         }
                      }
                  }
             }
         }
-        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);
-//            HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, 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.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);
                                    
-        Radio.SetRxDutyCycle(RxDutyCycle_RX_time,RxDutyCycle_SLEEP_time);
-                                   
-}
-void Lora47X_change(void)
-{
-            Radio.SetChannel( Loratx_frequency );
-            SX126x.ModulationParams.Params.LoRa.SpreadingFactor=7;
-            SX126x.ModulationParams.Params.LoRa.Bandwidth=2;
-            SX126x.ModulationParams.Params.LoRa.CodingRate=1;
-            SX126xSetModulationParams( &SX126x.ModulationParams );
 }

--
Gitblit v1.9.3