yincheng.zhong
2024-08-20 7744fffacb03dc81cc9dbaf9f5d86a0f21e79c03
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
/*
 * 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_wdt.h"
#include "mk_reset.h"
#include "mk_gpio.h"
#include "mk_misc.h"
#include "mk_sleep_timer.h"
#include "mk_power.h"
#include "mk_uwb.h"
#include "mk_calib.h"
#include "mk_flash.h"
 
#include "board.h"
 
#include "pal_sys.h"
#include "wsf_os.h"
#include "wsf_timer.h"
#include "wsf_buf.h"
#include "wsf_nvm.h"
 
#include "app.h"
#include "uwb_api.h"
 
#include "uwb_radar.h"
 
//*****************************************************************************
//
// WSF buffer pools.
//
//*****************************************************************************
#define WSF_BUF_POOLS 5
 
// Default pool descriptor.
static wsfBufPoolDesc_t poolDescriptors[WSF_BUF_POOLS] = {
    {32, 26}, {64, 24}, {128, 4}, {256 + 32, 4}, {1024 + 32, 2},
};
 
static void sleep_timer_callback(void *dev, uint32_t time)
{
    // LOG_INFO(TRACE_MODULE_APP, "Wake up by sleep timer %d\r\n", time);
}
 
static void board_init(void)
{
    // Clock configuration
    board_clock_run();
    // Pin configuration
    board_pins_config();
    // Trace configuration
    board_debug_console_open(TRACE_PORT);
    // Reset reason
    reset_cause_get();
    reset_cause_clear();
 
    // Load calibration parameters from NVM
    uint32_t internal_flash = (REG_READ(0x40000018) >> 17) & 0x1;
    uint32_t external_flash = (REG_READ(0x40010030) >> 28) & 0x3;
    if (internal_flash || external_flash == 1)
    {
        WsfNvmInit();
        board_calibration_params_load();
        flash_close(FLASH_ID0);
    }
    else
    {
        board_calibration_params_default();
    }
 
    // Chip calibration
    calib_chip();
 
    gpio_open();
 
    board_led_init();
    board_configure();
 
    app_restore_from_power_down();
}
 
int main(void)
{
    // Initialize MCU system
    board_init();
 
    // Disable watchdog timer
    wdt_close(WDT_ID0);
 
#if UWB_RADAR_1TNR_MODE == 1
    LOG_INFO(TRACE_MODULE_APP, "UWB radar 1T%dR overlay mode example\r\n", UWB_RADAR_RX_PORT_NUM);
#else
    LOG_INFO(TRACE_MODULE_APP, "UWB radar 1T%dR single mode example\r\n", UWB_RADAR_RX_PORT_NUM);
#endif
 
    // Platform init for WSF
    PalSysInit();
 
    // Initialize os
 
    //
    // Set up timers for the WSF scheduler.
    //
    WsfOsInit();
    WsfTimerInit();
    sys_tick_callback_set(WsfTimerUpdateTicks);
 
    //
    // Initialize a buffer pool for WSF dynamic memory needs.
    //
    uint32_t wsfBufMemLen = WsfBufInit(WSF_BUF_POOLS, poolDescriptors);
 
    if (wsfBufMemLen > FREE_MEM_SIZE)
    {
        LOG_INFO(TRACE_MODULE_APP, "Memory pool is not enough %d\r\n", wsfBufMemLen - FREE_MEM_SIZE);
    }
 
    //
    // Create app task
    //
    wsfHandlerId_t handlerId = WsfOsSetNextHandler(app_handler);
    app_init(handlerId);
 
    //
    // Create UWB RADAR task
    //
    handlerId = WsfOsSetNextHandler(uwb_radar_handler);
    uwb_radar_init(handlerId);
 
    uwb_open();
 
    uwbs_init();
    uwb_app_config.ranging_flow_mode = (uint8_t)(RANGING_FLOW_NONE);
    uwb_app_config.session_param.tx_power_level = board_param.tx_power_fcc[CALIB_CH(uwb_app_config.ppdu_params.ch_num)];
 
    // Initialize session
    uint32_t session_id = 0x0001;
    uwbapi_session_init(session_id, SESSION_TYPE_VENDOR_ALONE_RADAR);
 
    // Initialize parameters
    struct APP_CFG_PARAM_T param = {0};
    param.ch_num = UWB_CH_NUM;
    param.prf_mode = UWB_MEAN_PRF;
    param.preamble_code_index = UWB_PREAMBLE_CODE_IDX;
    param.preamble_duration = UWB_PREAMBLE_DURATION;
    param.sfd_id = UWB_SFD_ID;
    param.psdu_data_rate = UWB_PSDU_DATA_RATE;
    param.sts_segment_num = UWB_STS_SEGMENT_NUM;
    param.sts_segment_len = UWB_STS_SEGMENT_LEN;
    param.aoa_result_req = 0;
    param.rframe_config = UWB_RFRAME_TYPE;
    param.sts_config = STS_STATIC;
    param.result_report_config = 0x0F;
    param.ranging_round_control = 0x3;
 
    param.uwb_radar_bandwidth = UWB_RADAR_BANDWIDTH;
    param.uwb_radar_channel_num = UWB_RADAR_CHANNEL_NUM;
    param.uwb_radar_lna_gain_level = UWB_RADAR_LNA_GAIN_LEVEL;
    param.uwb_radar_rx_gain_level = UWB_RADAR_FILTRE_GAIN_LEVEL;
    param.uwb_radar_pulse_period = UWB_RADAR_PULSE_PERIOD;
    param.uwb_radar_rx_ant_id = UWB_RADAR_RX_ANT_ID;
    param.uwb_radar_sts_len = UWB_RADAR_STS_LENGTH;
    param.uwb_radar_tx_power_level = UWB_RADAR_TX_POWER_LEVEL;
 
    // Configure ranging parameters
    uwbapi_session_set_app_config(session_id, &param);
 
    // Start uwb radar
    uwbapi_session_start(session_id, NULL);
 
    // Initialize low power mode
    power_init();
 
#if LOW_POWER_EN
    power_mode_request(POWER_UNIT_USER, POWER_MODE_POWER_DOWN);
#else
    power_mode_request(POWER_UNIT_USER, POWER_MODE_SLEEP);
#endif
 
    // Enable sleep timer
    sleep_timer_open(true, SLEEP_TIMER_MODE_ONESHOT, sleep_timer_callback);
 
    while (1)
    {
        wsfOsDispatcher();
        power_manage();
    }
}
 
void app_restore_from_power_down(void)
{
#if UWB_RADAR_DETECT_PROCESS_EN == 0
    // If the results are not calculated locally, the data
    // needs to be output through the peripheral interface UART or SPI.
    struct UART_CFG_T radar_xfer_cfg = {
        .parity = UART_PARITY_NONE,
        .stop = UART_STOP_BITS_1,
        .data = UART_DATA_BITS_8,
        .flow = UART_FLOW_CONTROL_NONE,
        .rx_level = UART_RXFIFO_CHAR_1,
        .tx_level = UART_TXFIFO_EMPTY,
        .baud = BAUD_2000000,
        .dma_en = true,
        .int_rx = false,
        .int_tx = false,
    };
 
    uart_open(DUMP_DATA_PORT, &radar_xfer_cfg);
#endif
}