yincheng.zhong
2024-08-29 b4a3dfd45831f0db1e19f74beb685027e8dbc564
keil/uwb_app.c
@@ -50,7 +50,7 @@
uint32_t temp_count2=0;
uint32_t temp_count3=0;
uint32_t temp_internal=0;
int temp_flag,distance;
int32_t distance;
uint8_t taglist_num;
extern uint8_t recev_error_num;
@@ -87,7 +87,7 @@
#define FINAL_MSG_RESP_RX_TS_IDX 14
#define FINAL_MSG_FINAL_TX_TS_IDX 18
#define DELAY_DEFAULT 1000
#define HALF_SECOND_TIME 62400000
#define HALF_SECOND_TIME 624000000
/* Length of the common part of the message */
#define MSG_COMMON_LEN 10
@@ -143,9 +143,9 @@
static uint32_t resp_tx_timeout;
int64_t temp_resp_i64;
/* 41 bits timestamps of frames transmission/reception. */
 int64_t poll_rx_ts_i64;
 int64_t resp_tx_ts_i64;
 int64_t final_rx_ts_i64;
int64_t poll_rx_ts_i64;
int64_t resp_tx_ts_i64;
int64_t final_rx_ts_i64;
/* Frame sequence number, incremented after each transmission. */
static uint8_t frame_seq_nb = 0;
@@ -196,6 +196,7 @@
}
/* RX done process handler. */
int8_t rssi;
static void rx_int_callback(struct MAC_HW_REPORT_T *rx_report)
{
    // Power off radio
@@ -213,6 +214,7 @@
        poll_rx_en_start_u32 = rx_rpt.timestamp - phy_shr_duration();
        poll_rx_ts_i64 = ranging_rx_time_correct(&rx_rpt);
        poll_rx_num++;
        rssi = rx_report->rssi;
        receive_flag=1;
        recev_error_num=0;
    }
@@ -226,7 +228,7 @@
        /* UWB_STS_ERR     STS error                 */
        memcpy(&rx_rpt, rx_report, sizeof(struct MAC_HW_REPORT_T));
        rx_length = 0;
    }
}
@@ -272,6 +274,7 @@
    anchordata_dist[i] = dist;
    anchordata_bat[i] = battary;
    tagofflinetime[i]=0;//不断更新当前TAG对应离线时间
    //if(dist>0)
    distance=dist;
}
uint16_t CmpTagInList(uint16_t tagid)
@@ -337,11 +340,13 @@
    memcpy(&send_buffer[RESP_MSG_ANC_DISTOFFSET],&disoffset,2);//差个修正offset,修正有符号但是这个com表为无符号的,传过去直接赋给Int16_t相当于还原了
    temp_resp_i64=resp_tx_ts_i64;
    temp_count3= phy_timer_count_get();
    temp_flag=uwb_tx(send_buffer, 40,1 ,resp_tx_en_start_u32);//立即发送测试size大小
    uwb_tx(send_buffer, 40,1 ,resp_tx_en_start_u32);//立即发送测试size大小
    temp_count1=phy_timer_count_get();
    while(mac_is_busy());
    gpio_pin_clr(SCL_PIN);
}
uint16_t uwb_searchcount;
extern uint8_t g_start_send_flag,search_open_flag = 1,link_success_flag;
int UwbRange(void)
{
    uint8_t i;
@@ -400,6 +405,7 @@
            {
                if(rec_ancidlist[i] == dev_id)
                {
                    rec_ancdistlist[i]+=(int16_t)g_com_map[DIST_OFFSET];
                    PushAnchorDataArray(tag_id_recv,rec_ancdistlist[i],battary);
                }
            }
@@ -418,18 +424,20 @@
            {
                seize_anchor=1;   //抢占anchor
                Anchor_RecNearPoll(i);
            }
        }
    }else{
        if(recev_error_num++>20)
            {
                recev_error_num=0;
                UWB_work_state=SEARCH_DEV;
            }
    }
            LOG_INFO(TRACE_MODULE_APP,"标签ID:%X,距离: %d 信号强度:%d.\r\n",g_com_map[BIND_DEV_ID],distance,rssi);
        }
    } else {
        LOG_INFO(TRACE_MODULE_APP,"测距失败\r\n");
        if(recev_error_num++>5)
        {
            recev_error_num=0;
            UWB_work_state=SEARCH_DEV;
            uwb_searchcount = 0;
            search_open_flag = 1;
        }
    }
    sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US));//测试
    gpio_pin_clr(SCL_PIN);
}
@@ -438,7 +446,7 @@
//主函数绑定接受逻辑
int UwbSearch(void)
{
    uint8_t i;
    uint16_t tempid;
    // The following peripherals will be initialized in the uwb_open function
@@ -475,18 +483,18 @@
    {
        end_receive_count-=UINT32_MAX;
    }
    while(current_count<end_receive_count||current_count>end_receive_count+HALF_SECOND_TIME)//循环接受包体,若为124.8K则是+62400000
    {
        while(mac_is_busy())
        {
        {
            current_count=phy_timer_count_get();
            if(current_count>end_receive_count&&current_count<end_receive_count+HALF_SECOND_TIME)
            {
                    break;
            }
        }
        temp_count2=phy_timer_count_get();
        if(receive_flag==1)//成功接收
        {
@@ -523,13 +531,13 @@
                    Anchor_RecNearPoll(i);
                }
                sleep_timer_start(__MS_TO_32K_CNT(UWB_DELAY_TIME_US));//测试
                 gpio_pin_clr(SCL_PIN);//测试
            return 1;//返回发送成功标志
                gpio_pin_clr(SCL_PIN);//测试
                return 1;//返回发送成功标志
            }
        }
      uwb_rx(0, 0, RX_SYNC_WIN_US_TEMP);//再次开启接收
        uwb_rx(0, 0, RX_SYNC_WIN_US_TEMP);//再次开启接收
    }
    gpio_pin_clr(SCL_PIN);//测试
