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
/*
 * 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;
}