From 45511597788e2eea3abb66f9363682cf1ed96caa Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期一, 04 十一月 2019 20:02:02 +0800
Subject: [PATCH] 串口和DW ID正常

---
 源码/核心板/Src/application/dw_app.c |   82 ++++++++++++++++++++++++++++++-----------
 1 files changed, 60 insertions(+), 22 deletions(-)

diff --git "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c" "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c"
index 00e7907..f4f65eb 100644
--- "a/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c"
+++ "b/\346\272\220\347\240\201/\346\240\270\345\277\203\346\235\277/Src/application/dw_app.c"
@@ -64,6 +64,8 @@
 #define FINAL_MSG_FINAL_TX_TS_IDX 18
 #define FINAL_MSG_TS_LEN 4
 
+#define SYNC_SEQ_IDX    			5
+
 #define GROUP_ID_IDX   				0
 #define ANCHOR_ID_IDX    			1
 #define TAG_ID_IDX    				5
@@ -75,11 +77,13 @@
 #define BUTTON_IDX						16
 #define SEQUENCE_IDX					17
 //respose
-#define TIMECORRE             14
+#define ANCTIMEMS             14
+#define ANCTIMEUS             16
 
 #define POLL     					0x01
 #define RESPONSE 					0x02
 #define FINAL   					0x03
+#define SYNC   						0x04
 
 /*------------------------------------ Variables ------------------------------------------*/
 /* Default communication configuration. We use here EVK1000's default mode (mode 3). */
@@ -98,11 +102,12 @@
 
 /* Frames used in the ranging process. See NOTE 2 below. */
 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 rx_poll_msg[] = {0x00, 0x88, 0, 0xCA, 0xDE, 'W', 'A', 'V', 'E', 0x21, 0, 0};
-static uint8_t tx_resp_msg[18] = {0};
+static uint8_t tx_resp_msg[20] = {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. */
@@ -246,8 +251,9 @@
      * For initialisation, DW1000 clocks must be temporarily set to crystal speed. After initialisation SPI rate can be increased for optimum
      * performance. */
     Reset_DW1000();//重启DW1000 /* Target specific drive of RSTn line into DW1000 low for a period. */
+	Spi_ChangePrescaler(SPI_BaudRatePrescaler_64);	//设置为快速模式
     dwt_initialise(DWT_LOADUCODE);//初始化DW1000
-	Spi_ChangePrescaler(SPIx_PRESCALER_FAST);	//设置为快速模式
+	Spi_ChangePrescaler(SPI_BaudRatePrescaler_32);	//设置为快速模式
 
     /* Configure DW1000. See NOTE 6 below. */
     dwt_configure(&config);//配置DW1000
@@ -268,10 +274,12 @@
 	tx_poll_msg[MESSAGE_TYPE_IDX]=POLL;
 	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[TAG_ID_IDX], &dev_id, 4);
 	memcpy(&tx_final_msg[TAG_ID_IDX], &dev_id, 4);
 	memcpy(&tx_resp_msg[ANCHOR_ID_IDX], &dev_id, 4);
-	
+	memcpy(&tx_sync_msg[ANCHOR_ID_IDX], &dev_id, 4);
 }	
 uint16_t Checksum_u16(uint8_t* pdata, uint32_t len) 
 {
@@ -292,18 +300,31 @@
 	dwt_configuresleep(0x940, 0x7);
 	dwt_entersleep();
 }
+extern uint8_t g_start_send_flag;
+u8 g_start_sync_flag;
+void SyncPoll(u8 sync_seq)
+{
+	g_start_sync_flag=1;
+	dwt_forcetrxoff();
+	tx_sync_msg[SYNC_SEQ_IDX]=sync_seq;
+	dwt_writetxdata(sizeof(tx_sync_msg), tx_sync_msg, 0);//将Poll包数据传给DW1000,将在开启发送时传出去
+	dwt_writetxfctrl(sizeof(tx_sync_msg), 0);//设置超宽带发送数据长度
+	dwt_starttx(DWT_START_TX_IMMEDIATE);
+}
 uint16_t g_Resttimer;
 uint8_t result;
 u8 tag_succ_times=0;
 int32_t hex_dist;
 u16 checksum;
 int8_t tag_delaytime;
