From d875292ca0e18a027067f5c1c6bec205b3cfba31 Mon Sep 17 00:00:00 2001
From: chen <15335560115@163.com>
Date: 星期五, 18 十月 2024 18:24:19 +0800
Subject: [PATCH] 低功耗优化代码控制在500UA左右,缩小每次接收超时时间为2500和帧间隔缩为600,URT端也相应改为400帧间隔,目前实现较稳定测距

---
 keil/uwb_tag_.c |   36 ++++++++++++++++++++++++------------
 1 files changed, 24 insertions(+), 12 deletions(-)

diff --git a/keil/uwb_tag_.c b/keil/uwb_tag_.c
index 5f3ecee..5dafa93 100644
--- a/keil/uwb_tag_.c
+++ b/keil/uwb_tag_.c
@@ -25,7 +25,7 @@
 #define POLL_DELAY  100U //yuan100U
 
 /* Receive response timeout */
-#define RESP_RX_TIMEOUT_US 5000U //Yuan500 10mssuccess 300jixian
+#define RESP_RX_TIMEOUT_US 2500U //Yuan500 10mssuccess 300jixian
 
 /* Field index in frame */
 #define MSG_SEQ_NUM_IDX 2
@@ -33,7 +33,7 @@
 #define FINAL_MSG_RESP_RX_TS_IDX 14
 #define FINAL_MSG_FINAL_TX_TS_IDX 18
 #define DELAY_DEFAULT 50000
-#define DELAY_BETWEEN_TWO_FRAME_UUS 400
+#define DELAY_BETWEEN_TWO_FRAME_UUS 600  //yuan1400
 #define HALF_SECOND_TIME 62400000
 
 uint16_t CmpTagInList(uint16_t tagid);
@@ -220,6 +220,7 @@
 					//获取发射端时钟偏差
 			resp_rx_num++;
 			freq_offset=phy_freq_offset_get();
+			
 		//	freq_offset_filter=average_filter(freq_offset);//获取频偏
 			//int32_t ppm = freq_offset_filter / (int32_t)(ch_center_freq_map[UWB_CH_NUM] * 1e-6);
 			//calib_xtal38m4_load_cap_auto_tune(ppm);//利用电容调整晶振适配频偏应在完整的一包之后调整,需要关闭XTAL_AUTO_TUNE_EN 宏定义避免收包中途校准导致测距错误
@@ -366,7 +367,8 @@
     temp = AddNewTagIntoList(tagid);
 	return temp;
 }
-
+uint32_t count_index;
+int tt=1;
 int TagRange(void)
 {
     // The following peripherals will be initialized in the uwb_open function
@@ -410,6 +412,8 @@
 		poll_tx_ts_i64 = ranging_tx_time_correct(poll_tx_en_start_u32 + phy_shr_duration());//修正时间戳
 		temp_count1=phy_timer_count_get();
 		while(mac_is_busy());//等待发送完成
+		gpio_pin_clr(IO_PIN_5);//测试
+		gpio_pin_set(IO_PIN_5);//测试
 		temp_count2=phy_timer_count_get();
 		resp_rx_en_start_u32 =  poll_tx_en_start_u32+ US_TO_PHY_TIMER_COUNT(POLL_TX_TO_RESP_RX_DLY_US);//设置发送多少s后接收按target独立时间起点算+ US_TO_PHY_TIMER_COUNT(POLL_TX_TO_RESP_RX_DLY_US为发送后要多久开启接收时间
 		temp_resp=resp_rx_en_start_u32;
@@ -418,11 +422,14 @@
 	
 	
 	start_receive_count=phy_timer_count_get();
-	poll_timeout=nearbase_num*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS)+US_TO_PHY_TIMER_COUNT(DELAY_DEFAULT);//多一个多0.4ms默认0.4ms计算为0.125*4*100000,默认开启1mss
+	poll_timeout=nearbase_num*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS)+US_TO_PHY_TIMER_COUNT(RESP_RX_TIMEOUT_US);//多一个多0.4ms默认0.4ms计算为0.125*4*100000,默认开启1mss
 	end_receive_count=start_receive_count+poll_timeout;
+	
 	if(end_receive_count>=UINT32_MAX)
 	{end_receive_count-=UINT32_MAX;}
 	current_count=phy_timer_count_get();		
+	count_index=end_receive_count+HALF_SECOND_TIME;
+//	if(tt){
 	while(current_count<end_receive_count||current_count>end_receive_count+HALF_SECOND_TIME)//循环接受包体,若为124.8K则是+62400000
 		{
 		
@@ -433,7 +440,7 @@
 		if(receive_flag==1)//成功接收数据
 		{
 			receive_flag=0;
-	
+	tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//立即开启接受并设置0超时
 			//接收成功则判断是否为同一组
 			if (rx_buf[MESSAGE_TYPE_IDX] == MBX_RESPONSE&&!memcmp(&rx_buf[TAG_ID_IDX],&dev_id,2)&&rx_buf[GROUP_ID_IDX]==group_id) //判断接收到的数据是否是response数据
 							{	
@@ -455,10 +462,10 @@
 							//break;//成功接收就退出
 					
 		}
-							tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//立即开启接受并设置0超时
-				#ifdef BOXING 
-		gpio_pin_clr(IO_PIN_5);//测试
-			#endif
+							
+//				#ifdef BOXING 
+//		gpio_pin_clr(IO_PIN_5);//测试
+//			#endif
 		}else if(receive_flag==2){//接收出错
 		receive_flag=0;
 		tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//立即开启接受并设置0超时
@@ -466,12 +473,17 @@
 //		while(mac_is_busy());
 		temp_count1=phy_timer_count_get();
 		}
-			#ifdef BOXING 
+	#ifdef BOXING 
 		gpio_pin_clr(IO_PIN_5);//测试
+		gpio_pin_set(IO_PIN_5);//测试
 		#endif
 		}
-//		delay_us(1);
-//		uwb_rx_force_off(1);
+//	}
+		#ifdef BOXING 
+		gpio_pin_clr(IO_PIN_5);//测试
+		#endif
+		delay_us(1);
+		uwb_rx_force_off(1);
 		//dwt_forecetrxoff();
 		 CalculateDists(poll_tx_ts_i64); //计算距离并更新距离表
 		 AnchorListUpdate();//更新存活基站列表

--
Gitblit v1.9.3