From adfc7e798b9cbdd022bf8df971843436912a0fe5 Mon Sep 17 00:00:00 2001
From: chen <15335560115@163.com>
Date: 星期日, 20 七月 2025 16:58:30 +0800
Subject: [PATCH] 成功移植g_com_map表逻辑,初步测试能读能写,并且TDOA效果和官方一致

---
 keil/include/components/app/src/ranging_ss_twr_task.c |   93 +++++++++++++++++++---------------------------
 1 files changed, 38 insertions(+), 55 deletions(-)

diff --git a/keil/include/components/app/src/ranging_ss_twr_task.c b/keil/include/components/app/src/ranging_ss_twr_task.c
index 1ca8edc..f7fb0fb 100644
--- a/keil/include/components/app/src/ranging_ss_twr_task.c
+++ b/keil/include/components/app/src/ranging_ss_twr_task.c
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and
+ * Copyright (c) 2019-2025 Beijing Hanwei Innovation Technology Ltd. Co. and
  * its subsidiaries and affiliates (collectly called MKSEMI).
  *
  * All rights reserved.
@@ -46,13 +46,15 @@
 #include "ranging_ss_twr.h"
 #include "lib_aoa.h"
 #include "lib_ranging.h"
-#if KF_EN
+#if FILTER_EN
 #include "lib_kf.h"
 #endif
 #include "board.h"
 
 #define PRINT_PAYLOAD_EN 0
 #define PDOA_PRINT_EN 0
+
+int32_t g_freq_offset;
 
 #if FILTER_EN
 static void ranging_result_filter(uint16_t *distance, int16_t *azimuth, int16_t *elevation)
@@ -62,7 +64,6 @@
         return;
     }
 
-#if KF_EN
     float post_range, post_azimuth, post_elevation;
     float azimuth_meas = mk_q7_to_f32(*azimuth);
     float elevation_meas = mk_q7_to_f32(*elevation);
@@ -92,19 +93,6 @@
 
     // LOG_INFO(TRACE_MODULE_APP, "$%u %u %d %d %d %d;\r\n", (uint16_t)(range_meas*100),(uint16_t)(post_range*100),(int16_t)azimuth_meas,(int16_t)post_azimuth,
     // (int16_t)elevation_meas, (int16_t)post_elevation);
-#else
-    float post_range, post_azimuth;
-    int azimuth_meas = mk_q7_to_s16(*azimuth);
-    float range_meas = (float)*distance;
-    // call filter
-    loc_post_filter(0, range_meas, azimuth_meas, &post_range, &post_azimuth);
-    // update distance
-    *distance = (uint16_t)(post_range);
-    // update angle
-    *azimuth = mk_f32_to_q7(post_azimuth);
-
-    // LOG_INFO(TRACE_MODULE_APP, "$%u %u %d %d;\r\n", (uint16_t)(range_meas*100), (uint16_t)(post_range*100),(int16_t)azimuth_meas, (int16_t)post_azimuth);
-#endif
 }
 #endif
 
@@ -118,10 +106,6 @@
  *  \return None.
  */
 /*************************************************************************************************/
