From 4cdc4b3d488e15b1d6572bdcd61a2ce92d0c8c9b Mon Sep 17 00:00:00 2001
From: chen <15335560115@163.com>
Date: 星期五, 16 五月 2025 18:13:14 +0800
Subject: [PATCH] 成功分离2个测距功能

---
 keil/include/main/main.c |  132 ++++++++++++++++++++++++++++++++-----------
 1 files changed, 98 insertions(+), 34 deletions(-)

diff --git a/keil/include/main/main.c b/keil/include/main/main.c
index c9a34a8..6c9ab6b 100644
--- a/keil/include/main/main.c
+++ b/keil/include/main/main.c
@@ -50,6 +50,9 @@
 #include "mk_uart.h"
 #include "mk_spi.h"
 #include "mk_flash.h"
+#include "Usart.h"
+#include <serial_at_cmd_app.h>
+#include <global_param.h>
 
 #include "board.h"
 
@@ -110,6 +113,11 @@
 //*****************************************************************************
 #define WSF_BUF_POOLS 5
 void Fira_Change_Task(void);
+extern uint8_t normal_flag,log_4g_enable_flag;
+extern uint16_t ip0,ip1,ip2,ip3,port;
+uint8_t group_id,enable_sleep_count;
+uint32_t dev_id;
+uint16_t disoffset;
 // Default pool descriptor.
 static wsfBufPoolDesc_t poolDescriptors[WSF_BUF_POOLS] = {
     {32, 26}, {64, 24}, {128, 4}, {256 + 32, 4}, {1024 + 32, 2},
@@ -119,6 +127,37 @@
 {
     // LOG_INFO(TRACE_MODULE_APP, "Wake up by sleep timer %d\r\n", time);
 }
+void Program_Init(void)
+{
+    Usart1ParseDataCallback = UsartParseDataHandler;//需改为默认为gps处理,UsartParseDataHandler为升级处理当调试时候改为
+    parameter_init_anchor();//g_com_map表初始化角色默认为基站
+    dev_id=g_com_map[DEV_ID];//这里不太对
+    group_id=(uint8_t)g_com_map[GROUP_ID];//组ID
+//    tag_frequency = 1000/g_com_map[COM_INTERVAL];//测距频率这个存的是测距时间
+    memcpy(&disoffset,&g_com_map[DIST_OFFSET],2);
+//    g_com_map[ALARM_DISTANCE1] = 40;
+//    g_com_map[ALARM_DISTANCE2] = 40;
+//    warning_distance=g_com_map[ALARM_DISTANCE1];
+//    prewarning_distance=g_com_map[ALARM_DISTANCE2];
+		//g_com_map[SEND_4G_SECOND]
+//    if(g_com_map[SEND_4G_SECOND]<30)
+//		{
+//		gps_open_flag=0;
+//		}else{
+//		gps_open_flag=1;
+//		}
+    g_com_map[MODBUS_MODE] = 0;
+		log_4g_enable_flag=g_com_map[LOG_4G_ENABLE];
+    ip0 = (g_com_map[TCP_IP_0]>>12&0xf)*1000+(g_com_map[TCP_IP_0]>>8&0xf)*100+(g_com_map[TCP_IP_0]>>4&0xf)*10+(g_com_map[TCP_IP_0]&0xf);
+    ip1 = (g_com_map[TCP_IP_1]>>12&0xf)*1000+(g_com_map[TCP_IP_1]>>8&0xf)*100+(g_com_map[TCP_IP_1]>>4&0xf)*10+(g_com_map[TCP_IP_1]&0xf);
+    ip2 = (g_com_map[TCP_IP_2]>>12&0xf)*1000+(g_com_map[TCP_IP_2]>>8&0xf)*100+(g_com_map[TCP_IP_2]>>4&0xf)*10+(g_com_map[TCP_IP_2]&0xf);
+    ip3 = (g_com_map[TCP_IP_3]>>12&0xf)*1000+(g_com_map[TCP_IP_3]>>8&0xf)*100+(g_com_map[TCP_IP_3]>>4&0xf)*10+(g_com_map[TCP_IP_3]&0xf);
+    port = g_com_map[TCP_PORT];
+    g_com_map[VERSION] = (1<<8)|0;
+    LOG_INFO(TRACE_MODULE_APP,"设备ID: %x .\r\n",dev_id);
+    LOG_INFO(TRACE_MODULE_APP,"固件版本:4G-GPS定位手环 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
+    LOG_INFO(TRACE_MODULE_APP,"服务器地址: %d.%d.%d.%d:%d.\r\n",ip0,ip1,ip2,ip3,port);
+}
 
 static void board_init(void)
 {
@@ -127,6 +166,7 @@
 
     // Clock configuration
     board_clock_run();
+		boot_deinit();//新加的
     // Pin configuration
     board_pins_config();
     // Trace configuration
@@ -163,6 +203,7 @@
     // Configure IO_04 for RF Switch
     gpio_pin_set_dir(IO_PIN_4, GPIO_DIR_OUT, 0);
 #else
+		Program_Init();
     // led
     board_led_init();
 //    board_led_on(BOARD_LED_1);
@@ -195,15 +236,22 @@
 void Fira_Change_Task(void)
 {
 }
-extern uint8_t normal_flag;
-int main(void)
-{
-    // Initialize MCU system
-    board_init();
 
-    // Disable watchdog timer
+static void uart_receive_callback(void *dev, uint32_t err_code)
+{
+    uart_receive(UART_ID1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart_receive_callback);
+}
+
+int main(void)
+{		
+	// Initialize MCU system
+    board_init();
+	 // Disable watchdog timer
     wdt_close(WDT_ID0);
     LOG_INFO(TRACE_MODULE_APP, "UCI FiRa example\r\n");
+	if(gpio_pin_get_val(MODE_CHANGE_PIN))
+	{
+   
 		
 		// Platform init for WSF
     PalSysInit();
@@ -244,35 +292,40 @@
     uwb_test_init(handlerId);
 #endif
 
+#ifndef MY_MODE
     uwb_open();
 
-//    // set advanced parameters
-//    struct PHY_ADV_CONFIG_T adv_config =
-//    {
-//        // RPM0: 40, RPM3: 60
-//        .thres_fap_detect = 60,
-//        // RPM0: 4, RPM3: 8
-//        .nth_scale_factor = 8,
-//        // RFrame SP0: 0/1, Others: 0/1/2/3
-//        .ranging_performance_mode = 3,
-//#if RX_ANT_PORTS_NUM == 4
-//        .skip_weakest_port_en = 1,
-//#else
-//        .skip_weakest_port_en = 0,
-//#endif
-//    };
-//    phy_adv_params_configure(&adv_config);
+    // set advanced parameters
+    struct PHY_ADV_CONFIG_T adv_config =
+    {
+        // RPM0: 40, RPM3: 60
+        .thres_fap_detect = 60,
+        // RPM0: 4, RPM3: 8
+        .nth_scale_factor = 8,
+        // RFrame SP0: 0/1, Others: 0/1/2/3
+        .ranging_performance_mode = 3,
+#if RX_ANT_PORTS_NUM == 4
+        .skip_weakest_port_en = 1,
+#else
+        .skip_weakest_port_en = 0,
+#endif
+    };
+    phy_adv_params_configure(&adv_config);
 
-//    // which RX ports will be used for AoA/PDoA
-//    phy_rx_ant_mode_set(RX_ANT_PORTS_COMBINATION);
+    // which RX ports will be used for AoA/PDoA
+    phy_rx_ant_mode_set(RX_ANT_PORTS_COMBINATION);
 
-//    uwbs_init();
-//    uwb_app_config.ranging_flow_mode = (uint8_t)(RANGING_FLOW_FIRA);
-//    uwb_app_config.filter_en = (uint8_t)(FILTER_EN);
-//    uwb_app_config.session_param.tx_power_level = board_param.tx_power_fcc[CALIB_CH(uwb_app_config.ppdu_params.ch_num)];
-//    uwb_app_config.ppdu_params.rx_ant_id = (uint8_t)(RX_MAIN_ANT_PORT);
+    uwbs_init();
+    uwb_app_config.ranging_flow_mode = (uint8_t)(RANGING_FLOW_FIRA);
+    uwb_app_config.filter_en = (uint8_t)(FILTER_EN);
+    uwb_app_config.session_param.tx_power_level = board_param.tx_power_fcc[CALIB_CH(uwb_app_config.ppdu_params.ch_num)];
+    uwb_app_config.ppdu_params.rx_ant_id = (uint8_t)(RX_MAIN_ANT_PORT);
+	
+#elif defined MY_MODE
 		Uwb_init();//默认为我们测距配置
 		OpenUWB();
+		uart_receive(UART_ID1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart_receive_callback);
+#endif
     //
     // Create UCI transmission layer task
     //
@@ -291,19 +344,30 @@
 
     // Enable sleep timer
     sleep_timer_open(true, SLEEP_TIMER_MODE_ONESHOT, sleep_timer_callback);
-
+		
     while (1)
     {
         wsfOsDispatcher();
+#ifdef MY_MODE
 				if(normal_flag)
 				{
-				
+				IdleTask();
+				}
+#endif
+        power_manage();
+    }
+	}else{
+//				 adc_open(&usr_adc_cfg);
+		Uwb_init();//默认为我们测距配置
+		OpenUWB();
+		uart_receive(UART_ID1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE,uart_receive_callback);
+			 while (1)
+				{
 				uwb_app_poll();//我们的测距逻辑
 				IdleTask();
 				}
-        power_manage();
-    }
-    
+	}    
+
 }
 
 void app_restore_from_power_down(void)

--
Gitblit v1.9.3