From b13574876322e59f75f26ac38f72c6b19c9d4aba Mon Sep 17 00:00:00 2001
From: chen <15335560115@163.com>
Date: 星期五, 27 六月 2025 14:02:08 +0800
Subject: [PATCH] 刚解决完网关下发会导致异常问题,但蓝牙那边mk给蓝牙发未配置成功未解决,开始调lora功耗

---
 keil/include/src/Radio/lora_1268.c |  225 ++++++++++++++++++--------------------------------------
 1 files changed, 72 insertions(+), 153 deletions(-)

diff --git a/keil/include/src/Radio/lora_1268.c b/keil/include/src/Radio/lora_1268.c
index cf1a5e8..dccdedf 100644
--- a/keil/include/src/Radio/lora_1268.c
+++ b/keil/include/src/Radio/lora_1268.c
@@ -158,12 +158,12 @@
     lora_sendbuffer[HB_LORAPOWER_IDX] = g_com_map[LORA_POWER];//将距离校准改为了TX发射功率设置
     checksum = Checksum_u16(lora_sendbuffer,14);
     memcpy(&lora_sendbuffer[14],&checksum,2);	
-//    Radio.Send(lora_sendbuffer,16);
+    Radio.Send(lora_sendbuffer,16);
 		
 /*****************************心跳包上传内容*************************************/	
 		
 }
-#define HEATBEAT_UPDATE_TIME   100
+#define HEATBEAT_UPDATE_TIME   20
 uint16_t heatbeat_count = HEATBEAT_UPDATE_TIME-1 ;
 uint16_t flag_getwgresp = 0;
 int16_t intheight;			//气压
