From 3e165b6f13bfaf66385d857819cacff6e4d6f37c Mon Sep 17 00:00:00 2001
From: zhyinch <zhyinch@gmail.com>
Date: 星期六, 20 六月 2020 22:31:03 +0800
Subject: [PATCH] 时间同步完成,多基站有问题,时间不对

---
 Src/main.c |   80 +++++++++++++++++++++++++++-------------
 1 files changed, 54 insertions(+), 26 deletions(-)

diff --git a/Src/main.c b/Src/main.c
index 2d854a7..2f8c0ba 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -37,7 +37,7 @@
 
 /* Private define ------------------------------------------------------------*/
 /* USER CODE BEGIN PD */
-//#define DEBUG_MODE
+#define DEBUG_MODE
 #define Period               (uint32_t) 32768 - 1
 #define Timeout              (uint32_t) 32768 - 1//(32768 - 1)
 /* USER CODE END PD */
@@ -138,9 +138,39 @@
 
 uint16_t tyncpoll_time,lpsettime;
 uint16_t slottime,max_slotpos;
-uint16_t lastpoll_count,interval_count,slot_startcount,tag_frequency;
+uint16_t lastpoll_count,interval_count,slot_startcount,tag_frequency,lastpoll_time,current_time;
 extern uint8_t module_power;
 uint8_t imu_enable,motor_enable;	
+u16 GetLPTime(void)
+{
+	u16 count=HAL_LPTIM_ReadCounter(&hlptim1);
+ return count*LPTIMER_LSB/1000;
+}	
+u16 total_slotnum,current_slotnum;
+extern int32_t offsettimeus;
+
+void SetNextPollTime(u16 time)
+{
+  int32_t lpcount,lptime,target_time;
+	
+	time=0;
+	if(current_slotnum>=total_slotnum)
+		current_slotnum-=total_slotnum;
+	
+	target_time=((current_slotnum*g_com_map[COM_INTERVAL])+time)*1000;
+	//if(target_time<90000)
+	{
+		lptime=target_time-offsettimeus;
+		lpcount = lptime/LPTIMER_LSB;
+	if(lpcount>LPTIMER_1S_COUNT)
+		lpcount-=LPTIMER_1S_COUNT;
+	if(lpcount<0)
+	{
+	lpcount+=LPTIMER_1S_COUNT;
+	}
+	__HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount);
+}
+}
 void Program_Init(void)
 {	float temp;
 	uint16_t temp2;
@@ -164,14 +194,15 @@
 	
 	group_id=g_com_map[GROUP_ID];
 	dev_id = g_com_map[DEV_ID];
-	slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]*4/3);
+	slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]/4)+4;
 	max_slotpos=g_com_map[COM_INTERVAL]/slottime;
 	tyncpoll_time=(g_com_map[DEV_ID]%max_slotpos)*slottime;	
-	
-	slot_startcount=(float)(tyncpoll_time+10)*1000/LPTIMER_LSB;
-	lastpoll_count=slot_startcount;
-	interval_count=(float)g_com_map[COM_INTERVAL]*1000/LPTIMER_LSB;
+	slot_startcount=tyncpoll_time*1000/LPTIMER_LSB;
+	lastpoll_time=tyncpoll_time;
   tag_frequency = 1000/g_com_map[COM_INTERVAL];
+	total_slotnum = 1000/g_com_map[COM_INTERVAL];
+	current_slotnum=1;
+	current_time=GetLPTime();
 		if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, slot_startcount) != HAL_OK)  //system time is 1010ms   1 puls=30.518us
   {
     Error_Handler();
@@ -243,7 +274,7 @@
   /* Initialize all configured peripherals */
   MX_GPIO_Init();
   MX_LPTIM1_Init();
-  MX_USART1_UART_Init();
+ // MX_USART1_UART_Init();
   MX_SPI1_Init();
   MX_ADC_Init();
   MX_DMA_Init();
@@ -254,7 +285,7 @@
 	Dw1000_App_Init();
 	
 	dwt_configuresleep(DWT_PRESRV_SLEEP | DWT_CONFIG, DWT_WAKE_CS | DWT_WAKE_WK| DWT_SLP_EN);
-//	dwt_entersleep();
+	dwt_entersleep();
 	waitusart_timer=1;
 	DW_DISABLE;
 //	HAL_Delay(2000);
@@ -275,22 +306,24 @@
 	{ static uint16_t blink_count=0;
 		SystemClock_Config();
 		g_start_send_flag = 0;	
+		
 //		if(current_count>slot_startcount&&current_count<slot_startcount+interval_count)
 //		{
 //		HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_SET);
 //		}else{
 //		HAL_GPIO_WritePin(LED0_GPIO, GPIO_PIN_9, GPIO_PIN_RESET);
 //		}
+		LED_TR_BLINK;
 		
 			bat_percent=Get_Battary();
 		if(bat_percent>15)
-		{	LED0_ON;
+		{//	LED0_ON;
 			Tag_App();
-			LED0_OFF;
+		//	LED0_OFF;
 		}else{
-			LED_LR_ON;
+			//LED_LR_ON;
 			Tag_App();
-			LED_LR_OFF;		
+		//	LED_LR_OFF;		
 		}
 		//LED0_BLINK;		
 		IdleTask();	
@@ -451,10 +484,10 @@
     Error_Handler();
   }
   /* USER CODE BEGIN LPTIM1_Init 2 */
-	if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, Period, Timeout) != HAL_OK)
-  {
-    Error_Handler();
-  }
+//	if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, Period, Timeout) != HAL_OK)
+//  {
+//    Error_Handler();
+//  }
   /* USER CODE END LPTIM1_Init 2 */
 
 }
@@ -604,7 +637,7 @@
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
   /*Configure GPIO pin : SPI_CS_Pin */
-  GPIO_InitStruct.Pin = SPI_CS_Pin;
+  GPIO_InitStruct.Pin = SPI_CS_Pin|GPIO_PIN_9;
   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
@@ -687,17 +720,12 @@
 	static uint8_t i=0;
 	i++;
 	g_start_send_flag=1;
-	lastpoll_count+=interval_count;
-	if(lastpoll_count>LPTIMER_1S_COUNT-30)
-	{
-		lastpoll_count = slot_startcount; 
-	}
-	__HAL_LPTIM_COMPARE_SET(&hlptim1, lastpoll_count);
-	
+	current_slotnum++;
+	SetNextPollTime(tyncpoll_time);
 	nomove_count+=(float)g_com_map[COM_INTERVAL]/1000;
 	if(nomove_count>g_com_map[NOMOVESLEEP_TIME]&&imu_enable)
 	{
-		mcu_sleep();
+	//	mcu_sleep();
 	}
 }
 /* USER CODE END 4 */

--
Gitblit v1.9.3