chen
2024-11-01 631a90c1116fa33382a88a747c89bf761bc0fa9b
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
 * 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_uwb.h"
#include "wsf_msg.h"
#include "ranging_ccc.h"
#include "lib_ccc.h"
#include "lib_aoa.h"
#include "lib_ranging.h"
#include "uwb_api.h"
 
#if KF_EN
#include "lib_kf.h"
#endif
 
#if PDOA_3D_EN
#include "lib_pdoa_3d.h"
#endif
 
#if PDOA_3D_EN
#define PDOA_3D_SUPPORT_NUM 10
#define PDOA_3D_TIMEOUT_MS 2000
static struct PDOA_3D_MAC_ADDR_T mac_addr_cache[PDOA_3D_SUPPORT_NUM];
static struct PDOA_3D_PDOA_DATA_T pdoa_data_cache[PDOA_3D_SUPPORT_NUM];
#endif
 
#if KF_EN && FILTER_EN
#define KF_SUPPORT_NUM 6
#define KF_TIMEOUT_MS 1000
static struct KF_MAC_ADDR_T kf_mac_addr_cache[KF_SUPPORT_NUM];
static struct KF_CHANNEL_CACHE_T kf_channel_cache[KF_SUPPORT_NUM];
static struct KF_MAT_VALUE_CACHE_T kf_mat_value_cache[KF_SUPPORT_NUM];
#endif
 
static struct RANGING_CB_T ranging_cb;
 
//------------------------------------------------------------------------------
int ranging_init(uint8_t handle_id)
{
    /* store handler ID */
    ranging_cb.handle_id = handle_id;
 
    /* init rx queue */
    WSF_QUEUE_INIT(&ranging_cb.msg_queue);
 
    ranging_cb.daemon_timer.handlerId = handle_id;
    ranging_cb.daemon_timer.msg.event = RANGING_DAEMON_TIMER_MSG;
 
    LOG_INFO(TRACE_MODULE_APP, "Ranging lib version: %s\r\n", MK8000_get_rangelib_version());
 
#if AOA_EN
    LOG_INFO(TRACE_MODULE_APP, "AoA lib version: %s\r\n", MK8000_get_aoalib_version());
#endif
 
    return 0;
}
 
int ranging_deinit(void)
{
    return 0;
}
 
void ranging_configure(void)
{
    uwbs_configure(PHY_TX | PHY_RX, uwb_app_config.session_param.tx_power_level);
 
#if CSI_EN
    ranging_aux_out_opt_set(CH_LEN_DEFAULT, 3);
#endif
 
#if (ANT_PATTERN == ANT_PATTERN_SQUARE)
    struct AOA_ANGLE_SPAN_T aoa_span;
    aoa_span.Ndim = 2;
    aoa_span.el_low = 90;
    aoa_span.el_high = 90;
    aoa_span.el_step = 1;
    aoa_span.az_low = 0;
    aoa_span.az_high = 359;
    aoa_span.az_step = 1;
    aoa_angle_search_span_set(&aoa_span);
#endif
 
#if AOA_EN
    aoa_aux_info_set(AOA_AUX_ANT_IQ_RSSI_PDOA_AOA_FOM);
    aoa_steering_vector_set((const float *)((uint32_t)((uwb_app_config.ppdu_params.ch_num == 9) ? svec_ch9_ptr : svec_ch5_ptr) | SRAM_BASE));
#else
    aoa_aux_info_set(AOA_AUX_ANT_IQ_RSSI);
#endif
 
    aoa_param_config();
 
#if PDOA_3D_EN
    pdoa_3d_param_config(ANT_PATTERN, ANT_LAYOUT, PDOA_3D_AMBIGUITY_LEVEL_HIGH, mac_addr_cache, pdoa_data_cache, PDOA_3D_SUPPORT_NUM, PDOA_3D_TIMEOUT_MS);
    // pdoa_angle_reverse_set(1, 0);
#endif
 
    LOG_INFO(TRACE_MODULE_CCC, "AoA status:[%d]\r\n", uwb_app_config.session_param.aoa_result_req);
 
#if FILTER_EN
    if (uwb_app_config.filter_en)
    {
#if KF_EN
        loc_post_kf_config(uwb_app_config.session_param.ranging_interval, kf_mac_addr_cache, kf_channel_cache, kf_mat_value_cache, KF_SUPPORT_NUM,
                           KF_TIMEOUT_MS);
#else
        loc_post_filter_config(uwb_app_config.session_param.ranging_interval, 1, uwb_app_config.session_param.aoa_result_req);
#endif
    }
    LOG_INFO(TRACE_MODULE_CCC, "Ranging filter status:[%d]\r\n", uwb_app_config.filter_en);
#endif
 
#if RANGING_FOM_FILTER_EN
    ranging_debug_csi_en_set(1);
#endif
}
 
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)
    {
        ind->hdr.event = UWB_PKT_TX_DONE_MSG;
 
        ind->block_idx = ranging_env.block_idx;
        ind->round_idx = ranging_env.round_idx;
        ind->sts_idx = ranging_env.sts_idx;
 
        ind->ranging_stage = (uint8_t)stage;
        ind->status = tx->err_code;
 
        ind->tx_len = tx->pkt_len;
 
        if ((ind->tx_len) && (tx->pkt_data != NULL))
        {
            memcpy(ind->tx_data, tx->pkt_data, tx->pkt_len);
        }
 
        // Send the message
        WsfMsgSend(ranging_cb.handle_id, ind);
    }
    else
    {
        LOG_WARNING(TRACE_MODULE_APP, "memory is not enough for UWB_PKT_TX_DONE_IND_T\r\n");
    }
}
 