@@ -187,117 +187,6 @@
 #define GET_USERKEY gpio_pin_get_val(SOS_PIN)
 void LoraReportPoll(void)
 {
-////    // delay_ms(100);
-
-////#ifdef _USE_BAR
-////    GetPressAndHeight();
-////    intheight = Height*100;
-////#endif
-////#ifdef _SMT_TEST
-////    printf("气压值:%d",intheight);
-////#endif
-
-//////    TagListUpdate();
-
-////    LoraReportFreqPoll();
-////    flag_getwgresp = 0;
-
-//    SwitchLoraSettings(478,REPORT_CHANNEL_SF,22);
-
-////    if(heatbeat_count++>HEATBEAT_UPDATE_TIME && WG_Connected == wg_state) //如果心跳包到达上传时间,并且网关处于链接状态,就上传心跳包
-////    {
-////        heatbeat_count = 0;
-////        LoraHeartBeartPoll();
-////        return;
-////    }
-////    for(uint16_t i=0; i<report_ancnum-1; i++)
-////    {
-////        for(uint16_t j=0; j<report_ancnum-1-i; j++)
-////        {
-////            if(report_ancdist[j]>report_ancdist[j+1])
-////            {
-////                uint16_t id,dist;
-////                uint8_t bat;
-////                id = report_ancid[j];
-////                dist = report_ancdist[j];
-////                report_ancid[j] = report_ancid[j+1];
-////                report_ancdist[j] = report_ancdist[j+1];
-////                report_ancid[j+1] = id;
-////                report_ancdist[j+1] = dist;
-////            }
-////        }
-////    }
-////    
-////    if(report_ancnum>LORA_REPORT_MAXANC_NUM)	                              //考虑lora传输时间,最多发送10个基站数据。
-////		{
-////      report_ancnum = LORA_REPORT_MAXANC_NUM;
-////		} 		  
-////				
-////#ifdef USE_GPS
-////    lora_sendbuffer[MSG_TYPE_IDX] = LORA_MSGTYPE_TAGMSGTOWG_GPS;
-////    lora_sendbuffer[MSG_LENGTH] = 4*report_ancnum+30;
-////#else
-////		BT_NUM=DMA_RXBuf_BT[0];
-////		if(SOS_KEY_STATE)
-////		{
-////			BT_NUM=0;
-//////		}
-////    lora_sendbuffer[MSG_TYPE_IDX] = LORA_MSGTYPE_TAGMSGTOWG;
-//		lora_sendbuffer[MSG_TYPE_IDX] = LORA_MSGTYPE_TAGMSGTOWG_BT;
-//    //lora_sendbuffer[MSG_LENGTH] = 4*report_ancnum+4*BT_NUM+ANCID_IDX+3;
-////#endif
-//    memcpy(&lora_sendbuffer[SOURCE_ID_IDX],&g_com_map[DEV_ID],2);
-//    memcpy(&lora_sendbuffer[DEST_ID_IDX],&wg_report_id,2);
-//    lora_sendbuffer[SEQNUM_IDX] = seq_num++;
-//    lora_sendbuffer[BAT_IDX] = bat_percent;
-////		lora_sendbuffer[STATE_IDX] = !GET_USERKEY|stationary_flag<<1;
-////		//GET_USERKEY这个是那个lora——busy那个引脚
-//////		lora_sendbuffer[STATE_IDX]=0x01;
-//////		lora_sendbuffer[GATEWAY_CHANL]=0x02;
-//////		lora_sendbuffer[CHANL_NUM]=0x03;
-////    
-////#ifdef USE_GPS
-////    memcpy(&lora_sendbuffer[GPS_JINGDU_IDX],&gps_jingdu,8);
-////    memcpy(&lora_sendbuffer[GPS_WEIDU_IDX],&gps_jingdu,8);
-////    memcpy(&lora_sendbuffer[GPS_HEIGHT_IDX],&gps_height,8);
-////    lora_sendbuffer[GPS_STATE_IDX] = gps_state;
-////    lora_sendbuffer[GPS_SATEL_NUM_IDX] = gps_satel_num;
-////    lora_sendbuffer[GPS_SPOWER_IDX] = gps_signalpower;
-////    lora_sendbuffer[GPS_CHAFENLINGQI] = gps_chafenlingqi;
-////#endif
-////    memcpy(&lora_sendbuffer[BAR_HEIGHT_IDX],&intheight,2);
-////    lora_sendbuffer[BT_ANCID_IDX] = report_ancnum;
-////    memcpy(&lora_sendbuffer[BT_ANCID_IDX+1],report_ancid,report_ancnum*2);
-////    memcpy(&lora_sendbuffer[BT_ANCID_IDX+report_ancnum*2+1],report_ancdist,report_ancnum*2);
-////		uint8_t LORA_RETRANSNUSSION_BT=0;
-//////		
-//////		num[0]=REV_RX_NUM;
-//////		num[1]=REV_POLL_NUM;
-//////		num[2]=REPLY_POLL_NUM;
-////		LORA_RETRANSNUSSION_BT=BT_ANCID_IDX+report_ancnum*4+1;
-////		lora_sendbuffer[LORA_RETRANSNUSSION_BT] = 0x2D; 		//蓝牙上传标识符
-
-////    memcpy(&lora_sendbuffer[LORA_RETRANSNUSSION_BT+1],&DMA_RXBuf_BT[0],BT_NUM*2+1); 									//基站ID
-////    memcpy(&lora_sendbuffer[LORA_RETRANSNUSSION_BT+BT_NUM*2+2],&DMA_RXBuf_BT[1+BT_NUM*2],BT_NUM*2);//基站距离		
-////		lora_sendbuffer[MSG_LENGTH] = LORA_RETRANSNUSSION_BT+4*BT_NUM+2;
-//////		}
-//////		if(lora_sendbuffer[LORA_RETRANSNUSSION_BT]==0x2D)
-//////		{
-////		checksum = Checksum_u16(lora_sendbuffer,4*BT_NUM+4*report_ancnum+BT_ANCID_IDX+3);
-////		memcpy(&lora_sendbuffer[LORA_RETRANSNUSSION_BT+BT_NUM*4+2],&checksum,2);
-//		BT_SEND_flag=BT_ANCID_IDX+report_ancnum*4+BT_NUM*4+5;
-//		Radio.Send(lora_sendbuffer,BT_SEND_flag);	
-////		LORA_POLL_COUNT++;
-//////		LORA_3029_SINGLE_SEND(lora_sendbuffer,ANCID_IDX+report_ancnum*4+BT_NUM*4+4,0);
-//////		}
-//////		else
-//////		{
-//////		checksum = Checksum_u16(lora_sendbuffer,4*report_ancnum+ANCID_IDX);
-//////		memcpy(&lora_sendbuffer[ANCID_IDX+report_ancnum*4],&checksum,2);
-//////		Radio.Send(lora_sendbuffer,ANCID_IDX+report_ancnum*4+BT_NUM*4+2);
-//////		}
-
-
 #ifdef _USE_BAR
     GetPressAndHeight();
     intheight = Height*100;
@@ -405,7 +294,7 @@
         checksum = Checksum_u16(lora_sendbuffer,11+data_length);
         memcpy(&lora_sendbuffer[11+data_length],&checksum,2);
 //        LORA_3029_SINGLE_SEND(lora_sendbuffer,data_length+13,0);
-//				Radio.Send(lora_sendbuffer,data_length+13);
+				Radio.Send(lora_sendbuffer,data_length+13);
 	      //++;
   //  Delay_Ms(100);
 }
