| | |
| | | /* |
| | | * 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. |
| | |
| | | #include "lib_aoa.h" |
| | | #include "lib_ranging.h" |
| | | #include "ranging_fira.h" |
| | | #if KF_EN |
| | | #if FILTER_EN |
| | | #include "lib_kf.h" |
| | | #endif |
| | | #if PDOA_3D_EN |
| | |
| | | #define PRINT_PAYLOAD_EN 0 |
| | | #define PRINT_PDOA_IQ_EN 0 |
| | | |
| | | #if MCTT_TEST_EN |
| | | #define PAYLOAD_BUF_MAX 512 |
| | | #if FILTER_EN |
| | | extern void ranging_result_filter(uint8_t *mac_addr, uint16_t *distance, int16_t *azimuth, int16_t *elevation); |
| | | #endif |
| | | |
| | | static const char *work_mode_table[5] = {"Unknown", "SS-TWR Deferred", "DS-TWR Deferred", "SS-TWR Non-deferred", "DS-TWR Non-deferred"}; |
| | |
| | | const wsfMsgHdr_t *msg = (const wsfMsgHdr_t *)param; |
| | | // board_led_on(BOARD_LED_1); |
| | | |
| | | #if MCTT_TEST_EN |
| | | static uint8_t payload_print_buf[PAYLOAD_BUF_MAX]; |
| | | static uint16_t payload_print_len; |
| | | #endif |
| | | static uint8_t defer_to_report = 0; |
| | | |
| | | if (msg != NULL) |
| | |
| | | { |
| | | 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->slot_idx == 0) |
| | | { |
| | | ranging_env.range_data.sequence_num = (ranging_env.phy_sts_index - fira_key.phyStsIdxInit) / ranging_env.slots_per_block; |
| | | uint8_t work_mode_idx = uwb_app_config.session_param.ranging_round_usage < 5 ? uwb_app_config.session_param.ranging_round_usage : 0; |
| | | #if MCTT_TEST_EN |
| | | LOG_INFO(TRACE_NO_OPTION, "%s Init-seq:%u\r\n", work_mode_table[work_mode_idx], ranging_env.range_data.sequence_num + 1); |
| | | #else |
| | | |
| | | LOG_INFO(TRACE_MODULE_APP | TRACE_NO_OPTION, "\r\n"); |
| | | LOG_INFO(TRACE_MODULE_APP, "FiRa %s Initiator SEQ NUM %u\r\n", work_mode_table[work_mode_idx], ranging_env.range_data.sequence_num); |
| | | #endif |
| | | LOG_INFO(TRACE_MODULE_APP, "FiRa %s Controller SEQ NUM %u\r\n", work_mode_table[work_mode_idx], ranging_env.range_data.sequence_num); |
| | | |
| | | uwb_app_config.ranging_count++; |
| | | |
| | | // clear last round measurement |
| | |
| | | } |
| | | |
| | | uint8_t stage_idx = (ind->ranging_stage >= RANGING_RCM && ind->ranging_stage <= RANGING_RRRM) ? (ind->ranging_stage - RANGING_RCM + 1) : 0; |
| | | #if MCTT_TEST_EN |
| | | LOG_INFO(TRACE_NO_LEVEL_TAG | TRACE_NO_MODULE_NAME, "t%s tp=%x\r\n", fira_fsm_table[stage_idx], ind->timestamp); |
| | | #else |
| | | |
| | | LOG_INFO(TRACE_NO_REPORT_HOST | TRACE_MODULE_APP, "[TX][%u] %s\r\n", ind->tx_len, fira_fsm_table[stage_idx]); |
| | | #endif |
| | | |
| | | if (defer_to_report) |
| | | { |
| | |
| | | } |
| | | #endif |
| | | |
| | | //#if MCTT_TEST_EN |
| | | #if 0 |
| | | if (ind->tx_len) |
| | | { |
| | |
| | | { |
| | | uint8_t work_mode_idx = uwb_app_config.session_param.ranging_round_usage < 5 ? uwb_app_config.session_param.ranging_round_usage : 0; |
| | | ranging_env.range_data.sequence_num = (ranging_env.phy_sts_index - fira_key.phyStsIdxInit) / ranging_env.slots_per_block; |
| | | #if MCTT_TEST_EN |
| | | LOG_INFO(TRACE_NO_OPTION, "%s Resp-seq:%u Ridx:%d\r\n", work_mode_table[work_mode_idx], ranging_env.range_data.sequence_num + 1, |
| | | ranging_env.next_round_index); |
| | | #else |
| | | |
| | | LOG_INFO(TRACE_NO_REPORT_HOST | TRACE_MODULE_APP | TRACE_NO_OPTION, "\r\n"); |
| | | LOG_INFO(TRACE_NO_REPORT_HOST | TRACE_MODULE_APP, "FiRa %s Responder SEQ NUM %u\r\n", work_mode_table[work_mode_idx], |
| | | LOG_INFO(TRACE_NO_REPORT_HOST | TRACE_MODULE_APP, "FiRa %s Controlee SEQ NUM %u\r\n", work_mode_table[work_mode_idx], |
| | | ranging_env.range_data.sequence_num); |
| | | #endif |
| | | |
| | | // clear last round measurement |
| | | ranging_measurements_clear(); |
| | | |
| | | int32_t freq_offset = phy_freq_offset_get(); |
| | | ranging_env.freq_offset_filter = average_filter(freq_offset); |
| | | #if MCTT_TEST_EN == 0 |
| | | |
| | | LOG_INFO(TRACE_MODULE_APP, "CH Freq Offset %d\r\n", ranging_env.freq_offset_filter); |
| | | #endif |
| | | #if XTAL_AUTO_TUNE_EN |
| | | int32_t ppm = ranging_env.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 = ranging_env.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 |
| | | } |
| | | |
| | | if (ind->ranging_stage == RANGING_RIM || ind->ranging_stage == RANGING_RRM) |
| | | { |
| | | struct RANGING_MEASUREMENT_T *range_result = 0; |
| | | struct TWR_MEASUREMENT_T *range_result = 0; |
| | | if (ind->ranging_stage == RANGING_RIM) |
| | | { |
| | | range_result = &ranging_env.range_data.measurements[0]; |
| | |
| | | { |
| | | #if AOA_EN |
| | | // update PDoA IQ and calculate AoA angles (depends on aoa_aux_cfg) |
| | | // ~960us -- Keil (AoA - ranging_performance_mode == 0) |
| | | // ~1ms -- Keil (AoA - ranging_performance_mode == 3) |
| | | // ~458us -- Keil (PDoA - ranging_performance_mode == 3) |
| | | // board_led_on(BOARD_LED_1); |
| | | // ~530us -- Keil (AoA - ranging_performance_mode == 3) |
| | | // board_led_on(BOARD_LED_2); |
| | | aoa_calculate(&range_result->aoa_elevation, &range_result->aoa_azimuth); |
| | | // board_led_off(BOARD_LED_1); |
| | | // board_led_off(BOARD_LED_2); |
| | | aoa_fom_get(&range_result->aoa_elevation_fom, &range_result->aoa_azimuth_fom); |
| | | #elif PDOA_3D_EN |
| | | // calculate PDoA angles |
| | | // 324us ~ 349us -- Keil |
| | | // board_led_on(BOARD_LED_1); |
| | | // ~410us -- Keil |
| | | // board_led_on(BOARD_LED_2); |
| | | pdoa_3d_calculate(range_result->mac_addr, &range_result->aoa_elevation, &range_result->aoa_azimuth); |
| | | // board_led_off(BOARD_LED_1); |
| | | // board_led_off(BOARD_LED_2); |
| | | pdoa_fom_get(&range_result->aoa_elevation_fom, &range_result->aoa_azimuth_fom); |
| | | #endif |
| | | |
| | |
| | | } |
| | | |
| | | // 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(); |
| | |
| | | #endif |
| | | } |
| | | |
| | | #if RSSI_EN && (MCTT_TEST_EN == 0) |
| | | #if RSSI_EN |
| | | LOG_INFO(TRACE_MODULE_APP, "RSSI: %ddBm, SNR: %ddB \r\n", ind->rssi, ind->snr); |
| | | #endif |
| | | |
| | |
| | | ranging_fom_get(&NLoS, &FoM); |
| | | range_result->NLoS = NLoS; |
| | | // LOG_INFO(TRACE_MODULE_APP, "NLoS: %u, FoM: %u\r\n", NLoS, FoM); |
| | | |
| | | #if CSI_EN |
| | | ////////// need to increase slot duration for log printing |
| | | 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); |
| | | |
| | | // float chtaps_re[128]; |
| | | // float chtaps_im[128]; |
| | | // ranging_multi_taps_iq_get(chtaps_re, chtaps_im, 128); |
| | | // for (uint8_t i = 0; i < 128; i++) |
| | | //{ |
| | | // LOG_INFO(TRACE_NO_OPTION | TRACE_MODULE_APP, "%f, %f\r\n", chtaps_re[i], chtaps_im[i]); |
| | | //} |
| | | #endif |
| | | } |
| | | else if (ind->ranging_stage == RANGING_RFM) |
| | | { |
| | | struct RANGING_MEASUREMENT_T *range_result = &ranging_env.range_data.measurements[0]; |
| | | struct TWR_MEASUREMENT_T *range_result = &ranging_env.range_data.measurements[0]; |
| | | uint8_t NLoS, FoM; |
| | | ranging_fom_get(&NLoS, &FoM); |
| | | if (NLoS > range_result->NLoS) |
| | |
| | | if (res) |
| | | { |
| | | // LOG_INFO(TRACE_MODULE_APP, "ToF %d\r\n", ranging_env.tof); |
| | | struct RANGING_MEASUREMENT_T *range_result = &ranging_env.range_data.measurements[0]; |
| | | struct TWR_MEASUREMENT_T *range_result = &ranging_env.range_data.measurements[0]; |
| | | ranging_env.range_data.measurements_num = 1; |
| | | range_result->status = STATUS_OK; |
| | | |
| | |
| | | #if FILTER_EN |
| | | if (uwb_app_config.filter_en) |
| | | { |
| | | #if KF_EN |
| | | int16_t *azimuth = (uwb_app_config.session_param.aoa_result_req ? &range_result->aoa_azimuth : NULL); |
| | | #else |
| | | int16_t *azimuth = &range_result->aoa_azimuth; |
| | | #endif |
| | | |
| | | int16_t *elevation = NULL; |
| | | #if PDOA_3D_EN |
| | | if (ANT_PATTERN != ANT_PATTERN_LINEAR) |
| | |
| | | range_result->distance, mk_q7_to_s16(range_result->aoa_azimuth), mk_q7_to_s16(range_result->aoa_elevation), |
| | | range_result->aoa_azimuth_fom); |
| | | #elif AOA_EN |
| | | |
| | | LOG_INFO(TRACE_NO_REPORT_HOST | TRACE_MODULE_APP, "Distance %ucm, AoA Azimuth %d Elevation %d Azimuth FoM %u\r\n", |
| | | range_result->distance, mk_q7_to_s16(range_result->aoa_azimuth), mk_q7_to_s16(range_result->aoa_elevation), |
| | | range_result->aoa_azimuth_fom); |
| | | |
| | | #else |
| | | LOG_INFO(TRACE_NO_REPORT_HOST | TRACE_MODULE_APP, "Distance %ucm\r\n", (int16_t)range_result->distance); |
| | | #endif |
| | |
| | | uwbapi_report_ranging_data(&ranging_env.range_data); |
| | | } |
| | | |
| | | // int8_t expected_rssi = ranging_expected_rssi_get(ranging_tx_power_get(), range_result->distance, 2, 0); |
| | | // LOG_INFO(TRACE_MODULE_APP, "Expected RSSI: %ddBm\r\n", expected_rssi); |
| | | // calculate_first_tap_power(1, 2); |
| | | // print_preamble_cir(ranging_env.range_data.sequence_num, 1, 2); |
| | | // print_sts_cir(1, 2); |
| | | } |
| | | } |
| | | |
| | | uint8_t stage_idx = (ind->ranging_stage >= RANGING_RCM && ind->ranging_stage <= RANGING_RRRM) ? (ind->ranging_stage - RANGING_RCM + 1) : 0; |
| | | #if MCTT_TEST_EN |
| | | if (ind->ranging_stage == RANGING_RCM || ind->ranging_stage == RANGING_MRM || ind->ranging_stage == RANGING_RRRM) |
| | | { |
| | | if (ind->ranging_stage != RANGING_RRRM) |
| | | { |
| | | LOG_INFO(TRACE_NO_LEVEL_TAG | TRACE_NO_MODULE_NAME, "r%s tp=%x,%x,%x,%d\r\n", fira_fsm_table[stage_idx], ind->timestamp, |
| | | ind->phy_header, ranging_env.phy_sts_index, ind->slot_idx); |
| | | } |
| | | else |
| | | { |
| | | LOG_INFO(TRACE_NO_LEVEL_TAG | TRACE_NO_MODULE_NAME, "r%s,%x,%x,%d\r\n", fira_fsm_table[stage_idx], ind->phy_header, |
| | | ranging_env.phy_sts_index, ind->slot_idx); |
| | | } |
| | | } |
| | | else |
| | | { |
| | | LOG_INFO(TRACE_NO_LEVEL_TAG | TRACE_NO_MODULE_NAME, "r%s\r\n", fira_fsm_table[stage_idx]); |
| | | } |
| | | #else |
| | | |
| | | #if DYNAMIC_UPDATE_MAIN_ANTENNA_EN |
| | | if ((ind->ranging_stage == RANGING_RRM) || (ind->ranging_stage == RANGING_RRRM)) |
| | | { |
| | |
| | | { |
| | | LOG_INFO(TRACE_NO_REPORT_HOST | TRACE_MODULE_APP, "[RX][%u] %s\r\n", ind->rx_len, fira_fsm_table[stage_idx]); |
| | | } |
| | | #endif |
| | | |
| | | #if PRINT_PAYLOAD_EN |
| | | if (ind->rx_len) |
| | |
| | | LOG_INFO(TRACE_NO_OPTION | TRACE_MODULE_APP, "\r\n"); |
| | | } |
| | | #endif |
| | | |
| | | #if MCTT_TEST_EN |
| | | if (ind->rx_len) |
| | | { |
| | | if ((payload_print_len + ind->rx_len) < PAYLOAD_BUF_MAX) |
| | | { |
| | | memcpy(payload_print_buf + payload_print_len, ind->rx_data, ind->rx_len); |
| | | payload_print_len += ind->rx_len; |
| | | } |
| | | else |
| | | { |
| | | LOG_INFO(TRACE_NO_OPTION, "Insufficient payload_print_buf, don't print out rx_data.\r\n"); |
| | | } |
| | | } |
| | | #endif |
| | | } |
| | | else |
| | | { |
| | |
| | | #else |
| | | LOG_INFO(TRACE_MODULE_APP, "UWB RX fail 0x%04x\r\n", ind->status); |
| | | #endif |
| | | #if RSSI_EN && (MCTT_TEST_EN == 0) |
| | | #if RSSI_EN |
| | | if (ind->status != 0x0830) |
| | | { |
| | | LOG_INFO(TRACE_MODULE_APP, "RSSI: %ddBm, SNR: %ddB \r\n", ind->rssi, ind->snr); |
| | |
| | | { |
| | | // output result (notify) |
| | | uwbapi_report_ranging_data(&ranging_env.range_data); |
| | | |
| | | // calculate_first_tap_power(0, 1); |
| | | // print_preamble_cir(ranging_env.range_data.sequence_num, 0, 1); |
| | | // print_sts_cir(0, 1); |
| | | } |
| | | } |
| | | } |
| | | break; |
| | | |
| | | #if MCTT_TEST_EN |
| | | case RANGING_ROUND_PRINT_MSG: |
| | | { |
| | | if (payload_print_len > 0) |
| | | { |
| | | for (uint16_t i = 0; i < payload_print_len; i++) |
| | | { |
| | | if (i > 0 && payload_print_buf[i] == 0x49 && payload_print_buf[i + 1] == 0x2B) |
| | | LOG_INFO(TRACE_NO_OPTION, "\r\n"); |
| | | |
| | | LOG_INFO(TRACE_NO_OPTION, "%02x ", payload_print_buf[i]); |
| | | } |
| | | LOG_INFO(TRACE_NO_OPTION, "\r\n"); |
| | | memset(payload_print_buf, 0x0, PAYLOAD_BUF_MAX); |
| | | payload_print_len = 0; |
| | | } |
| | | } |
| | | break; |
| | | #endif |
| | | |
| | | default: |
| | | break; |