From 266dcffc919008dedf2acc84f7f52a3aff83bf07 Mon Sep 17 00:00:00 2001
From: yincheng.zhong <634916154@qq.com>
Date: 星期六, 19 四月 2025 20:00:33 +0800
Subject: [PATCH] 1

---
 Src/application/dw_app.c |  104 +++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 70 insertions(+), 34 deletions(-)

diff --git a/Src/application/dw_app.c b/Src/application/dw_app.c
index 226c813..f2dd915 100644
--- a/Src/application/dw_app.c
+++ b/Src/application/dw_app.c
@@ -81,7 +81,7 @@
     SINGLEPOLL,
 }tag_state=STARTPOLL;
  dwt_config_t config = {
-	2,               /* Channel number. */
+	5,               /* Channel number. */
 	DWT_PRF_64M,     /* Pulse repetition frequency. */
 	DWT_PLEN_128,    /* Preamble length. */
 	DWT_PAC8,        /* Preamble acquisition chunk size. Used in RX only. */
@@ -166,7 +166,7 @@
         *ts += ts_field[i] << (i * 8);
     }
 }
-
+extern u32 id,error_times;
 void Dw1000_Init(void)
 {
 	/* Reset and initialise DW1000.
@@ -175,20 +175,44 @@
     Reset_DW1000();//重启DW1000 /* Target specific drive of RSTn line into DW1000 low for a period. */
 	Spi_ChangePrescaler(SPIx_PRESCALER_SLOW);	//设置为快速模式
     dwt_initialise(DWT_LOADUCODE);//初始化DW1000
+    id =  dwt_readdevid() ;
 	Spi_ChangePrescaler(SPIx_PRESCALER_FAST);	//设置为快速模式
-
+    id =  dwt_readdevid() ;
     /* Configure DW1000. See NOTE 6 below. */
     dwt_configure(&config);//配置DW1000
 	
-
+    id =  dwt_readdevid() ;
 	
     /* Apply default antenna delay value. See NOTE 1 below. */
     dwt_setrxantennadelay(RX_ANT_DLY);		//设置接收天线延迟
     dwt_settxantennadelay(TX_ANT_DLY);		//设置发射天线延迟
-
+    id =  dwt_readdevid() ;
     /* Set expected response's delay and timeout. See NOTE 4 and 5 below.
      * As this example only handles one incoming frame with always the same delay and timeout, those values can be set here once for all. */
 				//设置接收超时时间
+}
+uint8_t uwb_onflag=1;
+void UWB_RXON()
+{
+    if(!g_com_map[MODBUS_MODE])
+    {
+    Dw1000_Init();
+    uwb_onflag=1;
+    g_com_map[UWB_ONFLAG]=uwb_onflag;
+    save_com_map_to_flash();    
+    }
+}
+void UWB_RXOFF()
+{
+    if(!g_com_map[MODBUS_MODE])
+    {
+    dwt_setinterrupt(  DWT_INT_RFCG | (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO | DWT_INT_RXPTO), 0);
+    dwt_forcetrxoff();
+    dwt_entersleep();
+    uwb_onflag=0;
+    g_com_map[UWB_ONFLAG]=uwb_onflag;
+    save_com_map_to_flash();    
+    }
 }
 void Dw1000_App_Init(void)
 {
@@ -402,7 +426,7 @@
 			
 			tag_succ_times++;
 			
-				LED0_BLINK;
+//				LED0_BLINK;
 				
 					memcpy(&anc_id_recv,&rx_buffer[ANCHOR_ID_IDX],2);
 //					if(hex_dist2!=0xffff)
@@ -764,6 +788,7 @@
 			//tyncpoll_time=tagslotpos*slottime;
 			//tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime;	
 			tag_state=NEARPOLL;
+            outrange_times=0;
 		}
 	}else{
 //			tyncpoll_time = (tagslotpos--%max_slotpos)*slottime;
@@ -827,24 +852,32 @@
 extern int32_t dwt_readcarrierintegrator(void) ;
 void CalculateDists(void)
 {
-    for(int i=0;i<11;i++)
+    for(int i=0; i<11; i++)
     {
-        rec_anc_signalpower[i] = exsistbase_list[i];
-      if(exsistbase_list[i]>0)
-      {
+       // rec_anc_signalpower[i] = exsistbase_list[i];
+        if(exsistbase_list[i]==KEEP_TIMES)
+        {
 
-          exsistbase_list[i]--;
-          clockOffsetRatio = anc_clockoffset[i] * (FREQ_OFFSET_MULTIPLIER * HERTZ_TO_PPM_MULTIPLIER_CHAN_2 / 1.0e6) ;
-          rtd_init = tag_resprx[i] - poll_tx_ts;
-          rtd_resp = anc_resptx[i] - anc_pollrx[i];
-          tof = ((rtd_init - rtd_resp * (1 - clockOffsetRatio)) / 2.0) * DWT_TIME_UNITS;
-          distance = tof * SPEED_OF_LIGHT;
-          if(distance>-1000&&distance<100000)
-            nearbase_distlist[i] = distance*100+anc_distoffset[i];
-      }else{
-          nearbase_distlist[i] = 0x1ffff;
-      }
-    
+            exsistbase_list[i]--;
+#ifdef _UWB_4G
+            clockOffsetRatio = anc_clockoffset[i] * (FREQ_OFFSET_MULTIPLIER * HERTZ_TO_PPM_MULTIPLIER_CHAN_2 / 1.0e6) ;
+#else
+            clockOffsetRatio = anc_clockoffset[i] * (FREQ_OFFSET_MULTIPLIER * HERTZ_TO_PPM_MULTIPLIER_CHAN_5 / 1.0e6) ;
+#endif
+            rtd_init = tag_resprx[i] - poll_tx_ts&0xffffffff;
+            rtd_resp = anc_resptx[i] - anc_pollrx[i];
+            tof = ((rtd_init - rtd_resp * (1 - clockOffsetRatio)) / 2.0) * DWT_TIME_UNITS;
+            distance = tof * SPEED_OF_LIGHT;
+            if(distance>-10&&distance<1000)
+            {
+                nearbase_distlist[i] = distance*100+anc_distoffset[i];
+            } else {  
+                nearbase_distlist[i] = 0x1ffff;
+            }
+        } else {
+            nearbase_distlist[i] = 0x1ffff;
+        }
+
     }
 }
 void NearPoll(void)
@@ -1218,10 +1251,10 @@
 		IdleTask();
 		
 	};