#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-qual"
#endif
 
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);
 
    if (ind != NULL)
    {
        ind->hdr.event = UWB_PKT_RX_DONE_MSG;
        ind->ranging_stage = (uint8_t)stage;
 
        ind->block_idx = ranging_env.block_idx;
        ind->round_idx = ranging_env.round_idx;
        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->rx_len = rx->pkt_len;
 
        if ((rx->pkt_len) && (rx->pkt_data != NULL))
        {
            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;
 
            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) && (rframe_idx != 0xFF))
            {
                debug_csi.rframe_idx = rframe_idx;
                dump_preamble_cir(rframe_idx, 128);
                dump_sts_cir(rframe_idx);
 
                debug_csi.rframe[rframe_idx].freq_offset = phy_freq_offset_get();
            }
 
            // prepare to report ranging failure message
            if (rx->err_code != UWB_RX_OK)
            {
                debug_csi.session_id = ranging_env.session_id;
                debug_csi.sts_index = ind->sts_idx;
                debug_csi.ranging_status = 0xFF;
                debug_csi.block_index = (uint16_t)ind->block_idx;
                debug_csi.round_index = ind->round_idx;
            }
        }
#endif
 
        // Send the message
        WsfMsgSend(ranging_cb.handle_id, ind);
    }
    else
    {
        LOG_WARNING(TRACE_MODULE_APP, "memory is not enough for UWB_PKT_RX_DONE_IND_T\r\n");
    }
}
#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
#pragma clang diagnostic pop
#endif
 
void ranging_report(struct RANGING_REPORT_IND_T *report, uint16_t block_idx, uint16_t round_idx, uint32_t sts_idx)
{
    struct RANGING_REPORT_IND_T *ind;
 
    if ((ind = WsfMsgAlloc(sizeof(struct RANGING_REPORT_IND_T))) != NULL)
    {
        ind->hdr.event = RANGING_REPORT_MSG;
 
#if RANGING_FOM_FILTER_EN
        if (ranging_debug_csi_en_get())
        {
            debug_csi.session_id = ranging_env.session_id;
 
            debug_csi.sts_index = sts_idx;
            debug_csi.block_index = block_idx;
            debug_csi.round_index = round_idx;
 
            debug_csi.ranging_status = ind->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));
 
        // Send the message
        WsfMsgSend(ranging_cb.handle_id, ind);
    }
    else
    {
        LOG_WARNING(TRACE_MODULE_APP, "memory is not enough for RANGING_REPORT_MSG\r\n");
    }
}
 
void ranging_req_update_keys(uint32_t sts_idx, uint8_t key_type)
{
    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
        WsfMsgSend(ranging_cb.handle_id, upd_keys);
    }
    else
    {
        LOG_WARNING(TRACE_MODULE_APP, "memory is not enough for RANGING_UPDATE_KEYS\r\n");
    }
}
 
void ranging_monitor_start(uint32_t time_ms)
{
    WsfTimerStartMs(&ranging_cb.daemon_timer, time_ms, WSF_TIMER_ONE_SHOT);
}
 
void ranging_monitor_stop(void)
{
    WsfTimerStop(&ranging_cb.daemon_timer);
}