zhangbo
2025-05-16 39842cc2707c7ceb890bd963d1d226cc7542bc91
keil/include/components/app/src/ranging_ccc.c
@@ -46,7 +46,9 @@
#include "lib_aoa.h"
#include "lib_ranging.h"
#include "uwb_api.h"
#ifdef RADAR_EN
#include "uwb_radar.h"
#endif
#if KF_EN
#include "lib_kf.h"
#endif
@@ -71,6 +73,32 @@
#endif
static struct RANGING_CB_T ranging_cb;
static struct UWB_OP_T op = {
    .session_configure = ranging_configure,
    .session_start = ranging_start,
    .session_stop = ranging_stop,
    .session_local_addr_set = ranging_local_addr_set,
    .session_peer_addr_set = ranging_peer_addr_set,
    .session_responder_addr_add = ranging_responder_addr_add,
    .session_responder_list_clr = ranging_responder_list_clr,
    .session_responder_num_get = ranging_responder_num_get,
    .session_responder_addr_get = ranging_responder_addr_get,
    .session_set_ccc_ursk = ranging_set_ccc_ursk,
#ifdef RADAR_EN
    .vendor_session_configure = uwb_radar_configure,
    .vendor_session_start = uwb_radar_start,
    .vendor_session_stop = uwb_radar_stop,
#else
    .vendor_session_configure = NULL,
    .vendor_session_start = NULL,
    .vendor_session_stop = NULL,
#endif
};
void app_session_init(void);
extern void ccc_tx_process(struct MAC_HW_REPORT_T *tx_report);
extern void ccc_rx_process(struct MAC_HW_REPORT_T *rx_report);
//------------------------------------------------------------------------------
int ranging_init(uint8_t handle_id)
@@ -98,8 +126,32 @@
    return 0;
}
/**
 * @brief Initialize ranging session.
 * @note This function will be called by uwbapi_session_init().
 */
