/* * 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 "uwb_trx.h" #include "wsf_msg.h" #ifndef RANGING_EN #define RANGING_EN (0) #endif #if RANGING_EN || CSI_EN #include "lib_ranging.h" #endif #include "lib_aoa.h" static struct UWB_CB_T uwb_cb; static void uwb_tx_process(struct MAC_HW_REPORT_T *tx_report); static void uwb_rx_process(struct MAC_HW_REPORT_T *rx_report); int uwb_init(uint8_t handle_id) { /* store handler ID */ uwb_cb.handle_id = handle_id; /* init rx queue */ WSF_QUEUE_INIT(&uwb_cb.msg_queue); uwb_open(); // register process handler for MAC TX done and RX done mac_register_process_handler(uwb_tx_process, uwb_rx_process); // which RX ports will be used for AoA/PDoA phy_rx_ant_mode_set(RX_ANT_PORTS_COMBINATION); #if CSI_EN ranging_aux_out_opt_set(CH_LEN_DEFAULT, 3); #endif return 0; } int uwb_deinit(void) { return 0; } static void uwb_tx_done_ind(struct MAC_HW_REPORT_T *tx) { struct UWB_TX_DONE_IND_T *ind; if ((ind = WsfMsgAlloc(sizeof(struct UWB_TX_DONE_IND_T) + tx->pkt_len)) != NULL) { ind->hdr.event = UWB_TX_DONE_MSG; ind->phy_timer_count = tx->timestamp; ind->status = tx->err_code; #if RANGING_EN if (tx->err_code == UWB_TX_OK) { int64_t timestamp = ranging_tx_time(tx->timestamp); ind->timestamp_frac = timestamp & 0x1ff; ind->timestamp_int = (uint32_t)(timestamp >> 9); } #endif ind->length = tx->pkt_len; if (tx->pkt_data != NULL) { memcpy(ind->data, tx->pkt_data, tx->pkt_len); } // Send the message WsfMsgSend(uwb_cb.handle_id, ind); } else { LOG_WARNING(TRACE_MODULE_UWB, "memory is not enough for UWB_TX_DONE_IND_T\r\n"); } } static void uwb_rx_done_ind(struct MAC_HW_REPORT_T *rx) { // send an indication to application struct UWB_RX_DONE_IND_T *ind; if ((ind = WsfMsgAlloc(sizeof(struct UWB_RX_DONE_IND_T) + rx->pkt_len)) != NULL) { ind->hdr.event = UWB_RX_DONE_MSG; ind->phy_timer_count = rx->timestamp; ind->status = rx->err_code; ind->rssi = rx->rssi; ind->snr = rx->snr; if (rx->err_code == UWB_RX_OK) { uint8_t sts_pkt_cfg = (rx->phy_header >> 28) & 0x3; if ((sts_pkt_cfg != SP0) && (sts_valid_check() == 0)) { ind->status |= UWB_STS_ERR; } if ((rx->pkt_len) && (rx->pkt_data != NULL)) { memcpy(ind->data, rx->pkt_data, rx->pkt_len); } ind->length = rx->pkt_len; #if RANGING_EN int64_t timestamp = ranging_rx_time(rx); ind->timestamp_frac = timestamp & 0x1ff; ind->timestamp_int = (uint32_t)(timestamp >> 9); if (ranging_debug_csi_en_get()) { uint8_t frame_idx = 0; debug_csi.frame_idx = frame_idx; debug_csi.rframe_idx = frame_idx; uint32_t val = REG_READ(0x40003050); debug_csi.frame[frame_idx].rf_gain = (val & 0x07); debug_csi.frame[frame_idx].bb_gain = ((val >> 3) & 0x1f); debug_csi.frame[frame_idx].bd_cnt = phy_bd_cnt_get(); 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) { debug_csi.frame[frame_idx].rssi = ind->rssi; debug_csi.frame[frame_idx].snr = ind->snr; dump_preamble_cir(frame_idx, 128); dump_sts_cir(frame_idx); } } #endif } // Send the message WsfMsgSend(uwb_cb.handle_id, ind); } else { LOG_WARNING(TRACE_MODULE_UWB, "memory is not enough for UWB_RX_DONE_IND_T\r\n"); } } void uwb_tx_process(struct MAC_HW_REPORT_T *tx_report) { uwb_tx_done_ind(tx_report); } void uwb_rx_process(struct MAC_HW_REPORT_T *rx_report) { uwb_rx_done_ind(rx_report); }