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_ccc_task.c |  168 ++++++++++++++++++++++++++++----------------------------
 1 files changed, 84 insertions(+), 84 deletions(-)

diff --git a/keil/include/components/app/src/ranging_ccc_task.c b/keil/include/components/app/src/ranging_ccc_task.c
index feaeef9..8208d10 100644
--- a/keil/include/components/app/src/ranging_ccc_task.c
+++ b/keil/include/components/app/src/ranging_ccc_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.
@@ -49,7 +49,7 @@
 #include "lib_aoa.h"
 #include "lib_ranging.h"
 
-#if KF_EN
+#if FILTER_EN
 #include "lib_kf.h"
 #endif
 
@@ -70,7 +70,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);
@@ -100,19 +99,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
 
@@ -145,11 +131,11 @@
         debug_csi.distance_cm = distance;
 
         debug_csi.azimuth = ranging_env.azimuth;
-        debug_csi.ranging_fom = ranging_fom_calculate(&debug_csi, 0, NULL, 0);
+        debug_csi.ranging_fom = ranging_fom_calculate(&debug_csi, 1, 1, NULL, 0);
         debug_csi.azimuth_fom = ranging_env.azimuth_fom;
 
-        calculate_first_tap_power(4, 2);
-        // print_preamble_chest(4, 2);
+        calculate_first_tap_power(1, 2);
+        // print_preamble_cir(debug_csi.block_index, 1, 2);
 
         uwbapi_report_debug_csi_data(&debug_csi);
     }
@@ -172,6 +158,7 @@
 /*************************************************************************************************/
 void ranging_handler(wsfEventMask_t event, const void *param)
 {
+    // board_led_on(BOARD_LED_2);
     const wsfMsgHdr_t *msg = (const wsfMsgHdr_t *)param;
 
     if (msg != NULL)
@@ -196,13 +183,19 @@
             case RANGING_UPDATE_KEYS:
             {
                 const struct RANGING_UPD_KEYS_T *upd_keys = (const struct RANGING_UPD_KEYS_T *)param;
-                ranging_update_keys(upd_keys->sts_idx, upd_keys->key_type);
+                ranging_update_keys(upd_keys->sts_idx);
             }
             break;
 
             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_PRE_POLL)
                 {
@@ -225,6 +218,10 @@
                 else if (ind->ranging_stage == RANGING_FINAL_DATA)
                 {
                     LOG_INFO(TRACE_MODULE_APP, "[TX][%u] Final-Data\r\n", ind->tx_len);
+
+                    // need to set RESPONDER_NUM = 1 for response cir printing
+                    // calculate_first_tap_power(0, 1);
+                    // print_preamble_cir(debug_csi.block_index, 0, 1);
                 }
                 else
                 {
@@ -262,8 +259,8 @@
                         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);
+#if (X38M4_AUTO_TUNE_EN == 1)
+                        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
                     }
@@ -338,24 +335,15 @@
                             }
 
                             // 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();
                             // LOG_INFO(TRACE_MODULE_APP, "STS RSSI: %f %f %f %f\r\n", sts_rssi[0], sts_rssi[1], sts_rssi[2], sts_rssi[3]);
 #endif
                         }