void app_session_init(void)
{
    // register process handler for MAC TX done and RX done
    mac_register_process_handler(ccc_tx_process, ccc_rx_process);
    uwbs_handler_init(&op);
}
void ranging_configure(void)
{
    ranging_env.uwb_evt_prefetch_time = UWB_EVT_PREFETCH_TIME;
    ranging_env.uwb_rx_open_in_advance = UWB_RX_OPEN_IN_ADVANCE;
    ranging_env.uwb_rx_window = UWB_RX_WINDOW;
    uint32_t compensation = 0;
#if LOW_POWER_EN
    compensation += LPM_PPM_COMPENSATION(LOW_POWER_CLOCK_PPM, uwb_app_config.session_param.ranging_interval) + US_TO_PHY_TIMER_COUNT(80);
#endif
    ranging_env.uwb_period_prefetch_time = UWB_PERIOD_PREFETCH_TIME + compensation;
    ranging_env.uwb_rx_open_in_advance_wakeup = UWB_RX_OPEN_IN_ADVANCE + compensation;
    ranging_env.uwb_rx_window_wakeup = UWB_RX_WINDOW + 2 * compensation;
    uwbs_configure(PHY_TX | PHY_RX, uwb_app_config.session_param.tx_power_level);
#if CSI_EN
@@ -150,12 +202,36 @@
#if RANGING_FOM_FILTER_EN
    ranging_debug_csi_en_set(1);
#endif
    if (uwb_app_config.session_param.sts_config == STS_DYNAMIC)
    {
        mac_ifs_set(PHY_LIFS_PERIOD_MACCLK_LONG, PHY_SIFS_PERIOD_MACCLK_LONG);
    }
#if 1
    LOG_INFO(TRACE_MODULE_CCC, "ranging interval:[%u], slot duration:[%u]\r\n", uwb_app_config.session_param.ranging_interval,
             uwb_app_config.session_param.slot_duration);
    LOG_INFO(TRACE_MODULE_CCC, "responders:[%u], responder slot idx:[%u]\r\n", uwb_app_config.session_param.controlees_num,
             uwb_app_config.session_param.ccc_responder_slot_idx);
    uint8_t rounds_per_block = (uint8_t)(MS_TO_PHY_TIMER_COUNT(uwb_app_config.session_param.ranging_interval) /
                                         (uwb_app_config.session_param.slots_per_round * RSTU_TO_PHY_TIMER_COUNT(uwb_app_config.session_param.slot_duration)));
    LOG_INFO(TRACE_MODULE_CCC, "rounds per block:[%u], slots per round:[%u]\r\n", rounds_per_block, uwb_app_config.session_param.slots_per_round);
    LOG_INFO(TRACE_MODULE_CCC, "channel:[%u], sync_ci:[%u]\r\n", uwb_app_config.ppdu_params.ch_num, uwb_app_config.ppdu_params.code_index);
    LOG_INFO(TRACE_MODULE_CCC, "hopping_mode:[0x%02x], hop_mode_key:[0x%08x]\r\n", uwb_app_config.session_param.hopping_mode,
             uwb_app_config.session_param.hop_mode_key);
#endif
    uwbs_security_enable_set(true);
}
void uwb_pkt_tx_done_ind(const struct MAC_HW_REPORT_T *tx, enum RANGING_STAGE_T stage)
{
    struct UWB_PKT_TX_DONE_IND_T *ind;
    if ((ind = WsfMsgAlloc(sizeof(struct UWB_PKT_TX_DONE_IND_T) + tx->pkt_len)) != NULL)
    // struct UWB_PKT_TX_DONE_IND_T *ind = WsfMsgAlloc(sizeof(struct UWB_PKT_TX_DONE_IND_T) + tx->pkt_len);
    struct UWB_PKT_TX_DONE_IND_T *ind = WsfMsgAlloc(sizeof(struct UWB_PKT_TX_DONE_IND_T));
    if (ind != NULL)
    {
        ind->hdr.event = UWB_PKT_TX_DONE_MSG;
@@ -170,7 +246,7 @@
        if ((ind->tx_len) && (tx->pkt_data != NULL))
        {
            memcpy(ind->tx_data, tx->pkt_data, tx->pkt_len);
            // memcpy(ind->tx_data, tx->pkt_data, tx->pkt_len);
        }
        // Send the message
@@ -190,7 +266,8 @@
void uwb_pkt_rx_done_ind(const struct MAC_HW_REPORT_T *rx, enum RANGING_STAGE_T stage, uint8_t slot_idx)
{
    // send an indication to application
    struct UWB_PKT_RX_DONE_IND_T *ind = WsfMsgAlloc(sizeof(struct UWB_PKT_RX_DONE_IND_T) + rx->pkt_len);
    // struct UWB_PKT_RX_DONE_IND_T *ind = WsfMsgAlloc(sizeof(struct UWB_PKT_RX_DONE_IND_T) + rx->pkt_len);
    struct UWB_PKT_RX_DONE_IND_T *ind = WsfMsgAlloc(sizeof(struct UWB_PKT_RX_DONE_IND_T));
    if (ind != NULL)
    {
@@ -202,45 +279,37 @@
        ind->sts_idx = ranging_env.sts_idx;
        ind->slot_idx = slot_idx;
        ind->status = rx->err_code;
        ind->rssi = rx->rssi;
        ind->snr = rx->snr;
        // ind->rssi = rx->rssi;
        ind->rssi = correct_rssi(rx->rssi);
        ind->snr = correct_snr(rx->snr);
        ind->rx_len = rx->pkt_len;
        if ((rx->pkt_len) && (rx->pkt_data != NULL))
        {
            memcpy(ind->rx_data, rx->pkt_data, rx->pkt_len);
            // memcpy(ind->rx_data, rx->pkt_data, rx->pkt_len);
        }
#if RANGING_FOM_FILTER_EN
        if (ranging_debug_csi_en_get())
        {
            uint8_t frame_idx = 0;
            uint8_t rframe_idx = 0xFF;
            if (ind->ranging_stage == RANGING_POLL)
            {
                frame_idx = 1;
                rframe_idx = 0;
            }
            else if (ind->ranging_stage == RANGING_FINAL)
            {
                frame_idx = 2;
                rframe_idx = 1;
            }
            else if (ind->ranging_stage == RANGING_FINAL_DATA)
            {
                frame_idx = 3;
            }
            else if (ind->ranging_stage == RANGING_RESPONSE)
            {
                frame_idx = 0;
                rframe_idx = 0;
            }
            debug_csi.frame_idx = frame_idx;
            debug_csi.frame[frame_idx].rssi = ind->rssi;
            debug_csi.frame[frame_idx].snr = ind->snr;
            debug_csi.antenna = ranging_env.main_ant_id[0];
            debug_csi.frame_idx = frame_idx;
            uint32_t val = REG_READ(0x40003050);
            debug_csi.frame[frame_idx].rf_gain = (val & 0x07);
@@ -249,13 +318,20 @@
            debug_csi.frame[frame_idx].sfd_cnt = phy_sfd_cnt_get();
            debug_csi.frame[frame_idx].error_code = ind->status;
            if ((rx->err_code != 0x0830) && (rframe_idx != 0xFF))
            if (rx->err_code != 0x0830)
            {
                debug_csi.rframe_idx = rframe_idx;
                dump_preamble_cir(rframe_idx, 128);
                dump_sts_cir(rframe_idx);
                debug_csi.frame[frame_idx].rssi = ind->rssi;
                debug_csi.frame[frame_idx].snr = ind->snr;
                debug_csi.frame[frame_idx].channel_power = REG_READ(0x40002064);
                debug_csi.frame[frame_idx].noise_power = REG_READ(0x40002078);
                debug_csi.frame[frame_idx].freq_offset = phy_freq_offset_get();
                debug_csi.rframe[rframe_idx].freq_offset = phy_freq_offset_get();
                dump_preamble_cir(frame_idx, 128);
                if ((frame_idx == 1) || (frame_idx == 2))
                {
                    dump_sts_cir(frame_idx);
                }
            }
            // prepare to report ranging failure message
@@ -299,13 +375,12 @@
            debug_csi.block_index = block_idx;
            debug_csi.round_index = round_idx;
            debug_csi.ranging_status = ind->status;
            debug_csi.ranging_status = report->status;
        }
#endif
        ind->block_idx = block_idx;
        memcpy(&ind->Tround1, &report->Tround1, sizeof(struct RANGING_REPORT_IND_T) - sizeof(ind->hdr) - sizeof(ind->block_idx));
        memcpy(&ind->Tround1, &report->Tround1, 4 * sizeof(int64_t) + 2);
        // Send the message
        WsfMsgSend(ranging_cb.handle_id, ind);
@@ -316,14 +391,13 @@
    }
}
void ranging_req_update_keys(uint32_t sts_idx, uint8_t key_type)
void ranging_req_update_keys(uint32_t sts_idx)
{
    struct RANGING_UPD_KEYS_T *upd_keys;
    if ((upd_keys = WsfMsgAlloc(sizeof(struct RANGING_UPD_KEYS_T))) != NULL)
    {
        upd_keys->hdr.event = RANGING_UPDATE_KEYS;
        upd_keys->key_type = key_type;
        upd_keys->sts_idx = sts_idx;
        // Send the message