/* * Copyright (c) 2019-2025 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_power.h" #include "mk_uwb.h" #include "mk_calib.h" #include "board.h" #include "pal_sys.h" #include "wsf_os.h" #include "mk_timer_list.h" #include "wsf_buf.h" #include "app.h" #include "ul_tdoa.h" #include "uwb_api.h" #include "lib_ranging.h" #include "libc_rom.h" #include "mk_adc.h" #include "UART.h" #include "Usart.h" #include "global_param.h" #include #define TEST_UART_POLL_MODE 0 #define TEST_UART_INTERUPT_MODE 1 #define TEST_UART_DMA_MODE 2 #define TEST_UART_MODE TEST_UART_DMA_MODE struct ADC_CFG_T usr_adc_cfg = { .mode = ADC_MODE_CONTINUE, /* Selected single conversion mode */ .clk_sel = ADC_CLK_HIGH, /* Selected 62.4M high speed clock */ .vref_sel = ADC_SEL_VREF_INT, /* Using internal reference voltage (1.2V)*/ .rate = 500000, /* ADC works at high frequency system clock, the maximum sampling rate is 2M */ .channel_p = ADC_IN_EXTPIN0, /* ADC positive channel --> GPIO0 */ .channel_n = ADC_IN_VREF, /* ADC negative channel --> Vref */ .int_en = false, .dma_en = false, /* DMA support only in continue mode */ .acc_num = 0, .high_pulse_time = 4, .settle_time = 1, }; typedef enum { UN_BIND=0, LINK_SUCCESS, SEARCH_DEV, } Operation_step; Operation_step UWB_work_state; struct UART_CFG_T test_uart_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_115200, #if (TEST_UART_MODE == TEST_UART_POLL_MODE) .dma_en = false, .int_rx = false, .int_tx = false, #elif (TEST_UART_MODE == TEST_UART_INTERUPT_MODE) .dma_en = false, .int_rx = true, .int_tx = true, #elif (TEST_UART_MODE == TEST_UART_DMA_MODE) .dma_en = true, .int_rx = false, .int_tx = false, #endif }; void uart0_receive_callback(void *dev, uint32_t err_code) { uart_receive(UART_ID0,m_EUART0_DMA_RXBuf,EUART0_RX_BUF_SIZE,uart0_receive_callback); } void uart1_receive_callback(void *dev, uint32_t err_code) { uart_receive(UART_ID1,m_EUART1_DMA_RXBuf,EUART1_RX_BUF_SIZE,uart1_receive_callback); } uint32_t dev_id; uint8_t group_id; uint16_t tag_frequency; uint16_t disoffset; uint8_t bind_flag; uint16_t warning_distance,prewarning_distance; int16_t fVoltage_mv,first_search_flag; uint8_t bat_percent,g_start_send_flag=1; uint8_t link_success_flag,motor_count; uint8_t flag_sleeptimer,flag_secondtask,secondtask_count,log_4g_enable_flag; uint16_t gps_wait_count,gps_wait_count2; extern uint16_t ip0,ip1,ip2,ip3,port; uint8_t TCP_reconnect_timer,flag_TCP_reconnectting = 1,flag_alam_state = 0,userkey_state; uint8_t gps_enable_flag,motor_open_air_flag,moter_open_uwb_flag,flag_fenli_alarm = 0,fenli_alarm_count,delaysleep_count,enbale_blink_flag; uint8_t motor_power_state=1,uwb_state,air780_power_state,gps_success_state,red_charge_state,green_charge_state,air780_success_state,first_motor_in_flag; void Program_Init(void) { Usart1ParseDataCallback = Usart1ParseDataHandler;//Ðè¸ÄΪĬÈÏΪgps´¦Àí£¬Usart1ParseDataHandlerΪÉý¼¶´¦Àíµ±µ÷ÊÔʱºò¸ÄΪ Usart0ParseDataCallback=Usart0ParseDataHandler; parameter_init_anchor();//g_com_map±í³õʼ»¯½ÇɫĬÈÏΪ»ùÕ¾ dev_id=g_com_map[DEV_ID];//ÕâÀﲻ̫¶Ô group_id=g_com_map[GROUP_ID];//×éID memcpy(&disoffset,&g_com_map[DIST_OFFSET],2); g_com_map[MODBUS_MODE] = 0; log_4g_enable_flag=g_com_map[LOG_4G_ENABLE]; ip0 = (g_com_map[TCP_IP_0]>>12&0xf)*1000+(g_com_map[TCP_IP_0]>>8&0xf)*100+(g_com_map[TCP_IP_0]>>4&0xf)*10+(g_com_map[TCP_IP_0]&0xf); ip1 = (g_com_map[TCP_IP_1]>>12&0xf)*1000+(g_com_map[TCP_IP_1]>>8&0xf)*100+(g_com_map[TCP_IP_1]>>4&0xf)*10+(g_com_map[TCP_IP_1]&0xf); ip2 = (g_com_map[TCP_IP_2]>>12&0xf)*1000+(g_com_map[TCP_IP_2]>>8&0xf)*100+(g_com_map[TCP_IP_2]>>4&0xf)*10+(g_com_map[TCP_IP_2]&0xf); ip3 = (g_com_map[TCP_IP_3]>>12&0xf)*1000+(g_com_map[TCP_IP_3]>>8&0xf)*100+(g_com_map[TCP_IP_3]>>4&0xf)*10+(g_com_map[TCP_IP_3]&0xf); port = g_com_map[TCP_PORT]; g_com_map[VERSION] = (1<<8)|0; gpio_pin_set(CHANGE_UART_PIN);//£»À­¸ß´òÓ¡Êä³ö LOG_INFO(TRACE_MODULE_APP,"É豸ID: %x .\r\n",dev_id); LOG_INFO(TRACE_MODULE_APP,"¹Ì¼þ°æ±¾:MK_ÎÞ¸ÐÕ¢»ú V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff); LOG_INFO(TRACE_MODULE_APP,"·þÎñÆ÷µØÖ·: %d.%d.%d.%d:%d.\r\n",ip0,ip1,ip2,ip3,port); while(uart_tx_in_progress(UART_ID0)); gpio_pin_clr(CHANGE_UART_PIN);//£»À­µÍÊäÈë } void boot_deinit(void) { //½«bootÖд®¿Ú·µ»ØÆÕͨgpio // UART0 TX/RX io_pin_mux_set(IO_PIN_5, IO_FUNC0); io_pin_mux_set(IO_PIN_6, IO_FUNC0); // UART1 RX/TX io_pin_mux_set(IO_PIN_10, IO_FUNC0); io_pin_mux_set(IO_PIN_9, IO_FUNC0); uart_close(UART_ID1);//½â°óÔ­À´´®¿Ú1 uart_close(UART_ID0);//½â°óÔ­À´´®¿Ú0 } void IdleTask(void) { //if(read_5v_input_pca()) // { // if(state5v==0) // { // state5v=1; // state5V_prase_flag=state5v; // gps_prase_flag=0;//½â³ýgps½âÎö // uart1_change_from_gps_to_debug();//²âÊÔ // PCA9555_Set_One_Value_Output(MCU_A,1);//Êä³ö¸ßµçƽÇл»Îª5VÊäÈë // } // }else { // if(state5v==1) // { // g_com_map[MODBUS_MODE] = 0; // state5v=0; // state5V_prase_flag=state5v; // gps_prase_flag=1;//»Ö¸´gps½âÎö // uart1_change_from_debug_to_gps();//²âÊÔ // PCA9555_Set_One_Value_Output(MCU_A,0);//Êä³öµÍµçƽÇл»ÎªGPS // } // } UART0_CheckReceive(); UART_CheckReceive(); } #define UT_TAG_ID (0xAAA1) #define UT_SYNC_ANCHOR_ID (0xAAA2) #define UT_ANCHOR_ID0 (0xAAA3) #define UT_ANCHOR_ID1 (0xAAA4) #define UT_ANCHOR_ID2 (0xAAA5) #define UT_TX_DEV_NUM_MAX 3 struct UL_TDOA_DEV_INF_T { uint16_t dev_id; uint64_t tx_timestamp; uint64_t rx_timestamp; }; static struct UL_TDOA_DEV_INF_T ul_tdoa_dev_list[UT_TX_DEV_NUM_MAX] = {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 app_ul_tdoa_report_callback(void *report) { struct UL_TDOA_MEASUREMENT_T *rpt = (struct UL_TDOA_MEASUREMENT_T *)report; uint16_t peer = READ_SHORT(rpt->dev_id); uint16_t local = uwbs_local_short_addr_get(); LOG_INFO(TRACE_MODULE_APP | TRACE_NO_OPTION, "\r\n"); LOG_INFO(TRACE_MODULE_APP, "Frame type %u, Frame number %u\r\n", rpt->frame_type, rpt->frame_number); LOG_INFO(TRACE_MODULE_APP, "Peer %X, Local %X, TX timestamp 0x%02x%02x%02x%02x%02x%02x%02x%02x, RX timestamp 0x%02x%02x%02x%02x%02x%02x%02x%02x\r\n", peer, local, rpt->tx_timestamp.ts_u8[7], rpt->tx_timestamp.ts_u8[6], rpt->tx_timestamp.ts_u8[5], rpt->tx_timestamp.ts_u8[4], rpt->tx_timestamp.ts_u8[3], rpt->tx_timestamp.ts_u8[2], rpt->tx_timestamp.ts_u8[1], rpt->tx_timestamp.ts_u8[0], rpt->rx_timestamp.ts_u8[7], rpt->rx_timestamp.ts_u8[6], rpt->rx_timestamp.ts_u8[5], rpt->rx_timestamp.ts_u8[4], rpt->rx_timestamp.ts_u8[3], rpt->rx_timestamp.ts_u8[2], rpt->rx_timestamp.ts_u8[1], rpt->rx_timestamp.ts_u8[0]); LOG_INFO(TRACE_MODULE_APP, "Clock SYNC offset %lld\r\n", (rpt->rx_timestamp.ts_u64 - rpt->tx_timestamp.ts_u64)); // need to be corrected by ToF uint8_t dev_found = 0; for (int i = 0; i < UT_TX_DEV_NUM_MAX; i++) { if (peer == ul_tdoa_dev_list[i].dev_id) { LOG_INFO(TRACE_MODULE_APP, "Clock offset %d (15.65ps) in 1s\r\n", (int32_t)((rpt->rx_timestamp.ts_u64 - ul_tdoa_dev_list[i].rx_timestamp) - (rpt->tx_timestamp.ts_u64 - ul_tdoa_dev_list[i].tx_timestamp))); ul_tdoa_dev_list[i].tx_timestamp = rpt->tx_timestamp.ts_u64; ul_tdoa_dev_list[i].rx_timestamp = rpt->rx_timestamp.ts_u64; dev_found = 1; break; } } // add device into the list if (dev_found == 0) { for (int i = 0; i < UT_TX_DEV_NUM_MAX; i++) { if (0 == ul_tdoa_dev_list[i].dev_id) { ul_tdoa_dev_list[i].dev_id = peer; ul_tdoa_dev_list[i].tx_timestamp = rpt->tx_timestamp.ts_u64; ul_tdoa_dev_list[i].rx_timestamp = rpt->rx_timestamp.ts_u64; break; } } } } static void board_init(void) { // Clock configuration board_clock_run(); boot_deinit(); // Pin configuration board_pins_config(); // Trace configuration board_debug_console_open_baud(TRACE_PORT_UART0,BAUD_115200); // Reset reason reset_cause_get(); reset_cause_clear(); // Load calibration parameters board_calibration_params_load(); // 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(); Program_Init(); uart_open(UART_ID1, &test_uart_cfg); uart_receive(UART_ID1,m_EUART1_DMA_RXBuf,EUART1_RX_BUF_SIZE,uart1_receive_callback); uart_receive(UART_ID0,m_EUART0_DMA_RXBuf,EUART0_RX_BUF_SIZE,uart0_receive_callback); board_configure(); } int main(void) { // Initialize MCU system board_init(); // Disable watchdog timer wdt_close(WDT_ID0); LOG_INFO(TRACE_MODULE_APP, "UL-TDoA example\r\n"); // Platform init for WSF PalSysInit(); // Initialize os WsfOsInit(); mk_timer_list_init(); // // 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 Uplink TDoA task // handlerId = WsfOsSetNextHandler(ul_tdoa_handler); ul_tdoa_init(handlerId); uwb_open(); // set advanced parameters struct PHY_ADV_CONFIG_T adv_config = { .thres_fap_detect = 40, .nth_scale_factor = 1, .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); uwbs_init(); uwb_app_config.ranging_flow_mode = (uint8_t)(RANGING_FLOW_CUSTOM); 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_main_ant = (uint8_t)(RX_MAIN_ANT_PORT); uwb_app_config.ppdu_params->rx_ant_mode = (uint8_t)(RX_ANT_PORTS_COMBINATION); // Initialize OWR session uint32_t session_id = 0x0001; uwbapi_session_init(session_id, SESSION_TYPE_RANGING); // Initialize OWR 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.multi_node_mode = ONE_TO_MANY; param.ranging_round_usage = OWR_UL_TDOA; param.mac_address_mode = ADDR_SHORT_USE_SHORT; param.result_report_config = 0x0F; param.ranging_round_control = 0x3; param.ul_tdoa_tx_interval = UWB_UL_TDOA_INTERVAL; param.ul_tdoa_random_window = UWB_UL_TDOA_RAND_WINDOW; param.ul_tdoa_ntf_report_config[0] = 0; param.ul_tdoa_ntf_report_config[1] = 1; param.ul_tdoa_ntf_report_config[2] = 0; param.ul_tdoa_device_id[0] = 0x01; // Select role by IO_02 if (1) { #if 1 param.device_role = DEV_ROLE_UT_SYNC_ANCHOR; param.ul_tdoa_device_id[1] = UT_SYNC_ANCHOR_ID & 0xff; param.ul_tdoa_device_id[2] = (UT_SYNC_ANCHOR_ID >> 8) & 0xff; #else param.device_role = DEV_ROLE_UT_TAG; param.ul_tdoa_device_id[1] = UT_TAG_ID & 0xff; param.ul_tdoa_device_id[2] = (UT_TAG_ID >> 8) & 0xff; #endif } else { param.device_role = DEV_ROLE_UT_ANCHOR; param.ul_tdoa_device_id[1] = UT_ANCHOR_ID0 & 0xff; param.ul_tdoa_device_id[2] = (UT_ANCHOR_ID0 >> 8) & 0xff; } // local address param.src_dev_mac_addr[0] = param.ul_tdoa_device_id[1]; param.src_dev_mac_addr[1] = param.ul_tdoa_device_id[2]; param.ul_tdoa_tx_timestamp = 0x02; // 64-bit TX timestamp // Configure ranging parameters uwbapi_session_set_app_config(session_id, ¶m); // Start TDoA uwbapi_session_start(session_id, app_ul_tdoa_report_callback); // 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 while (1) { wsfOsDispatcher(); power_manage(); IdleTask(); } } void app_restore_from_power_down(void) { }