/*
|
* 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_uart.h"
|
#include "mk_spi.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 "ranging_fira.h"
|
#include "uwb_api.h"
|
#include "lib_ranging.h"
|
#include "uci_tl_task.h"
|
#include "libc_rom.h"
|
|
#ifdef UWB_UCI_TEST_EN
|
#include "uwb_test.h"
|
#endif
|
|
#ifdef CELL_PHONE_EN
|
#include "mk_efuse.h"
|
|
#define EFUSE_FLASH_EN_ADDR (0x67)
|
#define EFUSE_FLASH_EN_BIT (0x80)
|
|
bool check_flash_bit_of_efuse(void);
|
bool program_efuse(void);
|
|
bool check_flash_bit_of_efuse(void)
|
{
|
uint8_t val;
|
val = efuse_read_byte(EFUSE_FLASH_EN_ADDR);
|
return ((val & EFUSE_FLASH_EN_BIT) ? true : false);
|
}
|
|
bool program_efuse(void)
|
{
|
efuse_program_byte(EFUSE_FLASH_EN_ADDR, EFUSE_FLASH_EN_BIT);
|
if (check_flash_bit_of_efuse())
|
{
|
LOG_INFO(TRACE_MODULE_APP, "Program eFuse INT_FLASH bit successfully.\r\n");
|
return true;
|
}
|
else
|
{
|
LOG_INFO(TRACE_MODULE_APP, "Program eFuse INT_FLASH bit failed.\r\n");
|
return false;
|
}
|
return true;
|
}
|
#endif
|
|
//*****************************************************************************
|
//
|
// WSF buffer pools.
|
//
|
//*****************************************************************************
|
#define WSF_BUF_POOLS 5
|
void Fira_Change_Task(void);
|
// 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)
|
{
|
uint32_t internal_flash = (REG_READ(0x40000018) >> 17) & 0x1;
|
uint32_t external_flash = (REG_READ(0x40010030) >> 28) & 0x3;
|
|
// Clock configuration
|
board_clock_run();
|
// Pin configuration
|
board_pins_config();
|
// Trace configuration
|
board_debug_console_open(TRACE_PORT_UART1);
|
// Reset reason
|
reset_cause_get();
|
reset_cause_clear();
|
|
#ifndef CELL_PHONE_EN
|
// Load calibration parameters from NVM
|
if (internal_flash || external_flash == 1)
|
{
|
WsfNvmInit();
|
board_calibration_params_load();
|
flash_close(FLASH_ID0);
|
}
|
else
|
{
|
board_calibration_params_default();
|
}
|
#else
|
if (internal_flash)
|
{
|
program_efuse();
|
}
|
board_calibration_params_default();
|
#endif
|
|
// Chip calibration
|
calib_chip();
|
|
gpio_open();
|
#ifdef CELL_PHONE_EN
|
// Configure IO_04 for RF Switch
|
gpio_pin_set_dir(IO_PIN_4, GPIO_DIR_OUT, 0);
|
#else
|
// led
|
board_led_init();
|
// board_led_on(BOARD_LED_1);
|
#endif
|
board_configure();
|
}
|
|
void app_process_handle(uint8_t msg_id, const void *param)
|
{
|
switch (msg_id)
|
{
|
case APP_TEST_TIMER1_MSG:
|
{
|
#if ANT_DELAY_CAL_EN
|
// stop calibration
|
uwbapi_session_stop(uwb_app_config.session_id);
|
uwbapi_session_deinit(uwb_app_config.session_id);
|
|
// restart UCI RX
|
uci_tl_rx_restart();
|
#endif
|
}
|
break;
|
|
default:
|
break;
|
}
|
}
|
|
void Fira_Change_Task(void)
|
{
|
}
|
extern uint8_t normal_flag;
|
int main(void)
|
{
|
// Initialize MCU system
|
board_init();
|
|
// Disable watchdog timer
|
wdt_close(WDT_ID0);
|
LOG_INFO(TRACE_MODULE_APP, "UCI FiRa example\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 ranging task or test task
|
//
|
handlerId = WsfOsSetNextHandler(ranging_handler);
|
ranging_init(handlerId);
|
|
#ifdef UWB_UCI_TEST_EN
|
// Create test task
|
handlerId = WsfOsSetNextHandler(uwb_test_handler);
|
uwb_test_init(handlerId);
|
#endif
|
|
uwb_open();
|
|
// set advanced parameters
|
struct PHY_ADV_CONFIG_T adv_config =
|
{
|
// RPM0: 40, RPM3: 60
|
.thres_fap_detect = 60,
|
// RPM0: 4, RPM3: 8
|
.nth_scale_factor = 8,
|
// RFrame SP0: 0/1, Others: 0/1/2/3
|
.ranging_performance_mode = 3,
|
#if RX_ANT_PORTS_NUM == 4
|
.skip_weakest_port_en = 1,
|
#else
|
.skip_weakest_port_en = 0,
|
#endif
|
};
|
phy_adv_params_configure(&adv_config);
|
|
// which RX ports will be used for AoA/PDoA
|
phy_rx_ant_mode_set(RX_ANT_PORTS_COMBINATION);
|
|
uwbs_init();
|
uwb_app_config.ranging_flow_mode = (uint8_t)(RANGING_FLOW_FIRA);
|
uwb_app_config.filter_en = (uint8_t)(FILTER_EN);
|
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);
|
|
|
//
|
// Create UCI transmission layer task
|
//
|
handlerId = WsfOsSetNextHandler(uci_tl_handler);
|
uci_tl_init(handlerId);
|
|
// Initialize low power mode
|
power_init();
|
#if LOW_POWER_EN
|
power_mode_request(POWER_UNIT_USER, POWER_MODE_POWER_DOWN);
|
uwb_app_config.low_power_en = 1;
|
#else
|
power_mode_request(POWER_UNIT_USER, POWER_MODE_SLEEP);
|
uwb_app_config.low_power_en = 0;
|
#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)
|
{
|
}
|