chen
2024-11-08 cc432b761c884a0bd8e9d83db0a4e26109fc08b1
keil/include/components/app/src/uwb_radar.c
对比新文件
@@ -0,0 +1,299 @@
/*
 * Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and
 * its subsidiaries and affiliates (collectly called MKSEMI).
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into an MKSEMI
 *    integrated circuit in a product or a software update for such product,
 *    must reproduce the above copyright notice, this list of conditions and
 *    the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 * 3. Neither the name of MKSEMI nor the names of its contributors may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    MKSEMI integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be
 *    reverse engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
#include "mk_trace.h"
#include "mk_power.h"
#include "mk_uwb.h"
#include "mk_timer.h"
#include "wsf_msg.h"
#include "uwb_api.h"
#include "mk_radar.h"
#include "uwb_radar.h"
#if UWB_DUAL_RADAR
#include "lib_fira.h"
#endif
// #include "board.h"
#ifndef UWB_RADAR_INTERVAL
/** Radar standalone mode interval, unit: ms */
#define UWB_RADAR_INTERVAL (100)
#endif
struct UWB_RADAR_ENV_T radar_env;
static struct UWB_RADAR_CB_T uwb_radar_cb;
static struct UWB_OP_T radar_op = {
    .session_configure = NULL,
    .session_start = NULL,
    .session_stop = NULL,
    .session_local_addr_set = NULL,
    .session_peer_addr_set = NULL,
    .session_responder_addr_add = NULL,
    .session_responder_list_clr = NULL,
    .session_dynamic_update_responder_list = NULL,
    .session_set_ccc_ursk = NULL,
    .vendor_session_configure = uwb_radar_configure,
    .vendor_session_start = uwb_radar_start,
    .vendor_session_stop = uwb_radar_stop,
};
static mac_callback_t bak_tx_cb;
static mac_callback_t bak_rx_cb;
static mac_callback_t force_bak_tx_cb;
static mac_callback_t force_bak_rx_cb;
static void uwb_radar_tx_process(struct MAC_HW_REPORT_T *tx_report);
static void uwb_radar_rx_process(struct MAC_HW_REPORT_T *rx_report);
void uwb_radar_session_init(void);
static void uwb_radar_tx_process(struct MAC_HW_REPORT_T *tx_report)
{
    radar_env.rx_ant_idx++;
    if (radar_env.rx_ant_idx == UWB_RADAR_RX_PORT_NUM)
    {
        radar_env.rx_ant_idx = 0;
    }
#if UWB_RADAR_1TNR_MODE == 1
    // board_led_on(BOARD_LED_2);
    if (radar_env.rx_ant_idx == 0)
    {
        power_off_radio();
        WsfSetEvent(uwb_radar_cb.handle_id, UWB_RADAR_SAMPLING_COMPLETE_EVT);
    }
    else
    {
        radar_start(radar_env.rx_ant_idx);
    }
    // board_led_off(BOARD_LED_2);
#else
    power_off_radio();
    WsfSetEvent(uwb_radar_cb.handle_id, UWB_RADAR_SAMPLING_COMPLETE_EVT);
#endif
}
static void uwb_radar_rx_process(struct MAC_HW_REPORT_T *rx_report)
{
    // do nothing
}
int uwb_radar_init(uint8_t handle_id)
{
    /* store handler ID */
    uwb_radar_cb.handle_id = handle_id;
    /* init rx queue */
    WSF_QUEUE_INIT(&uwb_radar_cb.msg_queue);
    uwb_radar_cb.radar_alone_timer.handlerId = handle_id;
    uwb_radar_cb.radar_alone_timer.msg.event = UWB_RADAR_ALONE_WAKEUP_MSG;
    radar_env.sample_count = 0;
    radar_env.sample_num = 0;
    return 0;
}
void uwb_radar_alone_timer_set(void)
{
    WsfTimerStartMs(&uwb_radar_cb.radar_alone_timer, UWB_RADAR_INTERVAL, WSF_TIMER_PERIODIC);
}
void uwb_radar_alone_timer_clr(void)
{
    WsfTimerStop(&uwb_radar_cb.radar_alone_timer);
}
int uwb_radar_deinit(void)
{
    return 0;
}
uint32_t uwb_is_radar_mode(void)
{
    uint32_t isenable = 0;
    uint32_t lock = int_lock();
    isenable = radar_env.enable;
    int_unlock(lock);
    return isenable;
}
void uwb_radar_start(void)
{
    uint32_t lock = int_lock();
    radar_env.sample_count = 0;
    radar_env.sample_num = 0;
    radar_env.rx_ant_idx = 0;
    radar_env.enable = 0x1U;
    mac_get_process_handler(&force_bak_tx_cb, &force_bak_rx_cb);
    int_unlock(lock);
    if (SESSION_TYPE_VENDOR_ALONE_RADAR == uwb_app_config.session_type)
    {
        uwb_radar_alone_timer_set();
    }
}
void uwb_radar_stop(void)
{
    uint32_t lock = int_lock();
    radar_env.sample_count = 0;
    radar_env.sample_num = 0;
    radar_env.enable = 0x0U;
    int_unlock(lock);
    if (SESSION_TYPE_VENDOR_ALONE_RADAR == uwb_app_config.session_type)
    {
        radar_disable();
    }
}
/**
 * @brief UWB radar session initialization function.
 * @note This function will be called by uwbapi_session_init().
 */