-		if(status_reg==0xffffffff)
-					{
-						NVIC_SystemReset();
-					}
+//		if(status_reg==0xffffffff)
+//					{
+//						NVIC_SystemReset();
+//					}
 					if (status_reg & SYS_STATUS_RXFCG)//如果成功接收
 						{
 							dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG | SYS_STATUS_TXFRS);//清楚寄存器标志位				
@@ -1270,6 +1303,7 @@
 }
 u32 id,error_times=0;
 extern float Height;
+extern IWDG_HandleTypeDef hiwdg;
 void Tag_App(void)//发送模式(TAG标签)
 {
 	
@@ -1289,25 +1323,28 @@
 //            SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
 //        }
 //    }
-    Dw1000_Init();
+//    Dw1000_Init();
+    dwt_forcetrxoff();
+    dwt_setinterrupt(  DWT_INT_RFCG | (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO | DWT_INT_RXPTO), 0);
     error_times = 0;
+    HAL_IWDG_Refresh(&hiwdg);
 	switch(tag_state)
 	{
 		case REGPOLL:
-			LED_LG_ON;		
+//			LED_LG_ON;		
 			Registor_Poll();			
-			LED_LG_OFF;
+//			LED_LG_OFF;
 			break;
 		case DISCPOLL:
-			LED_LG_ON;		
+//			LED_LG_ON;		
 			DiscPoll();
-			LED_LG_OFF;
+//			LED_LG_OFF;
 			break;
 		case GETNEARMSG:
 			trygetnearmsg_times++;	
-			LED_LG_ON;
+//			LED_LG_ON;
 				GetNearMsg();	 
-		LED_LG_OFF;
+//		LED_LG_OFF;
 		if(trygetnearmsg_times>5)
 		{
 			tag_state = STARTPOLL;
@@ -1327,5 +1364,4 @@
        dwt_setinterrupt(  DWT_INT_RFCG | (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO | DWT_INT_RXPTO), 1);
 		dwt_setrxtimeout(0);//设定接收超时时间,0位没有超时时间
         dwt_rxenable(0);
-	//dwt_entersleep();
 }

--
Gitblit v1.9.3