zhyinch
2020-04-15 d1f86e2717e82d116a0329f33f57435e8f1d4a45
Src/application/dw_app.c
@@ -46,7 +46,7 @@
#define POLL_TX_TO_RESP_RX_DLY_UUS 150
/* This is the delay from Frame RX timestamp to TX reply timestamp used for calculating/setting the DW1000's delayed TX function. This includes the
 * frame length of approximately 2.66 ms with above configuration. */
#define RESP_RX_TO_FINAL_TX_DLY_UUS 800
#define RESP_RX_TO_FINAL_TX_DLY_UUS 850
/* Receive response timeout. See NOTE 5 below. */
#define RESP_RX_TIMEOUT_UUS 600
@@ -65,20 +65,22 @@
#define FINAL_MSG_TS_LEN 4
#define SYNC_SEQ_IDX             5
//common
#define GROUP_ID_IDX               0
#define ANCHOR_ID_IDX             1
#define TAG_ID_IDX                5
#define MESSAGE_TYPE_IDX          9   
#define DIST_IDX                      10
//Poll
#define ANC_TYPE_IDX                14
#define BATTARY_IDX                  15
#define BUTTON_IDX                  16
#define SEQUENCE_IDX               17
//respose
#define DIST_IDX                      10
#define ANCTIMEMS             14
#define ANCTIMEUS             16
#define ANCSEND_INTERVAL      18
#define POLL                    0x01
#define RESPONSE                0x02
@@ -104,10 +106,10 @@
static uint8_t tx_poll_msg[20] = {0};
static uint8_t tx_sync_msg[14] = {0};
//static uint8_t rx_resp_msg[] = {0x41, 0x88, 0, 0xCA, 0xDE, 'V', 'E', 'W', 'A', 0x10, 0x02, 0, 0, 0, 0};
static uint8_t tx_final_msg[] = {0x41, 0x88, 0, 0xCA, 0xDE, 'W', 'A', 'V', 'E', 0x23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static uint8_t tx_final_msg[24] = {0};
   
//static uint8_t rx_poll_msg[] = {0x00, 0x88, 0, 0xCA, 0xDE, 'W', 'A', 'V', 'E', 0x21, 0, 0};
static uint8_t tx_resp_msg[20] = {0};
static uint8_t tx_resp_msg[22] = {0};
//static uint8_t rx_final_msg[] = {0x41, 0x88, 0, 0xCA, 0xDE, 'W', 'A', 'V', 'E', 0x23, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
   
/* Frame sequence number, incremented after each transmission. */
@@ -254,7 +256,7 @@
    Reset_DW1000();//重启DW1000 /* Target specific drive of RSTn line into DW1000 low for a period. */
      Spi_ChangePrescaler(SPIx_PRESCALER_SLOW);   //设置为快速模式
    dwt_initialise(DWT_LOADUCODE);//初始化DW1000
   Spi_ChangePrescaler(SPIx_PRESCALER_FAST);   //设置为快速模式
     Spi_ChangePrescaler(SPIx_PRESCALER_FAST);   //设置为快速模式
    /* Configure DW1000. See NOTE 6 below. */
    dwt_configure(&config);//配置DW1000
@@ -276,6 +278,10 @@
   tx_resp_msg[MESSAGE_TYPE_IDX]=RESPONSE;
   tx_final_msg[MESSAGE_TYPE_IDX]=FINAL;
   tx_sync_msg[MESSAGE_TYPE_IDX]=SYNC;
   memcpy(&tx_poll_msg[GROUP_ID_IDX], &group_id, 1);
   memcpy(&tx_final_msg[GROUP_ID_IDX], &group_id, 1);
   memcpy(&tx_resp_msg[GROUP_ID_IDX], &group_id, 1);
   
   memcpy(&tx_poll_msg[TAG_ID_IDX], &dev_id, 4);
   memcpy(&tx_final_msg[TAG_ID_IDX], &dev_id, 4);
@@ -319,8 +325,11 @@
uint16_t checksum;
int8_t tag_delaytime;
extern uint16_t sync_timer;
uint16_t tmp_time;
uint32_t time32_incr;uint32_t frame_len;
uint16_t tmp_time,current_slottimes;
uint32_t time32_incr;
int32_t ancsync_time;
uint32_t frame_len;
int32_t count_offset,nextpoll_delaytime;
void Tag_App(void)//发送模式(TAG标签)
{
   
@@ -329,7 +338,8 @@
   uint8_t i,getsync_flag=0;
   //LED0_ON;
   //dwt_forcetrxoff();
       while (DWT_DEVICE_ID != id) // MP IC ONLY (i.e. DW1000) FOR THIS CODE
   id =  dwt_readdevid() ;
       while (DWT_DEVICE_ID != id)
    {
         id =  dwt_readdevid() ;
    }
@@ -337,7 +347,7 @@
    dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS);         //设置发送后开启接收,并设定延迟时间
    dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS);      
   tag_succ_times = 0;
   tx_poll_msg[BATTARY_IDX] = Get_Battary();
   tx_poll_msg[BATTARY_IDX] = bat_percent;
   //tx_poll_msg[BUTTON_IDX] = !READ_KEY0;
   tx_poll_msg[SEQUENCE_IDX] = frame_seq_nb++;
