WXK
2025-02-11 e328ebef585cea2351b37117b2d5ac4978ecd3c0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 * 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->rssi = correct_rssi(rx->rssi);
        ind->snr = correct_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 CSI_EN
            if (ranging_debug_csi_en_get())
            {
                uint8_t frame_idx = 0;
                debug_csi.frame_idx = frame_idx;
                debug_csi.frame_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;
                    debug_csi.frame[frame_idx].channel_power = REG_READ(0x40002064);
                    debug_csi.frame[frame_idx].noise_power = REG_READ(0x40002078);
                    dump_preamble_cir(frame_idx, 128);
                    dump_sts_cir(frame_idx);
                }
            }
#endif
#if RANGING_EN || CSI_EN
            int64_t timestamp = ranging_rx_time(rx);
            ind->timestamp_frac = timestamp & 0x1ff;
            ind->timestamp_int = (uint32_t)(timestamp >> 9);
#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);
}