/*
|
* 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_trx.h"
|
#include "uwb_api.h"
|
#include "lib_aoa.h"
|
#include "libc_rom.h"
|
|
#define CCA_TEST_EN 0
|
#define NBI_TEST_EN 0
|
#define CSMACA_TEST_EN 0
|
|
extern uint8_t uwb_tx_done;
|
extern uint8_t uwb_rx_done;
|
|
static uint8_t tx_payload[127];
|
// maximum: 1021 @ proprietary mode
|
static uint16_t tx_len = 125;
|
|
static uint32_t test_rounds = 0;
|
static uint8_t test_on_flag = 0;
|
|
//*****************************************************************************
|
//
|
// 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 uwb_update_tx_payload(uint32_t slot_idx)
|
{
|
tx_payload[0] = slot_idx & 0xff;
|
tx_payload[1] = (slot_idx >> 8) & 0xff;
|
tx_payload[2] = (slot_idx >> 16) & 0xff;
|
tx_payload[3] = (slot_idx >> 24) & 0xff;
|
}
|
|
static void uwb_tx_test(void)
|
{
|
uwbs_configure(PHY_TX, uwb_app_config.session_param.tx_power_level);
|
uwb_tx_done = 1;
|
|
app_timer_set(APP_TEST_TIMER2, 50, WSF_TIMER_PERIODIC);
|
}
|
|
#if NBI_TEST_EN
|
static int16_t sweep_freq = -100;
|
#endif
|
|
static void uwb_rx_test(void)
|
{
|
uwbs_configure(PHY_RX, 0);
|
uwb_rx_done = 1;
|
|
#if NBI_TEST_EN
|
phy_nb_filter_config(1, sweep_freq, 3);
|
#endif
|
|
app_timer_set(APP_TEST_TIMER1, 50, WSF_TIMER_PERIODIC);
|
}
|
|
#if CCA_TEST_EN
|
static void phy_done_callback(void *dev, uint32_t int_status)
|
{
|
if (int_status & 0x04)
|
{
|
int32_t rssi = phy_cca_rssi_get();
|
LOG_INFO(TRACE_MODULE_APP, "CCA RSSI %d\r\n", rssi);
|
}
|
LOG_INFO(TRACE_MODULE_APP, "PHY INT status %x\r\n", int_status);
|
}
|
#endif
|
|
//
|
// Button Handlers
|
//
|
static void GPIO_UserHandler(enum IO_PIN_T pin)
|
{
|
// LOG_INFO(TRACE_MODULE_APP, "GPIO Interrupt happen\r\n");
|
|
if (pin == BOARD_SW_1)
|
{
|
app_button_event_set(APP_BUTTON1_EVT);
|
}
|
}
|
|
void app_process_handle(uint8_t msg_id, const void *param)
|
{
|
board_led_toggle(BOARD_LED_2);
|
switch (msg_id)
|
{
|
case APP_TEST_TIMER1_MSG:
|
{
|
LOG_INFO(TRACE_MODULE_APP | TRACE_NO_OPTION, "\r\n");
|
LOG_INFO(TRACE_MODULE_APP, "TEST TIMER1\r\n");
|
|
#if CCA_TEST_EN
|
// Power on radio
|
power_on_radio(0, 1);
|
phy_cca_start(1, phy_done_callback);
|
#else
|
if (uwb_rx_done)
|
{
|
uwb_rx_done = 0;
|
power_off_radio();
|
|
#if NBI_TEST_EN
|
int32_t energy = phy_nb_energy_get();
|
LOG_INFO(TRACE_MODULE_APP, "NB energy %d\r\n", energy);
|
#endif
|
|
sts_lsp_store_stop();
|
sts_lsp_store();
|
|
uwb_rx(0, 0, 50000);
|
}
|
#endif
|
}
|
break;
|
|
case APP_TEST_TIMER2_MSG:
|
{
|
LOG_INFO(TRACE_MODULE_APP | TRACE_NO_OPTION, "\r\n");
|
LOG_INFO(TRACE_MODULE_APP, "TEST TIMER2\r\n");
|
|
if (uwb_tx_done)
|
{
|
uwb_tx_done = 0;
|
power_off_radio();
|
uwb_update_tx_payload(test_rounds++);
|
|
#if CSMACA_TEST_EN
|
uint32_t target_time = phy_timer_count_get() + MS_TO_PHY_TIMER_COUNT(2);
|
uwb_tx(tx_payload, tx_len, 2, target_time);
|
#else
|
if (uwb_app_config.ppdu_params.sts_pkt_cfg == SP3)
|
{
|
uwb_tx(NULL, 0, 0, 0);
|
}
|
else
|
{
|
uwb_tx(tx_payload, tx_len, 0, 0);
|
}
|
#endif
|
}
|
}
|
break;
|
|
case APP_DEBOUNCE_TIMER1_MSG:
|
{
|
if (gpio_pin_get_val(BOARD_SW_1))
|
{
|
return;
|
}
|
|
LOG_INFO(TRACE_MODULE_APP, "DEBOUNCE TIMER1\r\n");
|
|
if (test_on_flag)
|
{
|
uwb_rx_force_off(0);
|
uwb_rx_done = 1;
|
board_led_off(BOARD_LED_1);
|
test_on_flag = 0;
|
}
|
else
|
{
|
board_led_on(BOARD_LED_1);
|
test_on_flag = 1;
|
}
|
}
|
break;
|
|
default:
|
break;
|
}
|
}
|
|
static void board_init(void)
|
{
|
// Clock configuration
|
board_clock_run();
|
// Pin configuration
|
board_pins_config();
|
// Trace configuration
|
board_debug_console_open(TRACE_PORT_UART0);
|
// 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();
|
|
// Configure IO_02 for role selection
|
gpio_open();
|
gpio_pin_set_dir(IO_PIN_2, GPIO_DIR_IN, 0);
|
io_pull_set(IO_PIN_2, IO_PULL_UP, IO_PULL_UP_LEVEL1);
|
|
board_led_init();
|
board_button_init(GPIO_UserHandler);
|
board_configure();
|
}
|
|
int main(void)
|
{
|
// Initialize MCU system
|
board_init();
|
|
// Disable watchdog timer
|
wdt_close(WDT_ID0);
|
LOG_INFO(TRACE_MODULE_APP, "UWB TX/RX test\r\n");
|
|
// 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 task
|
handlerId = WsfOsSetNextHandler(uwb_handler);
|
uwb_init(handlerId);
|
|
// set advanced parameters
|
struct PHY_ADV_CONFIG_T adv_config =
|
{
|
.thres_fap_detect = 40,
|
.nth_scale_factor = 4,
|
.ranging_performance_mode = 0,
|
#if RX_ANT_PORTS_NUM == 4
|
.skip_weakest_port_en = 1,
|
#else
|
.skip_weakest_port_en = 0,
|
#endif
|
};
|
phy_adv_params_configure(&adv_config);
|
|
// Initialize UWBS
|
uwbs_init();
|
uwbs_handler_init(NULL);
|
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)];
|
uwb_app_config.ppdu_params.rx_ant_id = (uint8_t)(RX_MAIN_ANT_PORT);
|
|
// Initialize test session
|
uint32_t session_id = 0x0000;
|
uwbapi_session_init(session_id, SESSION_TYPE_DEVICE_TEST_MODE);
|
|
// Initialize session 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.ranging_round_usage = DS_TWR_DEFERRED;
|
param.mac_address_mode = ARRD_SHORT_USE_SHORT;
|
param.controlees_num = 1;
|
param.multi_node_mode = UNICAST;
|
param.result_report_config = 0x0F;
|
param.ranging_round_control = 0x3;
|
|
// Configure session parameters
|
uwbapi_session_set_app_config(session_id, ¶m);
|
|
if ((uwb_app_config.ppdu_params.sts_pkt_cfg == SP1) || (uwb_app_config.ppdu_params.sts_pkt_cfg == SP3))
|
{
|
struct UWB_STS_KEY_CONFIG_T sts_iv_key;
|
sts_iv_key.sts_vcounter = 0x1F9A3DE4;
|
sts_iv_key.sts_vupper0 = 0xD37EC3CA;
|
sts_iv_key.sts_vupper1 = 0xC44FA8FB;
|
sts_iv_key.sts_vupper2 = 0x362EEB34;
|
sts_iv_key.sts_key0 = 0x14EB220F;
|
sts_iv_key.sts_key1 = 0xF86050A8;
|
sts_iv_key.sts_key2 = 0xD1D336AA;
|
sts_iv_key.sts_key3 = 0x14148674;
|
phy_sts_key_configure(&sts_iv_key);
|
phy_rx_sts_switch_mode_set(uwb_app_config.ppdu_params.sts_pkt_cfg, STS_NEVER_SWITCH, 0, 0);
|
aoa_param_config();
|
}
|
|
#if 0
|
// enable csi information output
|
ranging_debug_csi_en_set(1);
|
#endif
|
|
// Do the test
|
if (gpio_pin_get_val(IO_PIN_2) == 0)
|
{
|
uwb_tx_test();
|
}
|
else
|
{
|
uwb_rx_test();
|
}
|
|
// Initialize low power mode
|
power_init();
|
|
power_mode_request(POWER_UNIT_USER, POWER_MODE_SLEEP);
|
|
// 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)
|
{
|
}
|