yincheng.zhong
2024-01-22 4170266b0f0e779d0c1dcbe519e3307e9f2cde7a
Src/application/dw_mbx_anc.c
@@ -1,11 +1,141 @@
#include "dw_mbx_anc.h"
///////////////////////////////////免布线时间同步代码
//免布线基站端代码
#include "dw_app.h"
#include "deca_device_api.h"
#include "deca_regs.h"
#include "dw_driver.h"
#include "Spi.h"
#include "global_param.h"
typedef enum{   Sync_Lost, //同步丢失         同步时间 100秒同步一次,每次同步开启2秒,发起测距请求
                Sync_Range,//同步状态 测距    同步时间 10秒同步一次,开启10ms,发起测距请求
                Sync_Config,//同步状态 配置   同步时间 1秒同步一次,不发起测距请求
                Sync_Sleep, //同步状态 休眠    同步时间 100秒同步一次,不发起测距请求
}sync_state_enum;
sync_state_enum sync_state = Sync_Lost;
static uint8_t sync_interval_s,range_state = 0;
static uint16_t sync_listen_ms;
void SyncStateSwitch(void) //同步切换逻辑,开机LOST状态,收到信号后进入同步状态,在同步状态中,根据收到的网关信息判断测距还是休眠 测距 配置状态
{
    switch(sync_state)
    {
        case Sync_Lost:
            sync_interval_s = 100;
            sync_listen_ms =2000;
            range_state = 1;
            break;
        case Sync_Range:
            sync_interval_s = 10;
            sync_listen_ms =5;
            range_state = 1;
            break;
        case Sync_Config:
            sync_interval_s = 1;
            sync_listen_ms =5;
            range_state = 0;
            break;
        case Sync_Sleep:
            sync_interval_s = 100;
            sync_listen_ms =5;
            range_state = 0;
            break;
    }
}
uint8_t GetRangeState(void)
{
    return range_state;
}
#define SYNCLOST_THRES   10
static uint16_t current_count,poll_startcount,start_count,end_count,poll_timeout;
static uint8_t sync_interval_count = 0,flag_sync_success,sync_lost_count;
void SetFlagSyncSuccess(uint8_t value)
{
    flag_sync_success = value;
}
static uint16_t sync_wg_id,rec_sync_count;
uint8_t lora_recbuffer[255],lora_recbuffer_len;
static uint16_t synclock_zero_count,wg_sync_polltime_100us,uwbpoll_time_count;
uint16_t GetUwbPollCount(void)
{
    return uwbpoll_time_count;
}
uint16_t GetWGRecSyncCount(void)
{
    return rec_sync_count;
}
void SyncClockProcess(void) //同步时钟函数获取下次UWBpoll的定时器count。
{
    memcpy(&wg_sync_polltime_100us,&lora_recbuffer[SYSTIME_IDX],2); //WG下发的同步时间,单位0.1ms。
    synclock_zero_count = rec_sync_count-(uint32_t)wg_sync_polltime_100us*100/LPTIMER_LSB;
    if(synclock_zero_count<0)
        synclock_zero_count+=32768;
    uwbpoll_time_count = synclock_zero_count + (uint32_t)g_com_map[UWBPOLLTIME_MS_IDX]*1000/LPTIMER_LSB;
    if(uwbpoll_time_count>32768)
        uwbpoll_time_count-=32768;
}
void LoraSyncRecPoll(void)
{
    if(sync_interval_count++>=sync_interval_s)
    {
        sync_interval_count = 0;
        flag_sync_success = 0;
        SwitchLoraToSyncRec(); //切换lora接收频点
        start_count=HAL_LPTIM_ReadCounter(&hlptim1);     //等待接收完成直到超时
        poll_timeout=sync_listen_ms*10; //单位0.1ms
        end_count=start_count+(poll_timeout<<2);
        if(end_count>=32768)
        {end_count-=32768;}
        current_count=HAL_LPTIM_ReadCounter(&hlptim1);
       while(GetFlagLoraWaitSync())
       {
         current_count=HAL_LPTIM_ReadCounter(&hlptim1);
         if(current_count>=end_count&&current_count<end_count+15000)
         {
                break;
         }
       }
       if(flag_sync_success)  //同步成功
       {
           sync_lost_count = 0;
           lora_recbuffer_len = GetLoraBufferAndLen(lora_recbuffer,&rec_sync_count);//获取LORA收到的数据和长度
           if(lora_recbuffer[MSG_TYPE_IDX]==LORA_MSGTYPE_SYNC)
           {
                memcpy(&g_com_map[SYNCWG_ID_IDX],&lora_recbuffer[WG_ID_IDX],2);
                if(lora_recbuffer[RANGE_STATE_IDX])
                {
                    sync_state = Sync_Range;
                }else{
                    sync_state = Sync_Sleep;
                }
                switch(lora_recbuffer[SUBMSGTYPE_IDX])
                {
                    case SUBMSG_READ_ANCPARA:
                        break;
                    case SUBMSG_WRITE_ANCPARA:
                        break;
                    case SUBMSG_UPDATE_ANC:
                        break;
                }
           }
       }else{
            if(sync_lost_count++>SYNCLOST_THRES)
            {
                sync_state = Sync_Lost;
                SyncStateSwitch();
            }
       }
   }
  //  Radio.Sleep( ); //如果超时就sleep
}
////////////////////////////免布线测距相关代码
#define TAG_KEEPTIMES    30 //标签存活时间
#define MAX_TAG_LIST_NUM 200 //同时通讯标签最大数量
@@ -18,7 +148,7 @@
static u8 frame_seq_nb,frame_len;
static uint16_t current_count,poll_startcount,start_count,end_count,poll_timeout;
static uint32_t status_reg = 0;
extern int32_t dwt_readcarrierintegrator(void);
static int32_t test2;
@@ -185,15 +315,7 @@
   uwb_sendbuffer[MESSAGE_TYPE_IDX] = MBX_POLL;
    dwt_writetxdata(13+4*tag_num_tosend, uwb_sendbuffer, 0);//将Poll包数据传给DW1000,将在开启发送时传出去
   dwt_writetxfctrl(13+4*tag_num_tosend, 0);//设置超宽带发送数据长度
//    current_count=HAL_LPTIM_ReadCounter(&hlptim1);
//    while(current_count<poll_startcount||current_count>poll_startcount+16384)
//    {
//        current_count=HAL_LPTIM_ReadCounter(&hlptim1);
//        if(current_count<poll_startcount-300)
//        {
//            break;
//        }
//    }
   dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);//开启发送,发送完成后等待一段时间开启接收,等待时间在dwt_setrxaftertxdelay中设置   
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_12, GPIO_PIN_RESET);