@@ -366,7 +376,7 @@
   /* Increment frame sequence number after transmission of the poll message (modulo 256). */
   if(status_reg==0xffffffff)
   {
      NVIC_SystemReset();
   //   NVIC_SystemReset();
   }
   if (status_reg & SYS_STATUS_RXFCG)//如果成功接收
@@ -383,8 +393,8 @@
      /* Check that the frame is the expected response from the companion "DS TWR responder" example.
       * As the sequence number field of the frame is not relevant, it is cleared to simplify the validation of the frame. */
      
      if (rx_buffer[MESSAGE_TYPE_IDX] == RESPONSE&&!memcmp(&rx_buffer[TAG_ID_IDX],&dev_id,4)) //判断接收到的数据是否是response数据
      { uint16_t anc_id_recv;
      if (rx_buffer[GROUP_ID_IDX] == group_id&&rx_buffer[MESSAGE_TYPE_IDX] == RESPONSE&&!memcmp(&rx_buffer[TAG_ID_IDX],&dev_id,4)) //判断接收到的数据是否是response数据
      { uint16_t anc_id_recv,current_count,rec_com_interval;
         /* Retrieve poll transmission and response reception timestamp. */
         poll_tx_ts = get_tx_timestamp_u64();                              //获得POLL发送时间T1
         resp_rx_ts = get_rx_timestamp_u64();                              //获得RESPONSE接收时间T4
@@ -402,10 +412,20 @@
            if(sync_timer>=1010)
               {sync_timer=0;}
         }
      //   TIM3->CNT=tmp_time;
      }
         memcpy(&hex_dist2, &rx_buffer[DIST_IDX], 4);
         memcpy(&tx_final_msg[ANCHOR_ID_IDX], &rx_buffer[ANCHOR_ID_IDX], 4);
         memcpy(&rec_com_interval,&rx_buffer[ANCSEND_INTERVAL],  2);
         if(rec_com_interval>4&&rec_com_interval!=g_com_map[COM_INTERVAL])
         {
            g_com_map[COM_INTERVAL]=rec_com_interval;
            save_com_map_to_flash();
            delay_ms(100);
            SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader
         }
         /* Compute final message transmission time. See NOTE 9 below. */
         final_tx_time = (resp_rx_ts + (RESP_RX_TO_FINAL_TX_DLY_UUS * UUS_TO_DWT_TIME)) >> 8;//计算final包发送时间,T5=T4+Treply2
         dwt_setdelayedtrxtime(final_tx_time);//设置final包发送时间T5
@@ -423,10 +443,34 @@
         dwt_writetxdata(sizeof(tx_final_msg), tx_final_msg, 0);//将发送数据写入DW1000
         dwt_writetxfctrl(sizeof(tx_final_msg), 0);//设定发送数据长度
         result=dwt_starttx(DWT_START_TX_DELAYED);//设定为延迟发送
         ancsync_time=((sync_timer+0)*1000+tmp_time);
         current_count=HAL_LPTIM_ReadCounter(&hlptim1);
      //   count_offset=sync_count-current_count-143;
      //   current_slottimes=(ancsync_time-10000)/(g_com_map[COM_INTERVAL]*1000);
         nextpoll_delaytime=tyncpoll_time*1000+g_com_map[COM_INTERVAL]*1000-((ancsync_time-10000)%(g_com_map[COM_INTERVAL]*1000))-5150;
         if(abs(ancsync_time-910000)<1000)
         {
         nextpoll_delaytime+=10000;
         }
         if(nextpoll_delaytime<2000)
         {
            nextpoll_delaytime+=g_com_map[COM_INTERVAL]*1000;
         }
         lastpoll_count= current_count+(nextpoll_delaytime)/LPTIMER_LSB;
         if(lastpoll_count>LPTIMER_1S_COUNT)
            lastpoll_count-=LPTIMER_1S_COUNT;
         __HAL_LPTIM_COMPARE_SET(&hlptim1, lastpoll_count);
//         printf("ancsync_time: %u     \r\n ",ancsync_time);
//         printf("current_slottimes: %u   ",current_slottimes);
//         printf("nextpoll_delaytime: %u   ",nextpoll_delaytime);
//         printf("current_count: %u   ",current_count);
//         printf("lastpoll_count: %u",lastpoll_count);
         
         tag_succ_times++;
         
            LED0_BLINK;
            
               memcpy(&anc_id_recv,&rx_buffer[ANCHOR_ID_IDX],2);
//               g_Tagdist[anc_id_recv]=   hex_dist;
@@ -444,7 +488,7 @@
               usart_send[14] = button;
               checksum = Checksum_u16(&usart_send[2],17);
               memcpy(&usart_send[19],&checksum,2);
               UART_PushFrame(usart_send,21);
         //      UART_PushFrame(usart_send,21);
               }
      //         memcpy(&Modbus_HoldReg[anc_id_recv*2],&hex_dist,4);
         /* Poll DW1000 until TX frame sent event set. See NOTE 8 below. */
@@ -473,10 +517,10 @@
//   deca_sleep(10);
}
   dwt_entersleep();
   if(tag_succ_times<g_com_map[MIN_REPORT_ANC_NUM])
   {
   //poll_timer +=time32_incr&0x7+3;
   }
//   if(tag_succ_times<g_com_map[MIN_REPORT_ANC_NUM])
//   {
//   //poll_timer +=time32_incr&0x7+3;
//   }
//HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
   /* Execute a delay between ranging exchanges. */