/* * 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_audio.h" #include "uwb_api.h" #include "libc_rom.h" #define INITIATOR_ADDR (0xAAA1) #define RESPONDER0_ADDR (0xBBB1) #define RESPONDER_NUM (1) static uint32_t session_id = 0x0001; static uint8_t transfer_on_flag = 0; static uint32_t test_rounds = 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); } // // 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 uwb_audio_buf_update(uint8_t idx); void uwb_audio_buf_update(uint8_t idx) { uint32_t lock = int_lock(); // board_led_on(BOARD_LED_2); for (uint8_t i = 0; i < UWB_AUDIO_TX_SLOT_NUM; i++) { test_rounds++; uwb_audio_buf[idx][i].slot_idx = i; uwb_audio_buf[idx][i].len = UWB_AUDIO_MTU_MAX; uwb_audio_buf[idx][i].data[0] = test_rounds & 0xFF; uwb_audio_buf[idx][i].data[1] = (test_rounds >> 8) & 0xFF; uwb_audio_buf[idx][i].data[2] = (test_rounds >> 16) & 0xFF; uwb_audio_buf[idx][i].data[3] = (test_rounds >> 24) & 0xFF; uwb_audio_buf[idx][i].data[4] = idx; uwb_audio_buf[idx][i].data[5] = idx; uwb_audio_buf[idx][i].data[6] = idx; uwb_audio_buf[idx][i].data[7] = idx; uwb_audio_buf[idx][i].ready = 1; } // board_led_off(BOARD_LED_2); int_unlock(lock); } void app_process_handle(uint8_t msg_id, const void *param) { switch (msg_id) { case APP_TEST_TIMER1_MSG: { LOG_INFO(TRACE_MODULE_APP, "TEST TIMER1\r\n"); if (transfer_on_flag) { } } 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 (transfer_on_flag) { uwbapi_session_stop(session_id); transfer_on_flag = 0; } else { // Start transfering uwbapi_session_start(session_id, NULL); transfer_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(); gpio_open(); board_led_init(); board_led_off(BOARD_LED_1); board_led_off(BOARD_LED_2); 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 audio 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); uwb_open(); // // Create ranging task // handlerId = WsfOsSetNextHandler(uwb_audio_handler); uwb_audio_init(handlerId); uwbs_init(); uwb_app_config.ranging_flow_mode = (uint8_t)(RANGING_FLOW_DATA_TRANSFER); 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 ranging session uwbapi_session_init(session_id, SESSION_TYPE_DATA_TRANSFER); // Initialize ranging 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.rframe_config = UWB_RFRAME_TYPE; param.sts_config = STS_STATIC; param.ranging_round_usage = UWB_AUDIO_MODE; param.mac_address_mode = ARRD_SHORT_USE_SHORT; param.controlees_num = RESPONDER_NUM; param.multi_node_mode = param.controlees_num > 1 ? ONE_TO_MANY : UNICAST; param.result_report_config = 0x0F; param.ranging_round_control = 0x3; param.hopping_mode = 0x00; param.stride_length = 0x00; // Load local & peer short address uint16_t local_addr; uint16_t peer_addr; #ifdef CELL_PHONE_EN param.device_role = DEV_ROLE_INITIATOR; local_addr = INITIATOR_ADDR; peer_addr = RESPONDER0_ADDR; #else param.device_role = DEV_ROLE_RESPONDER; local_addr = RESPONDER0_ADDR; peer_addr = INITIATOR_ADDR; #endif if (board_param.flag & (1 << BOARD_LOCAL_SHORT_ADDR)) { local_addr = board_param.local_short_addr; } if (board_param.flag & (1 << BOARD_PEER_SHORT_ADDR)) { peer_addr = board_param.peer_short_addr; } param.src_dev_mac_addr[0] = local_addr & 0xff; param.src_dev_mac_addr[1] = (local_addr >> 8) & 0xff; param.dst_dev_mac_addr[0] = peer_addr & 0xff; param.dst_dev_mac_addr[1] = (peer_addr >> 8) & 0xff; param.device_type = param.device_role == DEV_ROLE_INITIATOR ? DEV_TYPE_CONTROLLER : DEV_TYPE_CONTROLEE; param.responder_slot_idx = 0; param.ranging_interval = UWB_RANGING_INTERVAL; param.slot_duration = UWB_RANGING_SLOT_DURATION; param.slots_per_round = (UWB_AUDIO_TX_SLOT_NUM + 1); // Configure session parameters uwbapi_session_set_app_config(session_id, ¶m); // Start transfering uwbapi_session_start(session_id, NULL); transfer_on_flag = 1; // 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) { }