@@ -538,26 +546,26 @@
//}
#define SEARCH_TIMESTEMP 120
uint16_t uwb_searchcount;
extern uint8_t g_start_send_flag,search_open_flag = 1,link_success_flag;
void UWBPoll(void)
{
    switch(UWB_work_state)
        {
    LOG_INFO(TRACE_MODULE_APP,"UWB状态:%d\r\n",UWB_work_state);
    switch(UWB_work_state)
    {
    case LINK_SUCCESS:
    {   //连接成功进行轮询测距
            uwb_led_on();
            UwbRange();
            uwb_led_off();
        uwb_led_on();
        UwbRange();
        uwb_led_off();
    }
    break;
    case SEARCH_DEV:
    {   //接包不成功或者通讯失败进入搜索模式
        if(search_open_flag)
            {
            first_search_flag=0;
        if(search_open_flag)
        {
            LOG_INFO(TRACE_MODULE_APP,"开启搜索\r\n");
            search_open_flag=0;
            uwb_led_on();
            link_success_flag=UwbSearch();//第一次开启接收2s绑定失败后则进行下面1分钟后重新开启2s绑定流程
            uwb_led_off();
@@ -574,18 +582,18 @@
    break;
    }
    if(UWB_work_state==LINK_SUCCESS)
        { //成功时清0错误计数
        uwb_searchcount=0;
    {   //成功时清0错误计数
        uwb_searchcount=0;
    }
    if(UWB_work_state==SEARCH_DEV)
        {
    {
        if(!search_open_flag)
        {
        {
            if(uwb_searchcount++>SEARCH_TIMESTEMP)
                {
                    uwb_searchcount=0;
                    search_open_flag=1;
                }
            {
                uwb_searchcount=0;
                search_open_flag=1;
            }
        }
        //link_error_count+=g_com_map[COM_INTERVAL];
        //if(link_error_count>=g_com_map[4G_INTERNAL])