-
-#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
                     }
                     else if (ind->ranging_stage == RANGING_FINAL_DATA)
                     {
@@ -402,73 +390,84 @@
             case RANGING_REPORT_MSG:
             {
                 const struct RANGING_REPORT_IND_T *ind = (const struct RANGING_REPORT_IND_T *)param;
+                uint16_t distance = 0;
 
-                int64_t tof_i = (ind->Tround1 * ind->Tround2 - ind->Treply1 * ind->Treply2) / (ind->Tround1 + ind->Tround2 + ind->Treply1 + ind->Treply2);
-
-                // outlier filter
-                if (tof_i < 0)
+                if (ind->status == 0)
                 {
-                    tof_i = 0;
-                }
+                    int64_t tof_i = (ind->Tround1 * ind->Tround2 - ind->Treply1 * ind->Treply2) / (ind->Tround1 + ind->Tround2 + ind->Treply1 + ind->Treply2);
 
-                double tof_f = (double)TIMESTAMP_UNIT_TO_NS((uint32_t)tof_i);
+                    // outlier filter
+                    if (tof_i < 0)
+                    {
+                        tof_i = 0;
+                    }
 
-                uint16_t distance = (uint16_t)(tof_f * 0.299702547 * VP_VAL - RANGING_CORR);
+                    // LOG_INFO(TRACE_NO_OPTION | TRACE_MODULE_APP, "Tround1 %u Treply1 %u Tround2 %u Treply2 %u\r\n", (uint32_t)ind->Tround1,
+                    // (uint32_t)ind->Treply1, (uint32_t)ind->Tround2, (uint32_t)ind->Treply2);
+
+                    double tof_f = (double)TIMESTAMP_UNIT_TO_NS((uint32_t)tof_i);
+
+                    distance = (uint16_t)(tof_f * 0.299702547 * VP_VAL - RANGING_CORR);
 
 #if FILTER_EN
-                if (uwb_app_config.filter_en)
-                {
-                    // filter process
-                    LOG_INFO(TRACE_MODULE_APP, "Raw Distance %ucm\r\n", distance);
+                    if (uwb_app_config.filter_en)
+                    {
+                        // filter process
+                        LOG_INFO(TRACE_MODULE_APP, "Raw Distance %ucm\r\n", distance);
 
-                    ranging_result_filter(&distance, &ranging_env.azimuth, &ranging_env.elevation);
-                }
+                        ranging_result_filter(&distance, &ranging_env.azimuth, &ranging_env.elevation);
+                    }
 #endif
-                if (uwb_app_config.session_param.aoa_result_req)
-                {
-                    board_ranging_result_correct(&distance, &ranging_env.azimuth, &ranging_env.elevation);
+                    if (uwb_app_config.session_param.aoa_result_req)
+                    {
+                        board_ranging_result_correct(&distance, &ranging_env.azimuth, &ranging_env.elevation);
 
-                    LOG_INFO(TRACE_MODULE_APP, "Peer %X, Slot idx %d, Distance %ucm Azimuth %d FoM %d\r\n", uwbs_peer_short_addr_get(),
-                             ranging_env.responder_slot_idx, distance, mk_q7_to_s16(ranging_env.azimuth), ranging_env.azimuth_fom);
+                        LOG_INFO(TRACE_MODULE_APP, "Peer %X, Slot idx %d, Distance %ucm Azimuth %d FoM %d\r\n", uwbs_peer_short_addr_get(),
+                                 ranging_env.responder_slot_idx, distance, mk_q7_to_s16(ranging_env.azimuth), ranging_env.azimuth_fom);
+                    }
+                    else
+                    {
+                        LOG_INFO(TRACE_MODULE_APP, "Peer %X, Slot idx %d, Distance %ucm\r\n", uwbs_peer_short_addr_get(), ranging_env.responder_slot_idx,
+                                 distance);
+                    }
+
+                    if (!ranging_debug_csi_en_get())
+                    {
+                        struct CCC_RANGE_DATA_T *range_data = &ranging_env.range_data;
+                        struct CCC_MEASUREMENT_T *range_result = &range_data->measurements[0];
+
+                        range_data->measurements_num = 1;
+                        range_data->ranging_type = 0x05; // CCC Controlee (Vehicle) Ranging Measurement
+                        range_data->mac_addr_mode = 0;
+                        range_data->sequence_num = ind->block_idx;
+                        range_data->session_id = ranging_env.session_id;
+                        range_data->ranging_interval = PHY_TIMER_COUNT_TO_MS(ranging_env.block_duration);
+
+                        range_result->status = STATUS_OK;
+                        range_result->slot_index = ranging_env.responder_slot_idx;
+                        range_result->rr_index = ranging_env.round_idx;
+                        range_result->sts_index = ranging_env.sts_idx;
+                        range_result->distance = distance;
+                        range_result->aoa_azimuth = ranging_env.azimuth;
+                        range_result->aoa_azimuth_fom = ranging_env.azimuth_fom;
+
+                        range_result->anchor_fom = (ranging_env.poll_fom + ranging_env.final_fom) / 2;
+                        range_result->initiator_fom = 0;
+
+                        // output result
+                        uwbapi_report_ranging_data(range_data);
+                    }
                 }
                 else
                 {
-                    LOG_INFO(TRACE_MODULE_APP, "Peer %X, Slot idx %d, Distance %ucm\r\n", uwbs_peer_short_addr_get(), ranging_env.responder_slot_idx, distance);
-                }
-
-                if (!ranging_debug_csi_en_get())
-                {
-                    struct RANGE_DATA_T *range_data = &ranging_env.range_data;
-                    struct RANGING_MEASUREMENT_T *range_result = &range_data->measurements[0];
-
-                    range_result->NLoS = MAX(ranging_env.poll_nlos, ranging_env.final_nlos);
-                    range_result->distance = distance;
-                    range_result->aoa_azimuth = ranging_env.azimuth;
-                    range_result->aoa_azimuth_fom = ranging_env.azimuth_fom;
-
-                    range_data->measurements_num = 1;
-                    range_data->ranging_type = 0x01;
-                    range_data->mac_addr_mode = 0;
-                    range_data->sequence_num = ind->block_idx;
-                    range_data->session_id = ranging_env.session_id;
-                    range_data->ranging_interval = PHY_TIMER_COUNT_TO_MS(ranging_env.block_duration);
-
-                    uint16_t target_addr = ranging_responder_addr_get(0);
-                    range_result->mac_addr[0] = target_addr & 0xff;
-                    range_result->mac_addr[1] = (target_addr >> 8) & 0xff;
-
-                    range_result->slot_idx = ranging_env.responder_slot_idx;
-                    range_result->status = STATUS_OK;
-
-                    // output result
-                    uwbapi_report_ranging_data(range_data);
+                    LOG_INFO(TRACE_NO_OPTION | TRACE_MODULE_APP, "Ranging fail %d\r\n", ind->status);
                 }
 
 #if RANGING_FOM_FILTER_EN
                 ranging_report_debug_csi_while_successed(distance, (ind->status == 0) && (distance == 0));
 
-                LOG_INFO(TRACE_MODULE_APP, "Poll-FoM %d, Response-FoM %d Final-FoM %d, Poll-NLoS %d, Response-NLoS %d Final-NLoS %d, C-FoM %d\r\n",
-                         ranging_env.poll_fom, ind->resp_fom, ranging_env.final_fom, ranging_env.poll_nlos, 0, ranging_env.final_nlos, debug_csi.ranging_fom);
+                LOG_INFO(TRACE_MODULE_APP, "FoM %d-%d-%d, NLoS %d-%d-%d, C-FoM %d\r\n", ranging_env.poll_fom, ind->resp_fom, ranging_env.final_fom,
+                         ranging_env.poll_nlos, 0, ranging_env.final_nlos, debug_csi.ranging_fom);
 
                 memset(&debug_csi, 0x00, sizeof(debug_csi));
 #endif
@@ -483,4 +482,5 @@
     else if (event)
     {
     }
+    // board_led_off(BOARD_LED_2);
 }

--
Gitblit v1.9.3