From e328ebef585cea2351b37117b2d5ac4978ecd3c0 Mon Sep 17 00:00:00 2001
From: WXK <287788329@qq.com>
Date: 星期二, 11 二月 2025 14:57:23 +0800
Subject: [PATCH] 1111111

---
 keil/include/components/app/src/ranging_ccc_task.c |  129 ++++++++++++++++++++++++------------------
 1 files changed, 74 insertions(+), 55 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..599a434 100644
--- a/keil/include/components/app/src/ranging_ccc_task.c
+++ b/keil/include/components/app/src/ranging_ccc_task.c
@@ -145,11 +145,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_chest(debug_csi.block_index, 1, 2);
 
         uwbapi_report_debug_csi_data(&debug_csi);
     }
@@ -172,6 +172,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 +197,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)
                 {
@@ -262,7 +269,7 @@
                         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
+#if (X38M4_AUTO_TUNE_EN == 1)
                         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
@@ -402,73 +409,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 +501,5 @@
     else if (event)
     {
     }
+    // board_led_off(BOARD_LED_2);
 }

--
Gitblit v1.9.3