@@ -423,9 +312,9 @@
 	memcpy(&lora_sendbuffer[7], &g_com_map[index/2], data_length);
 	checksum = Checksum_u16(lora_sendbuffer,9+data_length);
 	memcpy(&lora_sendbuffer[9+data_length],&checksum,2);
-//	Radio.Send(lora_sendbuffer,data_length+11);
-
-//	 LORA_3029_SINGLE_SEND(lora_sendbuffer,data_length+11,0);
+	Radio.Send(lora_sendbuffer,data_length+11);
+LOG_INFO(TRACE_MODULE_APP, "进入回复\r\n");
+  //LORA_3029_SINGLE_SEND(lora_sendbuffer,data_length+11,0);
   //  Delay_Ms(100);
 }
 uint8_t t22[20],ti;
@@ -471,7 +360,20 @@
 
      
 }
-
+void lora_in_sleep(void)
+{
+				 //SPI0/MOSI/MISO/CLK/CS
+//	gpio_pin_set(LORA_CS);
+//	gpio_pin_clr(LORA_MOSI);
+//	gpio_pin_clr(LORA_MISO);
+//	gpio_pin_clr(LORA_CLK);
+//	gpio_pin_clr(LORA_IRQ);
+	//gpio_pin_clr(LORA_NRST);
+	//gpio_pin_clr(LORA_NRST);//lora休眠
+//	 Radio.Standby();
+	
+    Radio.Sleep();
+}
 ////States_t State = LOWPOWER;
 
 //////int8_t RssiValue = 0;
@@ -488,13 +390,16 @@
 //uint16_t current_count;
 //extern wg_state_enum wg_state;
 uint8_t txdone=0;
+uint8_t send_flag;
 void OnTxDone( void )
 { 
 	  txdone++;
+		
     if(flag_writepara_needreset) //配置写入完成设备需要重启
     {
-        printf("网关下发配置写入完成,重启");
-        SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
+        LOG_INFO(TRACE_MODULE_APP,"网关下发配置写入完成,重启\r\n");
+//        SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
+			sys_reset(0);
     }
     Radio.Standby();
     if(LoraUp_flag)
@@ -504,7 +409,9 @@
     }
     else
     {
+			
     Radio.Rx( 50 );
+			send_flag=1;
     //LORA_LED_OFF;
     }
 }
@@ -538,6 +445,9 @@
 uint8_t LoraUp_flag;
 uint16_t REV_WG_pack=0; 
 uint8_t rxdone=0;
