| | |
| | | |
| | | #ifdef DW1000 |
| | | /* Default communication configuration. */ |
| | | static struct mk_uwb_configure config = {//yuan |
| | | struct mk_uwb_configure config = {//yuan |
| | | .phy_work_mode = (uint8_t)(PHY_TX | PHY_RX), |
| | | .phy_cfg.ch_num = UWB_CH_NUM, /* Channel number. */ |
| | | .phy_cfg.code_index = 9, /* TRX preamble code */ |
| | |
| | | .phy_cfg.sts_pkt_cfg = STS_PKT_CFG_0, /* SP0 Frame */ |
| | | .phy_cfg.sts_segnum = STS_SEGNUM_BPRF_1, /* Number of STS segments in the frame */ |
| | | .phy_cfg.sts_seglen = STS_SEGLEN_BPRF_64, /* Number of symbols in an STS segment */ |
| | | .phy_cfg.rx_ant_id = UWB_RX_ANT_3, /* UWB RX antenna port */ |
| | | .phy_cfg.rx_main_ant = UWB_RX_ANT_3, /* UWB RX main antenna port */ |
| | | .phy_cfg.rx_ant_mode = RX_ANT_PORTS_COMBINATION, /* UWB RX antenna mode */ |
| | | .phy_cfg.pulse_shape = 0x2, /* 0x0: CCC pulse, 0x2: FiRa pulse */ |
| | | }; |
| | | #elif defined STS_MODE |
| | | static struct mk_uwb_configure config = { |
| | |
| | | .phy_cfg.sts_pkt_cfg = STS_PKT_CFG_1, /* SP1 Frame */ |
| | | .phy_cfg.sts_segnum = STS_SEGNUM_BPRF_1, /* Number of STS segments in the frame */ |
| | | .phy_cfg.sts_seglen = STS_SEGLEN_BPRF_64, /* Number of symbols in an STS segment */ |
| | | .phy_cfg.rx_ant_id = UWB_RX_ANT_3, /* UWB RX antenna port */ |
| | | .phy_cfg.rx_main_ant = UWB_RX_ANT_3, /* UWB RX main antenna port */ |
| | | .phy_cfg.rx_ant_mode = RX_ANT_PORTS_COMBINATION, /* UWB RX antenna mode */ |
| | | .phy_cfg.pulse_shape = 0x2, /* 0x0: CCC pulse, 0x2: FiRa pulse */ |
| | | }; |
| | | /* Use the default key and IV specified in the IEEE 802.15.4z attachment */ |
| | | static struct UWB_STS_KEY_CONFIG_T sts_iv_key = { |
| | | .sts_vcounter = 0x1F9A3DE4, |
| | | .sts_vupper0 = 0xD37EC3CA, |
| | | .sts_vupper1 = 0xC44FA8FB, |
| | | .sts_vupper2 = 0x362EEB34, |
| | | .sts_key0 = 0x14EB220F, |
| | | .sts_key1 = 0xF86050A8, |
| | | .sts_key2 = 0xD1D336AA, |
| | | .sts_key3 = 0x14148674, |
| | | }; |
| | | #elif defined STS_MODE_SQUARE |
| | | struct mk_uwb_configure config = { |
| | | .phy_work_mode = (uint8_t)(PHY_TX | PHY_RX), |
| | | .phy_cfg.ch_num = 9, /* Channel number. */ |
| | | .phy_cfg.code_index = 9, /* TRX preamble code */ |
| | | .phy_cfg.mean_prf = MEAN_PRF_64M, /* Mean prf 64/128/256M */ |
| | | .phy_cfg.data_bit_rate = DATA_BR_6M8, /* Data rate 6.8M */ |
| | | .phy_cfg.sync_sym = PREAM_LEN_128, /* Preamble duration, length of preamble 128 */ |
| | | .phy_cfg.sfd_sym = BPRF_NSFD2_8, /* Identifier for SFD sequence */ |
| | | .phy_cfg.ranging_bit = 1, /* ranging bit set 1 */ |
| | | .phy_cfg.trx_mode = TRX_MODE_15_4Z_BPRF, /* IEEE802.15.4z - BPRF mode */ |
| | | .phy_cfg.sts_pkt_cfg = STS_PKT_CFG_1, /* SP1 Frame */ |
| | | .phy_cfg.sts_segnum = STS_SEGNUM_BPRF_1, /* Number of STS segments in the frame */ |
| | | .phy_cfg.sts_seglen = STS_SEGLEN_BPRF_64, /* Number of symbols in an STS segment */ |
| | | .phy_cfg.rx_main_ant = UWB_RX_ANT_3, /* UWB RX main antenna port */ |
| | | .phy_cfg.rx_ant_mode = RX_ANT_PORTS_COMBINATION, /* UWB RX antenna mode */ |
| | | .phy_cfg.pulse_shape = 0x2, /* 0x0: CCC pulse, 0x2: FiRa pulse */ |
| | | }; |
| | | /* Use the default key and IV specified in the IEEE 802.15.4z attachment */ |
| | | static struct UWB_STS_KEY_CONFIG_T sts_iv_key = { |
| | |
| | | .phy_cfg.sts_pkt_cfg = STS_PKT_CFG_0, /* SP0 Frame */ |
| | | .phy_cfg.sts_segnum = STS_SEGNUM_BPRF_1, /* Number of STS segments in the frame */ |
| | | .phy_cfg.sts_seglen = STS_SEGLEN_BPRF_64, /* Number of symbols in an STS segment */ |
| | | .phy_cfg.rx_ant_id = UWB_RX_ANT_3, /* UWB RX antenna port */ |
| | | .phy_cfg.rx_main_ant = UWB_RX_ANT_3, /* UWB RX main antenna port */ |
| | | .phy_cfg.rx_ant_mode = RX_ANT_PORTS_COMBINATION, /* UWB RX antenna mode */ |
| | | .phy_cfg.pulse_shape = 0x2, /* 0x0: CCC pulse, 0x2: FiRa pulse */ |
| | | }; |
| | | #endif |
| | | ///* Default communication configuration. */ |
| | |
| | | // .phy_cfg.sts_pkt_cfg = STS_PKT_CFG_0, /* SP0 Frame */ |
| | | // .phy_cfg.sts_segnum = STS_SEGNUM_BPRF_1, /* Number of STS segments in the frame */ |
| | | // .phy_cfg.sts_seglen = STS_SEGLEN_BPRF_64, /* Number of symbols in an STS segment */ |
| | | // .phy_cfg.rx_ant_id = UWB_RX_ANT_3, /* UWB RX antenna port */ |
| | | // .phy_cfg.rx_main_ant = UWB_RX_ANT_3, /* UWB RX main antenna port */ |
| | | // .phy_cfg.rx_ant_mode = RX_ANT_PORTS_COMBINATION, /* UWB RX antenna mode */ |
| | | // .phy_cfg.pulse_shape = 0x2, /* 0x0: CCC pulse, 0x2: FiRa pulse */ |
| | | //}; |
| | | /* Buffer to store received frame */ |
| | | static uint8_t rx_buf[150]; |
| | |
| | | int64_t tx_timestamp = ranging_tx_time(timestamp); |
| | | |
| | | // correct antenna delay (TX using the same antenna as RX) |
| | | tx_timestamp += ranging_ant_delays_get(config.phy_cfg.rx_ant_id) / 2; |
| | | tx_timestamp += ranging_ant_delays_get(config.phy_cfg.rx_main_ant) / 2; |
| | | |
| | | return tx_timestamp; |
| | | } |
| | |
| | | int64_t rx_timestamp = ranging_rx_time(ind); |
| | | |
| | | // correct antenna delay |
| | | rx_timestamp -= ranging_ant_delays_get(config.phy_cfg.rx_ant_id) / 2; |
| | | rx_timestamp -= ranging_ant_delays_get(config.phy_cfg.rx_main_ant) / 2; |
| | | |
| | | return rx_timestamp; |
| | | } |
| | | uint32_t temp_rx_first_stamp,temp_rx_second_stamp; |
| | | uint16_t temp_rx_first_id,temp_rx_second_id; |
| | | uint8_t mac_error; |
| | | /* RX done process handler. */ |
| | | static void rx_int_callback(struct MAC_HW_REPORT_T *rx_report) |
| | | { |
| | | // Power off radio |
| | | power_off_radio(); |
| | | sts_lsp_store_stop(); |
| | | //tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US); |
| | | rx_state = rx_report->err_code; |
| | | |
| | | /** UWB RX success */ |
| | | if (rx_state == UWB_RX_OK) |
| | | { |
| | | /* Received data does not contain FCS */ |
| | | if(sts_valid_check()) |
| | | { |
| | | //mac_error=0; |
| | | /* Received data does not contain FCS */ |
| | | rx_length = rx_report->pkt_len; |
| | | memcpy(rx_buf, rx_report->pkt_data, rx_length); |
| | | /* Calculate rx timestamp */ |
| | | resp_rx_ts_i64 = ranging_rx_time_correct(rx_report); |
| | | // if(temp_rx_first_stamp==0) |
| | | // { |
| | | // temp_rx_first_stamp=(uint32_t)resp_rx_ts_i64; |
| | | // memcpy(&temp_rx_first_id,&rx_buf[1],2); |
| | | // }else{ |
| | | // memcpy(&temp_rx_second_id,&rx_buf[1],2); |
| | | // temp_rx_second_stamp=(uint32_t)resp_rx_ts_i64; |
| | | // } |
| | | |
| | | temp_count1=phy_timer_count_get(); |
| | | //获取发射端时钟偏差 |
| | | resp_rx_num++; |
| | |
| | | //calib_xtal38m4_load_cap_auto_tune(ppm);//利用电容调整晶振适配频偏应在完整的一包之后调整,需要关闭XTAL_AUTO_TUNE_EN 宏定义避免收包中途校准导致测距错误 |
| | | //LOG_INFO(TRACE_MODULE_APP, "poll_tx_num is %d,resp_rx_num is %d,distance is %lf\r\n",poll_tx_num,resp_rx_num,distance); |
| | | receive_flag=1; |
| | | |
| | | }else{ |
| | | //mac_error=1; |
| | | rx_state |= UWB_STS_ERR; |
| | | } |
| | | } |
| | | else |
| | | { |
| | |
| | | rx_length = 0; |
| | | receive_flag=2; |
| | | temp_count2=phy_timer_count_get(); |
| | | |
| | | // mac_error=1; |
| | | } |
| | | //tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//立即开启接受并设置0超时 |
| | | } |
| | |
| | | /** UWB TX success */ |
| | | if (tx_report->err_code == UWB_TX_OK) |
| | | { |
| | | //mac_error=0; |
| | | poll_tx_num++; |
| | | temp_count4=phy_timer_count_get();//测试 |
| | | if(temp_flag){ |
| | |
| | | } |
| | | count1=temp_count1; |
| | | temp_flag=1; |
| | | } |
| | | }else{ |
| | | mac_error=1; |
| | | } |
| | | } |
| | | |
| | | |
| | |
| | | #if (defined STS_MODE) || (defined MK_MODE) |
| | | ranging_lib_init(); |
| | | #endif |
| | | ranging_frame_type_set(config.phy_cfg.sts_pkt_cfg); |
| | | //ranging_frame_type_set(config.phy_cfg.sts_pkt_cfg); |
| | | ranging_lib_init(config.phy_cfg.sts_pkt_cfg, CE_AUX_CH_PWR_NLOS_FOM); |
| | | } |
| | | void Anchor_uwb_aoa_square_init(void) |
| | | { uwb_open(); |
| | | // Set calibration parameters |
| | | uwb_calibration_params_set(config.phy_cfg.ch_num); |
| | | |
| | | // 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); |
| | | |
| | | // uwb configure |
| | | uwb_configure(config.phy_work_mode, board_param.tx_power_fcc[CALIB_CH(config.phy_cfg.ch_num)], &config.phy_cfg); |
| | | |
| | | ranging_lib_init(config.phy_cfg.sts_pkt_cfg, CE_AUX_CH_PWR_NLOS_FOM); |
| | | |
| | | // Register rx interrupt callback function |
| | | mac_register_process_handler(tx_int_callback, rx_int_callback); |
| | | |
| | | // Reduce inter frame spacing (IFS) for non encrypted packets |
| | | mac_ifs_set(0, 0); |
| | | |
| | | sts_param_config(config.phy_cfg.sts_pkt_cfg, STS_AUX_ANT_IQ_RSSI, STS_BUF_NUM, STS_BUF_SIZE); |
| | | |
| | | // Set STS key and IV |
| | | phy_sts_key_configure(&sts_iv_key); |
| | | |
| | | phy_rx_sts_switch_mode_set(config.phy_cfg.sts_pkt_cfg, STS_NEVER_SWITCH, 0, 0); |
| | | |
| | | } |
| | | int sleep_time_step=SLEEP_COUNT; |
| | |
| | | temp_tag_num=0;//临时数量为0 |
| | | poll_tx_en_start_u32 = phy_timer_count_get()+US_TO_PHY_TIMER_COUNT(POLL_DELAY);//发送必须要延时发送才可以用于测距否则立即发送会获取时间戳不对,需要计算程序运行时间,避免设置过去时间 |
| | | tempflag=uwb_tx(uwb_sendbuffer,13+4*tag_num_tosend,1,poll_tx_en_start_u32);//立即发送 |
| | | |
| | | //tempflag=uwb_tx(uwb_sendbuffer,sizeof(uwb_sendbuffer),1,poll_tx_en_start_u32);//立即发送 |
| | | #ifdef BOXING |
| | | gpio_pin_set(IO_PIN_5);//测试 |
| | | #endif |
| | |
| | | poll_tx_ts_i64 = ranging_tx_time_correct(poll_tx_en_start_u32 + phy_shr_duration());//修正时间戳 |
| | | temp_count1=phy_timer_count_get(); |
| | | while(mac_is_busy());//等待发送完成 |
| | | // if(!mac_error) |
| | | // phy_update_sts_iv_counter(0x01, 0x00);//新增sts发送成功后开启 |
| | | // else{ |
| | | // mac_error=0; |
| | | // return 0;//发送失败 |
| | | // } |
| | | #ifdef BOXING |
| | | gpio_pin_clr(IO_PIN_5);//测试 |
| | | gpio_pin_set(IO_PIN_5);//测试 |
| | |
| | | if(receive_flag==1)//成功接收数据 |
| | | { |
| | | receive_flag=0; |
| | | //phy_update_sts_iv_counter(0x01, 0x00);//新增sts发送成功后开启 |
| | | tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//立即开启接受并设置0超时 |
| | | //接收成功则判断是否为同一组 |
| | | if(rx_buf[MESSAGE_TYPE_IDX] == MBX_POLL)//收到是其他的基站的POLL包 |
| | |
| | | gpio_pin_clr(IO_PIN_5);//测试 |
| | | gpio_pin_set(IO_PIN_5);//测试 |
| | | #endif |
| | | //phy_update_sts_iv_counter(0x00, sts_iv_key.sts_vcounter);//sts |
| | | } |
| | | // } |
| | | #ifdef BOXING |
| | |
| | | |
| | | return (crc&0xffffu); |
| | | } |
| | | void delay_ms(uint32_t nTimer) |
| | | { |
| | | uint32_t i=1000*nTimer; |
| | | delay_us(i); |
| | | } |
| | | //void delay_ms(uint32_t nTimer) |
| | | //{ |
| | | // uint32_t i=1000*nTimer; |
| | | // delay_us(i); |
| | | //} |
| | | uint8_t send_lora_data[250]; |
| | | uint16_t muqiandeshengjibao; |
| | | static uint16_t current_count1,target_count1,end_count1,start_count1; |