+extern uint16_t sync_timer;
+u16 tmp_time;
 void Tag_App(void)//发送模式(TAG标签)
 {
 	uint32_t frame_len;
 	uint32_t final_tx_time;
 	u32 start_poll;
-	u8 i;
+	u8 i,getsync_flag=0;
 	//LED0_ON;
 	dwt_forcetrxoff();
 	g_Resttimer=0;
@@ -313,7 +334,7 @@
 	tx_poll_msg[BATTARY_IDX] = Get_Battary();
 	tx_poll_msg[BUTTON_IDX] = !READ_KEY0;
 	tx_poll_msg[SEQUENCE_IDX] = frame_seq_nb++;
-
+	GPIO_WriteBit(GPIOA, GPIO_Pin_9, Bit_RESET);
 	for(i=0;i<g_com_map[MAX_REPORT_ANC_NUM];i++)
 	{
 	/* Write frame data to DW1000 and prepare transmission. See NOTE 7 below. */
@@ -360,15 +381,21 @@
 			poll_tx_ts = get_tx_timestamp_u64();										//获得POLL发送时间T1
 			resp_rx_ts = get_rx_timestamp_u64();										//获得RESPONSE接收时间T4
 			
-							if(i==0)
-								{
-								tag_delaytime=rx_buffer[TIMECORRE];
-							if(tag_delaytime>g_com_map[COM_INTERVAL]/2)
-							{
-							tag_delaytime=tag_delaytime-g_com_map[COM_INTERVAL];
-							}
-								}
-								
+			if(getsync_flag==0)
+			{
+				getsync_flag=1;
+			memcpy(&sync_timer,&rx_buffer[ANCTIMEMS],2);
+			memcpy(&tmp_time,&rx_buffer[ANCTIMEUS],2);
+			tmp_time=tmp_time+450;
+			if(tmp_time>999)
+			{
+				tmp_time-=999;
+				sync_timer++;
+				if(sync_timer>=1010)
+					{sync_timer=0;}
+			}
+			TIM3->CNT=tmp_time;
+		}
 			memcpy(&anchor_dist_last_frm[0], &rx_buffer[DIST_IDX], 4);
 			memcpy(&tx_final_msg[ANCHOR_ID_IDX], &rx_buffer[ANCHOR_ID_IDX], 4);
 			/* Compute final message transmission time. See NOTE 9 below. */
@@ -399,7 +426,7 @@
 					usart_send[4] = frame_seq_nb;//数据段长度
 					memcpy(&usart_send[5],&dev_id,2);
 					memcpy(&usart_send[7],&rx_buffer[ANCHOR_ID_IDX],2);
-					hex_dist = anchor_dist_last_frm[0];;
+					hex_dist = anchor_dist_last_frm[0]+(int16_t)g_com_map[DIST_OFFSET];
 					memcpy(&usart_send[9],&hex_dist,4);
 					usart_send[13] = battary;
 					usart_send[14] = button;
@@ -442,7 +469,8 @@
 	
 }
 int8_t correction_time;
-extern uint8_t g_start_send_flag;
+extern uint8_t sync_seq;
+
 void Anchor_App(void)
 {
 	uint32_t frame_len;
@@ -455,7 +483,7 @@
 	dwt_rxenable(0);//打开接收
 
 	/* Poll for reception of a frame or error/timeout. See NOTE 7 below. */
-	while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))&&!g_start_send_flag)//不断查询芯片状态直到接收成功或者出现错误
+	while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_ERR))&&!g_start_send_flag&&!g_start_sync_flag)//不断查询芯片状态直到接收成功或者出现错误
 	{ 
 		IdleTask();
 		g_Resttimer=0;
@@ -489,10 +517,10 @@
 		
 		if (rx_buffer[MESSAGE_TYPE_IDX] == POLL&&(anchor_type == rx_buffer[ANC_TYPE_IDX])) //判断是否是poll包数据
 		{
-			correction_time=tag_timer-tag_id_recv*3;
-							if(correction_time == g_com_map[COM_INTERVAL])
-								correction_time = 0;
-								tx_resp_msg[TIMECORRE] = correction_time;
+			tmp_time=TIM3->CNT;
+			memcpy(&tx_resp_msg[ANCTIMEMS],&sync_timer,2);
+			memcpy(&tx_resp_msg[ANCTIMEUS],&tmp_time,2);
+
 //							if(correction_time>10)
 //							{correction_time++;}
 							
@@ -608,6 +636,16 @@
 				/* Clear RX error events in the DW1000 status register. */
 				dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_ERR);
 			}
+		}else if(rx_buffer[MESSAGE_TYPE_IDX] == SYNC)
+		{
+			if(rx_buffer[SYNC_SEQ_IDX]<sync_seq)
+		//	if(rx_buffer[SYNC_SEQ_IDX]==2)
+			{
+				sync_seq=rx_buffer[SYNC_SEQ_IDX]+1;
+				TIM3->CNT = sync_seq*325%1000+15;
+				sync_timer = sync_seq*325/1000;
+				SyncPoll(sync_seq);
+			}
 		}
 	}
 	else

--
Gitblit v1.9.3