+extern uint16_t set_mk_time;
+extern mk_send_message_to_BLE(void);
+uint8_t tt4=1;
 void OnRxDone( uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr )
 {  
 				uint16_t checksum1;
@@ -570,43 +480,43 @@
 										switch(RX_Buffer[PWTAG_RW_FLAG_IDX])
 										{
 												case WGRSP_RWTAG_NONE:
-												REV_WG_pack++;
-                        wg_report_freq = RX_Buffer[POLL_FREQ_IDX]+400;
-                        memcpy(&rec_delaytime,&RX_Buffer[NEXTPOLL_TIME_IDX],2);
+												REV_WG_pack++;//此处刚放开
+                        wg_report_freq = RX_Buffer[POLL_FREQ_IDX]+400;//此处刚放开未测试
+//                        memcpy(&rec_delaytime,&RX_Buffer[NEXTPOLL_TIME_IDX],2);
 
-                        if(report_ancnum<2)
-                        {
-                            datalen_offset = report_ancnum*85;
-                        }else{
-                            datalen_offset = (report_ancnum-1)*46+85;
-                        }
-                        rec_secdelay = RX_Buffer[PWTAG_SECDELAY_IDX];
-                        if(BufferSize!=13||rec_secdelay>20)
-                        {
-                         rec_secdelay = 0;
-                        }
-											if(rec_delaytime<500)
-												{
-												int t;
-													t++;
-												}
-//                        sleep_time_count =(__MS_TO_32K_CNT(rec_delaytime)/10)- delaytime-datalen_offset;
-                        sleep_time_count =(__MS_TO_32K_CNT(rec_delaytime)/10);
-                        while(sleep_time_count>=32768)
-												{
-                         sleep_time_count-=32768;											
-												}
-												while(sleep_time_count<2000)
-												{
-												 sleep_time_count+=10000;
-												}
+//                        if(report_ancnum<2)
+//                        {
+//                            datalen_offset = report_ancnum*85;
+//                        }else{
+//                            datalen_offset = (report_ancnum-1)*46+85;
+//                        }
+//                        rec_secdelay = RX_Buffer[PWTAG_SECDELAY_IDX];
+//                        if(BufferSize!=13||rec_secdelay>20)
+//                        {
+//                         rec_secdelay = 0;
+//                        }
+//											if(rec_delaytime<500)
+//												{
+//												int t;
+//													t++;
+//												}
+////                        sleep_time_count =(__MS_TO_32K_CNT(rec_delaytime)/10)- delaytime-datalen_offset;
+//                        sleep_time_count =(__MS_TO_32K_CNT(rec_delaytime)/10);
+//                        while(sleep_time_count>=32768)
+//												{
+//                         sleep_time_count-=32768;											
+//												}
+//												while(sleep_time_count<2000)
+//												{
+//												 sleep_time_count+=10000;
+//												}
 //												if(REV_WG_pack==1)
 //												{
 //												sleep_time_count_LAST=sleep_time_count;
 //												}
 //												if(abs(sleep_time_count_LAST-sleep_time_count)>2000||REV_WG_pack==1)
 //												{
-												 sleep_timer_start(sleep_time_count);
+//												 sleep_timer_start(sleep_time_count);
 													time_count++;
 //												}
                         break;
@@ -615,6 +525,7 @@
 															LoraSendComMap(WGRSP_RWTAG_READ);
 															break;
 												case WGRSP_RWTAG_WRITE:
+
 														rec_index = RX_Buffer[PWTAG_WRITE_IDX_IDX];
 													switch(rec_index)
 															{
@@ -627,12 +538,17 @@
                         default :
                             memcpy(&rec_value,&RX_Buffer[PWTAG_WRITE_VALUE_IDX],2);
                             g_com_map[rec_index/2] = rec_value;
+														if(rec_index/2==POWER)//新增判断
+														{
+														 memcpy(&set_mk_time,&RX_Buffer[PWTAG_WRITE_VALUE_IDX],2);
+															mk_send_message_to_BLE();	//发送给蓝牙
+															//LOG_INFO(TRACE_MODULE_APP, "修改成功g_com表为%#x,时间为%#x\r\n",g_com_map[POWER],set_mk_time);
+														}
                             save_com_map_to_flash();
                             LoraRspWriteCommap(SUBMSG_WRITE_ANCPARA);
-                            //flag_writepara_needreset = 1;
+                            flag_writepara_needreset = 1;
                             no_rx_flag = 1;                           
 															}
-
 															break; 
 												case WGRSP_RWTAG_UPDATE:
 												rec_index = RX_Buffer[PWTAG_WRITE_IDX_IDX];
@@ -657,7 +573,8 @@
 									}
 								}
 
-							}						
+							}
+			
     if(!no_rx_flag)
     {
         if(lora_jianting_flag&&LoraUp_flag==0)
@@ -666,6 +583,7 @@
             Radio.Rx(0);
         }
     }
+		
 }
 
 void OnTxTimeout( void )
@@ -681,6 +599,7 @@
 void OnRxTimeout( void )
 {
     Radio.Standby();
+	
     if(lora_jianting_flag)
     {
         SwitchLoraSettings(UWB_CHANNEL_FRQ+group_id,UWB_CHANNEL_SF,0);

--
Gitblit v1.9.3