- int32_t tof_i;
-   double tof_f;
-	  int64_t Tround;
-		int64_t Treply;
 void ranging_handler(wsfEventMask_t event, const void *param)
 {
     const wsfMsgHdr_t *msg = (const wsfMsgHdr_t *)param;
@@ -148,6 +132,12 @@
             case UWB_PKT_TX_DONE_MSG:
             {
                 const struct UWB_PKT_TX_DONE_IND_T *ind = (const struct UWB_PKT_TX_DONE_IND_T *)param;
+
+                if (ind->status != UWB_TX_OK)
+                {
+                    LOG_INFO(TRACE_MODULE_APP, "UWB TX fail  0x%04x\r\n", ind->status);
+                    break;
+                }
                 if (ind->ranging_stage == RANGING_POLL)
                 {
                     uint16_t seq_num = READ_SHORT(&ind->tx_data[2]);
@@ -196,25 +186,18 @@
                             LOG_INFO(TRACE_MODULE_APP, "RSSI: %ddBm, SNR: %ddB \r\n", ind->rssi, ind->snr);
 #endif
 
+                            int32_t freq_offset_filter = average_filter(g_freq_offset);
+                            LOG_INFO(TRACE_MODULE_APP, "CH Freq Offset %d\r\n", freq_offset_filter);
+
+#if X38M4_AUTO_TUNE_EN
+                            int32_t ppm = freq_offset_filter / (int32_t)(uwb_ch_freq_table[uwb_app_config.ppdu_params->ch_num] * 1e-6);
+                            calib_xtal38m4_load_cap_auto_tune(ppm);
+#endif
+
                             // uint8_t NLoS, FoM;
                             // ranging_fom_get(&NLoS, &FoM);
                             // LOG_INFO(TRACE_MODULE_APP, "NLoS: %u, FoM: %u\r\n", NLoS, FoM);
 
-#if CSI_EN
-                            struct RANGING_TAPS_INF_T taps_inf;
-                            ranging_taps_inf_get(&taps_inf);
-                            LOG_INFO(TRACE_MODULE_APP, "fap: %d, %f\r\n", taps_inf.fap_loc, taps_inf.fap_pow);
-                            LOG_INFO(TRACE_MODULE_APP, "tap1: %d, %f\r\n", taps_inf.tap1_loc, taps_inf.tap1_pow);
-                            LOG_INFO(TRACE_MODULE_APP, "tap2: %d, %f\r\n", taps_inf.tap2_loc, taps_inf.tap2_pow);
-                            LOG_INFO(TRACE_MODULE_APP, "tap3: %d, %f\r\n", taps_inf.tap3_loc, taps_inf.tap3_pow);
-#endif
-                            int32_t freq_offset = phy_freq_offset_get();
-                            int32_t freq_offset_filter = average_filter(freq_offset);
-                            LOG_INFO(TRACE_MODULE_APP, "CH Freq Offset %d\r\n", freq_offset_filter);
-#if XTAL_AUTO_TUNE_EN
-                            int32_t ppm = freq_offset_filter / (int32_t)(ch_center_freq_map[uwb_app_config.ppdu_params.ch_num] * 1e-6);
-                            calib_xtal38m4_load_cap_auto_tune(ppm);
-#endif
 #if USER_DEFINED_DATA_REPORT_EN
                             if (ind->rx_len - POLL_MSG_USER_DATA_IDX)
                             {
@@ -230,12 +213,11 @@
                             LOG_INFO(TRACE_MODULE_APP, "RSSI: %ddBm, SNR: %ddB \r\n", ind->rssi, ind->snr);
 #endif
 
-                            int32_t freq_offset = phy_freq_offset_get();
-                            int32_t freq_offset_filter = average_filter(freq_offset);
+                            int32_t freq_offset_filter = average_filter(g_freq_offset);
                             LOG_INFO(TRACE_MODULE_APP, "CH Freq Offset %d\r\n", freq_offset_filter);
 
-                            Tround = ranging_tround();
-                            Treply = 0;
+                            int64_t Tround = ranging_tround();
+                            int64_t Treply = 0;
 
                             for (int i = 4; i >= 0; i--)
                             {
@@ -247,32 +229,33 @@
                             if ((Tround) && (Treply))
                             {
                                 // corrected by frequency offset
-                                Tround = (int64_t)((double)Tround * (1 - (double)freq_offset_filter / ch_center_freq_map[uwb_app_config.ppdu_params.ch_num]));
-                                tof_i = (int32_t)(Tround - Treply) / 2;
+                                Tround = (int64_t)((double)Tround * (1 - (double)freq_offset_filter / uwb_ch_freq_table[uwb_app_config.ppdu_params->ch_num]));
+                                int32_t tof_i = (int32_t)(Tround - Treply) / 2;
 
                                 // outlier filter
                                 if (tof_i < 0)
                                 {
                                     tof_i = 0;
                                 }
-																
-                                ranging_env.tof = (uint32_t)(tof_i);
-																
-                                tof_f = (double)TIMESTAMP_UNIT_TO_NS(ranging_env.tof);
 
-                                struct RANGING_MEASUREMENT_T *range_result = &ranging_env.range_data.measurements[0];
+                                ranging_env.tof = (uint32_t)(tof_i);
+
+                                double tof_f = (double)TIMESTAMP_UNIT_TO_NS(ranging_env.tof);
+
+                                struct TWR_MEASUREMENT_T *range_result = &ranging_env.range_data.measurements[0];
 
                                 // update distance result
                                 range_result->distance = (uint16_t)(tof_f * 0.299702547 * VP_VAL - RANGING_CORR);
-																
-																LOG_INFO(TRACE_MODULE_APP, "tof_f  %lf,tof_i %d\r\n", tof_f,tof_i);
-																
-                                range_result->status = STATUS_OK;
 
-                                if (ranging_frame_type_get() == SP1)
+                                if (uwb_app_config.ppdu_params->sts_pkt_cfg != SP0)
                                 {
                                     range_result->status = sts_valid_check() ? STATUS_OK : STATUS_FAILED;
                                 }
+                                else
+                                {
+                                    range_result->status = STATUS_OK;
+                                }
+
                                 // STS valid
                                 if (range_result->status == STATUS_OK)
                                 {
@@ -283,9 +266,9 @@
 
 #if PDOA_PRINT_EN
                                     float pdoa[3];
-                                    pdoa[0] = pdoa_select_get(0, 3);
-                                    pdoa[1] = pdoa_select_get(1, 3);
-                                    pdoa[2] = pdoa_select_get(2, 3);
+                                    pdoa[0] = pdoa_result_get(0, 3);
+                                    pdoa[1] = pdoa_result_get(1, 3);
+                                    pdoa[2] = pdoa_result_get(2, 3);
                                     LOG_INFO(TRACE_MODULE_APP, "PDOA: %f %f %f\r\n", pdoa[0], pdoa[1], pdoa[2]);
 
                                     // float *sts_rssi = sts_rssi_output_get();
@@ -323,7 +306,7 @@
 
                                     struct RANGE_DATA_T *range_data = &ranging_env.range_data;
 
-                                    range_data->ranging_type = 0x01;
+                                    range_data->ranging_type = 0x01; // TWR (SS-TWR, DS-TWR)
                                     range_data->measurements_num = 1;
                                     uint16_t target_addr = uwbs_peer_short_addr_get();
                                     range_result->mac_addr[0] = target_addr & 0xff;

--
Gitblit v1.9.3