void uwb_radar_session_init(void)
{
    uwb_radar_creat_event();
    uwbs_handler_init(&radar_op);
}
void uwb_radar_configure(void)
{
    struct UWB_RADAR_T radar_param;
    radar_param.request = uwb_app_config.session_param.uwb_radar_req;
    radar_param.ant_id = uwb_app_config.session_param.uwb_radar_rx_ant_id;
    radar_param.bandwidth = uwb_app_config.session_param.uwb_radar_bandwidth;
    radar_param.channel_num = uwb_app_config.session_param.uwb_radar_channel_num;
    radar_param.lna_gain_level = uwb_app_config.session_param.uwb_radar_lna_gain_level;
    radar_param.pulse_period = uwb_app_config.session_param.uwb_radar_pulse_period;
    radar_param.ranging_tx_power_level = uwb_app_config.session_param.tx_power_level;
    radar_param.rx_gain_level = uwb_app_config.session_param.uwb_radar_rx_gain_level;
    radar_param.sts_len = uwb_app_config.session_param.uwb_radar_sts_len;
    radar_param.tx_power_level = uwb_app_config.session_param.uwb_radar_tx_power_level;
    radar_config(&radar_param);
}
void uwb_radar_alone_continue_trigger(void)
{
    WsfSetEvent(uwb_radar_cb.handle_id, UWB_RADAR_START_SAMPLING_EVT);
}
void uwb_radar_force_destroy_event(void)
{
    uint32_t lock = int_lock();
    mac_register_process_handler(force_bak_tx_cb, force_bak_rx_cb);
    int_unlock(lock);
}
void uwb_radar_creat_event(void)
{
    uint32_t lock = int_lock();
    if (0 == radar_env.sample_count)
    {
        mac_get_process_handler(&bak_tx_cb, &bak_rx_cb);
        mac_register_process_handler(uwb_radar_tx_process, uwb_radar_rx_process);
    }
    int_unlock(lock);
}
void uwb_radar_destroy_event(void)
{
    uint32_t lock = int_lock();
    radar_env.sample_count++;
    if (radar_env.sample_count >= radar_env.sample_num)
    {
        radar_env.sample_count = 0;
        mac_register_process_handler(bak_tx_cb, bak_rx_cb);
        bak_tx_cb = NULL;
        bak_rx_cb = NULL;
    }
    else
    {
        uwb_radar_alone_continue_trigger();
    }
    int_unlock(lock);
}
bool ranging_twr_idle_evt(uint32_t idle_duration)
{
    bool ret = true;
    radar_env.sample_num = idle_duration / UWB_RADAR_SLOT_DURATION;
#if UWB_DUAL_RADAR
    if (ranging_env.responder_slot_idx[SLOT_RESPONSE] == 2)
    {
        radar_env.sample_num = (idle_duration / UWB_RADAR_SLOT_DURATION) / 2 + (idle_duration / UWB_RADAR_SLOT_DURATION) % 2;
    }
#endif
    // LOG_INFO(TRACE_MODULE_APP, "radar rounds: %u\r\n", radar_env.sample_num);
    if (radar_env.sample_num)
    {
        uwb_radar_alone_continue_trigger();
        ret = false;
    }
    return ret;
}
uint16_t uwb_radar_data_checksum(const uint8_t *data, uint32_t data_len)
{
    uint32_t sum = 0;
    uint16_t check_sum = 0;
    for (uint32_t i = 0; i < data_len; i++)
    {
        sum ^= *(data + i);
    }
    check_sum = (uint16_t)(sum & 0xFFFF);
    return check_sum;
}