From 448e16db1aaadf2ae321af8a7955aba2b610b12e Mon Sep 17 00:00:00 2001 From: chen <15335560115@163.com> Date: 星期三, 16 七月 2025 18:25:55 +0800 Subject: [PATCH] 增加防撞基站修改485和读取指令,修改showbuffer距离修改为g_com表设置距离 --- keil/uwb_tag.c | 836 ++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 672 insertions(+), 164 deletions(-) diff --git a/keil/uwb_tag.c b/keil/uwb_tag.c index f1b4181..f4092cf 100644 --- a/keil/uwb_tag.c +++ b/keil/uwb_tag.c @@ -2,22 +2,26 @@ #include "mk_uwb.h" #include "mk_phy.h" #include "mk_misc.h" +#include "mk_wdt.h" #include "mk_power.h" #include "mk_sleep_timer.h" #include "lib_ranging.h" #include "dw_tag.h" #include "board.h" #include "mk_calib.h" +#include "stdlib.h" +#include "mk_flash.h" +#include "global_param.h" +//#if defined(MK_SS_TWR_DW_INIT) extern int TagRange(void); -extern Operation_step UWB_work_state; - -#define CALCULATE_TIMECOUNT +void Tag_uwb_init(void); +void AnchorLiatUpdate(void); /* Ranging period: 1s */ -#define RANGING_PERIOD_MS (1000) +#define RANGING_PERIOD_MS (10000) /* This is the delay from the end of the poll frame transmission to the enable of the receiver */ -#define POLL_TX_TO_RESP_RX_DLY_US 750U //yuan700 +#define POLL_TX_TO_RESP_RX_DLY_US 550U //yuan700 #define RESP_RX_TO_FINAL_TX_DLY_US 550U @@ -25,18 +29,18 @@ #define POLL_DELAY 100U //yuan100U /* Receive response timeout */ -#define RESP_RX_TIMEOUT_US 2000U //Yuan500 10mssuccess 300jixian - +#define RESP_RX_TIMEOUT_US_DEFAULT 600U //Yuan500 10mssuccess 300jixian +#define RESP_RX_TIMEOUT_US 2000000 /* Field index in frame */ #define MSG_SEQ_NUM_IDX 2 #define FINAL_MSG_POLL_TX_TS_IDX 10 #define FINAL_MSG_RESP_RX_TS_IDX 14 #define FINAL_MSG_FINAL_TX_TS_IDX 18 -#define DELAY_DEFAULT 1000 -#define DELAY_BETWEEN_TWO_FRAME_UUS 600 //yuan1400 +#define DELAY_DEFAULT 50000 +#define DELAY_BETWEEN_TWO_FRAME_UUS 500 //yuan1400 #define HALF_SECOND_TIME 62400000 -static uint16_t CmpTagInList(uint16_t tagid); +uint16_t CmpTagInList_Anchor(uint16_t tagid); uint16_t AddNewTagIntoList(uint16_t tagid); void AnchorListUpdate(void); void Rank_ANchor_list_by_dis(void); @@ -47,10 +51,12 @@ }; extern uint32_t dev_id; extern uint8_t group_id; + +#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 = 5, /* Channel number. */ + .phy_cfg.ch_num = UWB_CH_NUM, /* 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 */ @@ -61,10 +67,88 @@ .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_main_ant = UWB_RX_ANT_3, /* UWB RX main 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_work_mode = (uint8_t)(PHY_TX | PHY_RX), + .phy_cfg.ch_num = 5, /* 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 = { + .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 = 5, /* 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 = { + .sts_vcounter = 0x1F9A3DE4, + .sts_vupper0 = 0xD37EC3CA, + .sts_vupper1 = 0xC44FA8FB, + .sts_vupper2 = 0x362EEB34, + .sts_key0 = 0x14EB220F, + .sts_key1 = 0xF86050A8, + .sts_key2 = 0xD1D336AA, + .sts_key3 = 0x14148674, +}; +#else //MKmode +/* Default communication configuration. */ +static 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, /* TX preamble code. */ + .phy_cfg.mean_prf = MEAN_PRF_64M, /* Data rate 6.8M */ + .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. */ + .phy_cfg.trx_mode = TRX_MODE_15_4Z_BPRF, /* IEEE802.15.4z - BPRF mode */ + .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_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. */ //static struct mk_uwb_configure config = { // .phy_work_mode = (uint8_t)(PHY_TX | PHY_RX), @@ -79,7 +163,9 @@ // .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]; @@ -125,27 +211,27 @@ int32_t test2; static uint8_t receive_flag=0; static uint8_t rec_tag_index,tag_num_tosend; - -static uint32_t start_receive_count,end_receive_count,poll_timeout,current_count,temp_resp; uint16_t taglist_total_num,taglist_current_index; //当前列表总数量 + +uint32_t start_receive_count_anchor,end_receive_count_anchor,poll_timeout_Anchor,current_count_anchor,temp_resp_anchor; uint8_t taglist_keeptime[MAX_TAG_LIST_NUM]; //每个标签存活时间 -uint16_t taglist_id[MAX_TAG_LIST_NUM],taglist_dist[MAX_TAG_LIST_NUM]; //标签距离和标签的ID +uint16_t taglist_id[MAX_TAG_LIST_NUM],taglist_dist[MAX_TAG_LIST_NUM],temp_index_anchor; //标签距离和标签的ID int32_t mainbase_dist,nearbase_distlist[MAX_NEARBASE_NUM],true_nearbase_distlist[MAX_NEARBASE_NUM],true_exsistbase_list[MAX_NEARBASE_NUM],ancsync_time,nextpoll_delaytime,offsettimeus; uint8_t anclost_times=0 , mainbase_lost_count=0,exsistbase_list[MAX_NEARBASE_NUM],get_newbase=0,nearbase_num,last_nearbase_num,next_nearbase_num; uint16_t nearbaseid_list[MAX_NEARBASE_NUM],mainbase_id,true_nearbase_idlist[MAX_NEARBASE_NUM],rec_nearbaseid,rec_nearbasepos; uint16_t u16_nearbase_distlist[MAX_NEARBASE_NUM]; -uint32_t temp_tag_count1=0; -uint32_t temp_tag_count2=0; -uint32_t temp_tag_count3=0; +uint32_t temp_count1_anchor=0; +uint32_t temp_count2_anchor=0; +uint32_t temp_count3_anchor=0; uint32_t temp_count4=0; uint32_t tempflag=0; int32_t freq_offset,freq_offset_filter; int temp_flag,poll_tx_num,resp_rx_num; extern double distance; +extern uint8_t bat_percent; uint32_t count1,count2; -extern double distance_tag; - +//extern uint32_t reboot_num; enum SIMPLE_FSM_T { SIMPLE_IDLE = 0, @@ -155,19 +241,27 @@ }; void uwb_poll_buffer_construct(void) { - for(uint8_t i=0;i<nearbase_num;i++) //标签距离32位转成16位 -{ - u16_nearbase_distlist[i] = nearbase_distlist[i]; -} +// for(uint8_t i=0;i<nearbase_num;i++) //标签距离32位转成16位 +//{ +// u16_nearbase_distlist[i] = nearbase_distlist[i]; +//} + + tag_num_tosend = taglist_total_num-taglist_current_index; + if(tag_num_tosend>SEND_GROUP_MAX_NUM) //这次发的数量 + { + tag_num_tosend = SEND_GROUP_MAX_NUM; + } + memcpy(&uwb_sendbuffer[7],&g_com_map[VERSION],2); uwb_sendbuffer[GROUP_ID_IDX] = group_id; memcpy(&uwb_sendbuffer[TAG_ID_IDX],&dev_id,2); - //uwb_sendbuffer[BATTARY_IDX] = bat_percent; + uwb_sendbuffer[BATTARY_IDX] = bat_percent; uwb_sendbuffer[SEQUENCE_IDX] = frame_seq_nb++; - uwb_sendbuffer[NEARBASENUM_INDEX] = nearbase_num; - memcpy(&uwb_sendbuffer[NEARBASEID_INDEX],&nearbaseid_list,nearbase_num*2);//基站id - memcpy(&uwb_sendbuffer[NEARBASEID_INDEX+nearbase_num*2],&u16_nearbase_distlist,nearbase_num*2); + uwb_sendbuffer[NEARBASENUM_INDEX] = tag_num_tosend; + memcpy(&uwb_sendbuffer[NEARBASEID_INDEX],&taglist_id[taglist_current_index],tag_num_tosend*2);//基站id + memcpy(&uwb_sendbuffer[NEARBASEID_INDEX+tag_num_tosend*2],&taglist_dist[taglist_current_index],tag_num_tosend*2); uwb_sendbuffer[MESSAGE_TYPE_IDX] = MBX_POLL; memcpy(&uwb_sendbuffer[ANCHOR_ID_IDX],&mainbase_id,2); + } static enum SIMPLE_FSM_T state = SIMPLE_IDLE; @@ -203,33 +297,44 @@ 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); - temp_tag_count1=phy_timer_count_get(); + + temp_count1_anchor=phy_timer_count_get(); //获取发射端时钟偏差 resp_rx_num++; freq_offset=phy_freq_offset_get(); - freq_offset_filter=average_filter(freq_offset);//获取频偏 -// int32_t ppm = freq_offset_filter / (int32_t)(ch_center_freq_map[UWB_CH_NUM] * 1e-6); -// calib_xtal38m4_load_cap_auto_tune(ppm);//利用电容调整晶振适配频偏应在完整的一包之后调整,需要关闭XTAL_AUTO_TUNE_EN 宏定义避免收包中途校准导致测距错误 + + // freq_offset_filter=average_filter(freq_offset);//获取频偏 + //int32_t ppm = freq_offset_filter / (int32_t)(ch_center_freq_map[UWB_CH_NUM] * 1e-6); + //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 { @@ -242,10 +347,10 @@ rx_state1=rx_report->err_code; rx_length = 0; receive_flag=2; - temp_tag_count2=phy_timer_count_get(); - + temp_count2_anchor=phy_timer_count_get(); + // mac_error=1; } - + //tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//立即开启接受并设置0超时 } /* TX done process handler. */ @@ -257,6 +362,7 @@ /** 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){ @@ -264,9 +370,11 @@ temp_flag=0; return; } - count1=temp_tag_count1; + count1=temp_count1_anchor; temp_flag=1; - } + }else{ + mac_error=1; + } } @@ -275,7 +383,7 @@ //方法是将新注册的标签添加到下次发送的标签列表头部,其他标签依次向后挪动。 uint16_t AddNewTagIntoList(uint16_t tagid) { - uint16_t newindex_cal = taglist_current_index+25;//当前索引+25,为下次索引要发出的值的最大值,如果总数小于这个值,即可放到最后面。 + uint16_t newindex_cal = taglist_current_index+SEND_GROUP_MAX_NUM;//当前索引+25,为下次索引要发出的值的最大值,如果总数小于这个值,即可放到最后面。 if(newindex_cal>=taglist_total_num) //如果当前索引是最后一包,那么就加到总索引的后面,如果不是就加到下一次起始索引位置。 { newindex_cal = taglist_total_num; @@ -291,7 +399,7 @@ taglist_total_num++; taglist_id[newindex_cal] = tagid; taglist_dist[newindex_cal] = 0; - taglist_keeptime[newindex_cal] = TAG_KEEPTIMES; + taglist_keeptime[newindex_cal] = KEEP_TIMES; return newindex_cal; } uint8_t FindNearBasePos(uint16_t baseid)//寻找当前列表中的基站返回索引 @@ -321,48 +429,62 @@ true_nearbase_distlist[j++]=nearbase_distlist[i]; } - nearbase_num = next_nearbase_num;//更新现存基站数 - last_nearbase_num = next_nearbase_num;//更新当前基站数 } + nearbase_num = next_nearbase_num;//更新现存基站数 + last_nearbase_num = next_nearbase_num;//更新当前基站数 } - -void Rank_ANchor_list_by_dis(void)//按照目前的基站距离进行冒泡排序用临时数组方式将最近的放到最前面,最远的放到最后面 +int temp_j; +void AnchorLiatUpdate(void) { -for(int i=0;i<last_nearbase_num-1;i++) - { - for(int j=0;j<last_nearbase_num-1;j++) - { - if(true_nearbase_distlist[j]>true_nearbase_distlist[j+1]) - { - uint32_t temp_dist,temp_id,temp_exsis; - temp_dist=true_nearbase_distlist[j]; - temp_id = true_nearbase_idlist[j]; - temp_exsis=true_exsistbase_list[j]; - true_nearbase_distlist[j]=true_nearbase_distlist[j+1]; - true_nearbase_idlist[j]=true_nearbase_idlist[j+1]; - true_exsistbase_list[j]=true_exsistbase_list[j+1]; - - true_nearbase_distlist[j+1]=temp_dist; - true_nearbase_idlist[j+1]=temp_id; - true_exsistbase_list[j+1]=temp_exsis; - } - } - } - for (int i=0;i<last_nearbase_num;i++) - { - nearbaseid_list[i]=true_nearbase_idlist[i]; - nearbase_distlist[i]=true_nearbase_distlist[i]; - exsistbase_list[i] = true_exsistbase_list[i]; - } + uint16_t i,j=0,temp[TAG_NUM_IN_SYS]; + for(i=0;i<taglist_total_num;i++) + { + if(taglist_keeptime[i]-->0) + { + taglist_id[j]=taglist_id[i]; + taglist_keeptime[j++]=taglist_keeptime[i];//自动覆盖,即从前往后寻找存活时间为0的标签,并把它用后面离得最近的第一个有效存活的标签覆盖,那么最后j的值就是当前有效个数 + } + } + temp_j=j; + taglist_total_num=j; } -static uint16_t CmpTagInList(uint16_t tagid)//找这个新一包的基站在不在现存的taglist中在的话对应位置更新存活时间,不在就增加(确保有效数量小于最大设置数) +//void Rank_ANchor_list_by_dis(void)//按照目前的基站距离进行冒泡排序用临时数组方式将最近的放到最前面,最远的放到最后面 +//{ +//for(int i=0;i<last_nearbase_num-1;i++) +// { +// for(int j=0;j<last_nearbase_num-1;j++) +// { +// if(true_nearbase_distlist[j]>true_nearbase_distlist[j+1]) +// { +// uint32_t temp_dist,temp_id,temp_exsis; +// temp_dist=true_nearbase_distlist[j]; +// temp_id = true_nearbase_idlist[j]; +// temp_exsis=true_exsistbase_list[i]; +// true_nearbase_distlist[j]=true_nearbase_distlist[j+1]; +// true_nearbase_idlist[j]=true_nearbase_idlist[j+1]; +// true_exsistbase_list[j]=true_exsistbase_list[j+1]; +// +// true_nearbase_distlist[j+1]=temp_dist; +// true_nearbase_idlist[j+1]=temp_id; +// true_exsistbase_list[j+1]=temp_exsis; +// } +// } +// } +// for (int i=0;i<last_nearbase_num;i++) +// { +// nearbaseid_list[i]=true_nearbase_idlist[i]; +// nearbase_distlist[i]=true_nearbase_distlist[i]; +// exsistbase_list[i] = true_exsistbase_list[i]; +// } +//} +uint16_t CmpTagInList_Anchor(uint16_t tagid)//找这个新一包的基站在不在现存的taglist中在的话对应位置更新存活时间,不在就增加(确保有效数量小于最大设置数) { uint16_t temp; for(uint8_t i=0;i<taglist_total_num;i++) { if(memcmp(&tagid,&taglist_id[i],2)==0) { - taglist_keeptime[i] = TAG_KEEPTIMES; + taglist_keeptime[i] = KEEP_TIMES; return i; } } @@ -370,16 +492,30 @@ temp = AddNewTagIntoList(tagid); return temp; } - -int TagRange(void) +uint32_t count_index; +int tt1=1; +float freqlost_count; +void Tag_uwb_init(void) { - // The following peripherals will be initialized in the uwb_open function + // The following peripherals will be initialized in the uwb_open function // phy/mac/aes/lsp/phy timers initialized - uwb_open(); - + uwb_open(); +#ifdef STS_MODE + // Set STS key and IV + phy_sts_key_configure(&sts_iv_key); +#endif // Set calibration parameters uwb_calibration_params_set(config.phy_cfg.ch_num); - +#ifdef STS_MODE + + // set advanced parameters + struct PHY_ADV_CONFIG_T adv_config = { + .thres_fap_detect = 40, + .nth_scale_factor = 4, + .ranging_performance_mode = 3, + .skip_weakest_port_en = 0, + }; +#else // set advanced parameters struct PHY_ADV_CONFIG_T adv_config = { .thres_fap_detect = 40, @@ -387,124 +523,496 @@ .ranging_performance_mode = 0, .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); +#if (defined STS_MODE) || (defined MK_MODE) + ranging_lib_init(); +#endif + //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 Tag_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_frame_type_set(config.phy_cfg.sts_pkt_cfg); - ranging_lib_init(config.phy_cfg.sts_pkt_cfg, CE_AUX_CH_PWR_NLOS_FOM); + 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); -//#if LOW_POWER_EN -// // Initialize low power mode -// power_init(); -// // Enable sleep timer -// sleep_timer_open(true, SLEEP_TIMER_MODE_ONESHOT, NULL); -//#endif + // 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; +int rand_temp; +static uint16_t source_id; +uint8_t rec_index,rec_secdelay; +uint16_t rec_value; +uint8_t readgcom_flag; +uint16_t wg_report_id; +uint16_t rec_wenjian_daxiao; +uint16_t wangguan_up_id; +uint8_t shengji_flag,time=5; +uint8_t LoraUp_flag; +extern uint16_t final_bag_num; +uint16_t checksum1; +void Uwb_Update(void) +{ + + checksum1=Checksum_u16(rx_buf,rx_length-2); + if(!memcmp(&checksum1,&rx_buf[rx_length-2],2)) + if(!memcmp(&dev_id,&rx_buf[DEST_ID_IDX],2)) + { + memcpy(&source_id,&rx_buf[SOURCE_ID_IDX],2); +// if(wg_state==WG_Lost) +// { +// wg_state = WG_Connected; + wg_report_id = source_id; +// } +// if(!memcmp(&wg_report_id,&rx_buf[SOURCE_ID_IDX],2)) +// { +// wg_lost_count = 0; +// loraled=GREEN; + switch(rx_buf[PWTAG_RW_FLAG_IDX]) + { + case WGRSP_RWTAG_READ: + readgcom_flag = 1; +// LoraSendComMap(WGRSP_RWTAG_READ); + break; + case WGRSP_RWTAG_WRITE: + rec_index = rx_buf[PWTAG_WRITE_IDX_IDX]; + if(rec_index<=DEV_ID) + {break;} + memcpy(&rec_value,&rx_buf[PWTAG_WRITE_VALUE_IDX],2); + if(g_com_map[rec_index/2]==rec_value) + {break;} + g_com_map[rec_index/2] = rec_value; + save_com_map_to_flash(); + delay_us(60000); + NVIC_SystemReset(); + break; + case WGRSP_RWTAG_UPDATE: + rec_index = rx_buf[PWTAG_WRITE_IDX_IDX]; + switch(rec_index) + { + case 0xaa: //升级下发 + memcpy(&rec_wenjian_daxiao,&rx_buf[11],2);//文件大小 + if(rec_wenjian_daxiao>APP_SIZE||rec_wenjian_daxiao==0) + {}//文件过大,超出范围 + else + { + final_bag_num=(rec_wenjian_daxiao/ONE_BAG_DAXIAO)+1; + memcpy(&wangguan_up_id,&rx_buf[SOURCE_ID_IDX],2);//网关ID 占用2个字节 + shengji_flag=1; + LoraUp_flag=1; + LOG_INFO(TRACE_MODULE_APP, "进入升级模式,接收文件大小%d\r\n",rec_wenjian_daxiao); + } + break; + } + break; + } +// } + } +} +int TagRange(void) +{ + if(readgcom_flag) + { + readgcom_flag=0; + phy_timer_open(1, IRQ_PRIORITY_HIGH); + mac_register_process_handler(tx_int_callback, rx_int_callback); + uint8_t data_length = 0x50; + uint16_t checksum = 0; + uwb_sendbuffer[MSG_TYPE_IDX] = LORA_MSGTYPE_READPARARESP; + uwb_sendbuffer[MSG_LENGTH] = data_length+11; + memcpy(&uwb_sendbuffer[SOURCE_ID_IDX],&g_com_map[DEV_ID],2); + memcpy(&uwb_sendbuffer[DEST_ID_IDX],&wg_report_id,2); + uwb_sendbuffer[RP_CMD_IDX] = WGRSP_RWTAG_READ; + uwb_sendbuffer[RP_INDEX_IDX] = 2; + uwb_sendbuffer[RP_LEN_IDX] = data_length; + memcpy(&uwb_sendbuffer[9], &g_com_map[1], data_length); + checksum = Checksum_u16(uwb_sendbuffer,11+data_length); + memcpy(&uwb_sendbuffer[11+data_length],&checksum,2); +// Radio.Send(uwb_sendbuffer,data_length+13); + // Delay_Ms(100); + poll_tx_en_start_u32 = phy_timer_count_get()+US_TO_PHY_TIMER_COUNT(POLL_DELAY);//发送必须要延时发送才可以用于测距否则立即发送会获取时间戳不对,需要计算程序运行时间,避免设置过去时间 + tempflag=uwb_tx(uwb_sendbuffer,data_length+13,1,poll_tx_en_start_u32);//立即发送 + poll_tx_ts_i64 = ranging_tx_time_correct(poll_tx_en_start_u32 + phy_shr_duration());//修正时间戳 + temp_count1_anchor=phy_timer_count_get(); + while(mac_is_busy());//等待发送完成 + }else{ + phy_timer_open(1, IRQ_PRIORITY_HIGH); + // Register rx interrupt callback function + mac_register_process_handler(tx_int_callback, rx_int_callback); + //taglist_total_num=50;//测试 + uwb_poll_buffer_construct(); 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*nearbase_num,1,poll_tx_en_start_u32);//立即发送 - - //gpio_pin_set(IO_PIN_5);//测试 + 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 //LOG_INFO(TRACE_MODULE_APP, "进入测距",poll_tx_num,resp_rx_num,distance); poll_tx_ts_i64 = ranging_tx_time_correct(poll_tx_en_start_u32 + phy_shr_duration());//修正时间戳 - temp_tag_count1=phy_timer_count_get(); + temp_count1_anchor=phy_timer_count_get(); while(mac_is_busy());//等待发送完成 - temp_tag_count2=phy_timer_count_get(); +// 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);//测试 + #endif + temp_count2_anchor=phy_timer_count_get(); resp_rx_en_start_u32 = poll_tx_en_start_u32+ US_TO_PHY_TIMER_COUNT(POLL_TX_TO_RESP_RX_DLY_US);//设置发送多少s后接收按target独立时间起点算+ US_TO_PHY_TIMER_COUNT(POLL_TX_TO_RESP_RX_DLY_US为发送后要多久开启接收时间 - temp_resp=resp_rx_en_start_u32; + temp_resp_anchor=resp_rx_en_start_u32; //tempflag=uwb_rx(1, resp_rx_en_start_u32, RESP_RX_TIMEOUT_US);//开启接受并设置接收超时 - tempflag=uwb_rx(0,0, RESP_RX_TIMEOUT_US); + tempflag=uwb_rx(1,resp_rx_en_start_u32, RESP_RX_TIMEOUT_US); - receive_flag =0; - start_receive_count=phy_timer_count_get(); - poll_timeout=nearbase_num*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS)+US_TO_PHY_TIMER_COUNT(RESP_RX_TIMEOUT_US);//多一个多0.4ms默认0.4ms计算为0.125*4*100000,默认开启1mss - end_receive_count=start_receive_count+poll_timeout; - if(end_receive_count>=UINT32_MAX) - {end_receive_count-=UINT32_MAX;} - current_count=phy_timer_count_get(); - while(current_count<end_receive_count||current_count>end_receive_count+HALF_SECOND_TIME)//循环接受包体,若为124.8K则是+62400000 + + start_receive_count_anchor=phy_timer_count_get(); + //poll_timeout_Anchor=nearbase_num*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS)+US_TO_PHY_TIMER_COUNT(RESP_RX_TIMEOUT_US);//多一个多0.4ms默认0.4ms计算为0.125*4*100000,默认开启1mss + poll_timeout_Anchor=US_TO_PHY_TIMER_COUNT(RESP_RX_TIMEOUT_US_DEFAULT)+tag_num_tosend*US_TO_PHY_TIMER_COUNT(DELAY_BETWEEN_TWO_FRAME_UUS); + end_receive_count_anchor=start_receive_count_anchor+poll_timeout_Anchor; + + if(end_receive_count_anchor>=UINT32_MAX) + {end_receive_count_anchor-=UINT32_MAX;} + current_count_anchor=phy_timer_count_get(); + count_index=end_receive_count_anchor+HALF_SECOND_TIME; +// if(tt){ + while(current_count_anchor<end_receive_count_anchor||current_count_anchor>end_receive_count_anchor+HALF_SECOND_TIME)//循环接受包体,若为124.8K则是+62400000 { - current_count=phy_timer_count_get();//不断刷新当前计数器值 - temp_tag_count2=phy_timer_count_get(); - while(mac_is_busy());//等待接收完成 - temp_tag_count3=phy_timer_count_get(); + current_count_anchor=phy_timer_count_get();//不断刷新当前计数器值 + temp_count2_anchor=phy_timer_count_get(); + while(mac_is_busy())//等待接收完成 + { + IdleTask(); + current_count_anchor=phy_timer_count_get(); + if(current_count_anchor>end_receive_count_anchor&¤t_count_anchor<end_receive_count_anchor+HALF_SECOND_TIME) + { + break; + } + } + + temp_count3_anchor=phy_timer_count_get(); 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_RESPONSE&&!memcmp(&rx_buf[TAG_ID_IDX],&dev_id,2)&&rx_buf[GROUP_ID_IDX]==group_id) //判断接收到的数据是否是response数据 + if(rx_buf[MESSAGE_TYPE_IDX] == MBX_POLL)//收到是其他的基站的POLL包 + { + rand_temp=rand()%10; + sleep_timer_start(__MS_TO_32K_CNT(SLEEP_COUNT-rand_temp));//测试 + }else if (rx_buf[MESSAGE_TYPE_IDX] == MBX_RESPONSE&&!memcmp(&rx_buf[TAG_ID_IDX],&dev_id,2)&&rx_buf[GROUP_ID_IDX]==group_id) //判断接收到的数据是否是response数据 { - memcpy(&rec_nearbaseid,&rx_buf[ANCHOR_ID_IDX],2); - if(last_nearbase_num==0) - { - nearbaseid_list[0]=rec_nearbaseid; - nearbase_num=1; - exsistbase_list[0]=KEEP_TIMES; - SetANCTimestap(0,&rx_buf[RESP_MSG_POLL_RX_TS_IDX],&rx_buf[RESP_MSG_RESP_TX_TS_IDX],(uint32_t)resp_rx_ts_i64,&rx_buf[RESP_MSG_ANC_DISTOFFSET],test2,(uint32_t)poll_tx_ts_i64);//这个是改过的参数 - } -// if(rec_nearbaseid==nearbaseid_list[0]) -// { -// exsistbase_list[0]=KEEP_TIMES; -// SetANCTimestap(0,&rx_buf[RESP_MSG_POLL_RX_TS_IDX],&rx_buf[RESP_MSG_RESP_TX_TS_IDX],(uint32_t)resp_rx_ts_i64,&rx_buf[RESP_MSG_ANC_DISTOFFSET],test2);//这个是改过的参数 -// } - else - { - rec_nearbasepos=FindNearBasePos(rec_nearbaseid); - SetANCTimestap(rec_nearbasepos,&rx_buf[RESP_MSG_POLL_RX_TS_IDX],&rx_buf[RESP_MSG_RESP_TX_TS_IDX],(uint32_t)resp_rx_ts_i64,&rx_buf[RESP_MSG_ANC_DISTOFFSET],test2,(uint32_t)poll_tx_ts_i64); - if(rec_nearbasepos>=last_nearbase_num) //发现新的基站 - { - //get_newbase=1; - nearbase_num++; - nearbaseid_list[rec_nearbasepos] = rec_nearbaseid; - } - exsistbase_list[rec_nearbasepos]=KEEP_TIMES;//更新存活时间 - } + //reboot_num=0;//清0复位监测数值 + memcpy(&rec_nearbaseid,&rx_buf[ANCHOR_ID_IDX],2); + rec_tag_index = CmpTagInList_Anchor(rec_nearbaseid);//当前新来的这包数据插入当前表中的位置索引 + //rec_nearbasepos=FindNearBasePos(rec_nearbaseid); + freqlost_count = 0; + temp_index_anchor =rec_tag_index -taglist_current_index; + SetANCTimestap(temp_index_anchor,&rx_buf[RESP_MSG_POLL_RX_TS_IDX],&rx_buf[RESP_MSG_RESP_TX_TS_IDX],(uint32_t)resp_rx_ts_i64,&rx_buf[RESP_MSG_ANC_DISTOFFSET],test2,(uint32_t)poll_tx_ts_i64); +// if(rec_nearbasepos>=nearbase_num) //发现新的基站 +// { +// //get_newbase=1; +// nearbase_num++; +// nearbaseid_list[rec_nearbasepos] = rec_nearbaseid; +// } +// exsistbase_list[rec_nearbasepos]=KEEP_TIMES;//更新存活时间 + memset(rx_buf, 0, sizeof(rx_buf));//清空接收到的用完的数组 rx_length=0;//清0长度 -// uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//立即开启接受并设置0超时 + //uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//立即开启接受并设置0超时 // while(mac_is_busy()); //break;//成功接收就退出 - } - tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//立即开启接受并设置0超时 - //gpio_pin_clr(IO_PIN_5);//测试 - + }else if(rx_buf[MSG_TYPE_IDX]==LORA_MSGTYPE_WGRESPTAG){ + Uwb_Update(); + } + +// #ifdef BOXING +// gpio_pin_clr(IO_PIN_5);//测试 +// #endif }else if(receive_flag==2){//接收出错 receive_flag=0; tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//立即开启接受并设置0超时 - temp_tag_count3=phy_timer_count_get(); +// sleep_timer_start(__MS_TO_32K_CNT(SLEEP_COUNT-phy_timer_count_get()%30));//测试 +// sleep_time_step=SLEEP_COUNT-phy_timer_count_get()%10; + //tempflag=uwb_rx(0, 0, RESP_RX_TIMEOUT_US);//立即开启接受并设置0超时 + temp_count3_anchor=phy_timer_count_get(); // while(mac_is_busy()); - temp_tag_count1=phy_timer_count_get(); + temp_count1_anchor=phy_timer_count_get(); } - //gpio_pin_clr(IO_PIN_5);//测试 + #ifdef BOXING + 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 + gpio_pin_clr(IO_PIN_5);//测试 + #endif delay_us(1); uwb_rx_force_off(1); + //dwt_forecetrxoff(); CalculateDists(poll_tx_ts_i64); //计算距离并更新距离表 - AnchorListUpdate();//更新存活基站列表 - Rank_ANchor_list_by_dis();//冒泡排序重新按距离重新排列基站列表最小距离存货基站放到最前面 - LOG_INFO(TRACE_MODULE_APP,"基站ID:%X,距离: %d\r\n",rec_nearbaseid,nearbase_distlist[0]); + + //Calculate_battery_percent(); + + //LOG_INFO(TRACE_MODULE_APP,"现存测距数 %d id1:%04x dist:%d .\r\n",taglist_total_num,taglist_id[0],(int16_t)taglist_dist[0]); + //AnchorListUpdate();//更新存活基站列表 + AnchorLiatUpdate(); + taglist_current_index+=tag_num_tosend; + if(taglist_current_index>=taglist_total_num||taglist_total_num<=SEND_GROUP_MAX_NUM) //如果标签当前index大于总index,或者总基站数量小于等于25,则从0开始index。一个是已经发完一轮了,一个是不够最大发送数 + taglist_current_index = 0; + //Rank_ANchor_list_by_dis();//冒泡排序重新按距离重新排列基站列表最小距离存货基站放到最前面 + } +} +//#endif +uint16_t UpdateCRC16(uint16_t crcIn, uint8_t byte) +{ + uint32_t crc = crcIn; + uint32_t in = byte|0x100; + + do + { + crc <<= 1; + in <<= 1; + + if(in&0x100) + { + ++crc; + } + + if(crc&0x10000) + { + crc ^= 0x1021; + } + } while(!(in&0x10000)); + + return (crc&0xffffu); +} + +/** + * @brief Cal CRC16 for YModem Packet + * @param data + * @param length + * @retval CRC value + */ +uint16_t Cal_CRC16(const uint8_t* data, uint32_t size) +{ + uint32_t crc = 0; + const uint8_t* dataEnd = data+size; + + while(data<dataEnd) + { + crc = UpdateCRC16(crc,*data++); + } + crc = UpdateCRC16(crc,0); + crc = UpdateCRC16(crc,0); + + return (crc&0xffffu); +} +//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; +uint8_t huifushengjibaoerror_num; +uint16_t final_bag_num; +uint16_t testflag; +uint32_t Zhongjian_data[60]; +uint8_t shengji_flag; +uint16_t wangguan_up_id; +uint8_t huifushengjibao_flag; +uint8_t LoraUp_flag; +uint16_t rec_wenjian_daxiao; +uint8_t lora_up_rec_flag; +uint16_t flash_shuju; +static uint32_t status_reg; +static uint32_t frame_len; +void LoraUp_Poll(void) +{ uint16_t result ; + uint16_t crc16; + uint8_t jindu; + if(shengji_flag) + { + uwb_rx_force_off(1); + Tag_uwb_init(); + phy_timer_open(1, IRQ_PRIORITY_HIGH); + mac_register_process_handler(tx_int_callback, rx_int_callback); + + send_lora_data[MSG_TYPE_IDX]=LORA_MSGTYPE_UPDATE_CONFIRM; + memcpy(&send_lora_data[SOURCE_ID_IDX],&dev_id,2);//网关ID 占用2个字节 + memcpy(&send_lora_data[DEST_ID_IDX],&wangguan_up_id,2);//标签或者基站的设备ID 2个字节 +// send_lora_data[MUQIAN_BAG]=0xFF; + crc16=Checksum_u16(send_lora_data,6); + memcpy(&send_lora_data[6],&crc16,2); + huifushengjibao_flag=0; + LoraUp_flag=1; + + poll_tx_en_start_u32 = phy_timer_count_get()+US_TO_PHY_TIMER_COUNT(POLL_DELAY);//发送必须要延时发送才可以用于测距否则立即发送会获取时间戳不对,需要计算程序运行时间,避免设置过去时间 + tempflag=uwb_tx(send_lora_data,8+2,1,poll_tx_en_start_u32);//立即发送 + poll_tx_ts_i64 = ranging_tx_time_correct(poll_tx_en_start_u32 + phy_shr_duration());//修正时间戳 + temp_count1_anchor=phy_timer_count_get(); + while(mac_is_busy());//等待发送完成 + + __disable_irq(); + result=flash_open(FLASH_ID0,NULL); + flash_read(FLASH_ID0, APP2_ADRESS, (uint8_t*)&flash_shuju, 2); + if(flash_shuju!=0xffff/*&&flash_shuju!=0x0000*/) + { + while(1) + { + flash_erase(FLASH_ID0,APP2_ADRESS,APP_SIZE); + delay_ms(500); + flash_read(FLASH_ID0, APP2_ADRESS, (uint8_t*)&flash_shuju, 2); + if(flash_shuju==0xffff/*&&flash_shuju!=0x0000*/) + {break;} + } + } + __enable_irq(); + while(1) + { + uwb_rx_force_off(1); + send_lora_data[MSG_TYPE_IDX]=LORA_MSGTYPE_UPDATEFILE_REQUEST; + memcpy(&send_lora_data[SOURCE_ID_IDX],&dev_id,2);//网关ID 占用2个字节 + memcpy(&send_lora_data[DEST_ID_IDX],&wangguan_up_id,2);//标签或者基站的设备ID 2个字节 +// send_lora_data[MUQIAN_BAG]=muqiandeshengjibao; + memcpy(&send_lora_data[MUQIAN_BAG],&muqiandeshengjibao,2); + crc16=Checksum_u16(send_lora_data,8); + memcpy(&send_lora_data[8],&crc16,2); + lora_up_rec_flag=0; +// LOG_INFO(TRACE_MODULE_APP, "正在请求第%d包\r\n",muqiandeshengjibao); + jindu=(muqiandeshengjibao*100)/final_bag_num; + LOG_INFO(TRACE_MODULE_APP, "升级进度%d\r\n",jindu); + wdt_ping(WDT_ID0);//喂狗 + poll_tx_en_start_u32 = phy_timer_count_get()+US_TO_PHY_TIMER_COUNT(POLL_DELAY);//发送必须要延时发送才可以用于测距否则立即发送会获取时间戳不对,需要计算程序运行时间,避免设置过去时间 + tempflag=uwb_tx(send_lora_data,10+2,1,poll_tx_en_start_u32);//立即发送 + poll_tx_ts_i64 = ranging_tx_time_correct(poll_tx_en_start_u32 + phy_shr_duration());//修正时间戳 + temp_count1_anchor=phy_timer_count_get(); + while(mac_is_busy());//等待发送完成 + temp_count2_anchor=phy_timer_count_get(); + resp_rx_en_start_u32 = poll_tx_en_start_u32+ US_TO_PHY_TIMER_COUNT(POLL_TX_TO_RESP_RX_DLY_US);//设置发送多少s后接收按target独立时间起点算+ US_TO_PHY_TIMER_COUNT(POLL_TX_TO_RESP_RX_DLY_US为发送后要多久开启接收时间 + temp_resp_anchor=resp_rx_en_start_u32; + tempflag=uwb_rx(0,0, 50000); + + while(mac_is_busy()) + {} + if(receive_flag==1)//成功接收数据 + { uint16_t result12; + + __disable_irq(); + crc16=Cal_CRC16(rx_buf,DATA_IDX+ONE_BAG_DAXIAO); + + if(!memcmp(&muqiandeshengjibao,&rx_buf[MUQIAN_BAG],2)&&!memcmp(&crc16,&rx_buf[DATA_IDX+ONE_BAG_DAXIAO],2)&&!memcmp(&dev_id,&rx_buf[DEST_ID_IDX],2)&& !memcmp(&wangguan_up_id,&rx_buf[SOURCE_ID_IDX],2)) + { + result=flash_open(FLASH_ID0,NULL); + memcpy(Zhongjian_data,&rx_buf[DATA_IDX],ONE_BAG_DAXIAO); + result12=flash_write_nbytes(FLASH_ID0, APP2_ADRESS+ONE_BAG_DAXIAO*muqiandeshengjibao, (uint8_t*)Zhongjian_data, ONE_BAG_DAXIAO); + while(!result12==0) + { + result12=flash_write_nbytes(FLASH_ID0, APP2_ADRESS+ONE_BAG_DAXIAO*muqiandeshengjibao, (uint8_t*)Zhongjian_data, ONE_BAG_DAXIAO); + delay_ms(10); + } + if(result12==0) + { +// if(muqiandeshengjibao==650) +// { +// delay_ms(10); +// } + muqiandeshengjibao++; + huifushengjibaoerror_num=0; + delay_ms(10); + } + } + __enable_irq(); + } + else + { + delay_ms(500); + huifushengjibaoerror_num++; + if(huifushengjibaoerror_num>=20) + { + huifushengjibaoerror_num=0; + muqiandeshengjibao=0; + delay_ms(500); + NVIC_SystemReset(); + break; + } + uwb_rx_force_off(1); + } + if(muqiandeshengjibao==final_bag_num) + { + send_lora_data[MSG_TYPE_IDX]=0x99; + memcpy(&send_lora_data[SOURCE_ID_IDX],&dev_id,2);//网关ID 占用2个字节 + memcpy(&send_lora_data[DEST_ID_IDX],&wangguan_up_id,2);//标签或者基站的设备ID 2个字节 + crc16=Checksum_u16(send_lora_data,6); + memcpy(&send_lora_data[6],&crc16,2); + huifushengjibao_flag=0; + LoraUp_flag=1; + LOG_INFO(TRACE_MODULE_APP, "正在发送最后一包\r\n"); + poll_tx_en_start_u32 = phy_timer_count_get()+US_TO_PHY_TIMER_COUNT(POLL_DELAY);//发送必须要延时发送才可以用于测距否则立即发送会获取时间戳不对,需要计算程序运行时间,避免设置过去时间 + tempflag=uwb_tx(send_lora_data,8+2,1,poll_tx_en_start_u32);//立即发送 + poll_tx_ts_i64 = ranging_tx_time_correct(poll_tx_en_start_u32 + phy_shr_duration());//修正时间戳 + temp_count1_anchor=phy_timer_count_get(); + while(mac_is_busy());//等待发送完成 + + uint16_t tmp11 ,result11; + tmp11=0XDDDD; + result=flash_open(FLASH_ID0,NULL); + result11=flash_erase(FLASH_ID0,APP_1OR2_ADRESS,0x1000); + if(result11==0) + flash_write_nbytes(FLASH_ID0, APP_1OR2_ADRESS, (uint8_t*)&tmp11, 2); + __disable_irq(); + delay_ms(500); + NVIC_SystemReset(); + } + } + uwb_rx_force_off(1); + } } -void UWB_Tag_Poll(void) -{ - switch(UWB_work_state) - { - case LINK_SUCCESS: - case SEARCH_DEV: - case UN_BIND: - { - uwb_led_on(); - TagRange(); - uwb_led_off(); - } - break; - } -// update_led_power_state();//更新灯状态 -} \ No newline at end of file -- Gitblit v1.9.3