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 |   87 ++++++++++++++++++++++++++++---------------
 1 files changed, 57 insertions(+), 30 deletions(-)

diff --git a/Src/main.c b/Src/main.c
index 8772dc7..2f8c0ba 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -37,8 +37,8 @@
 
 /* Private define ------------------------------------------------------------*/
 /* USER CODE BEGIN PD */
-//#define DEBUG_MODE
-#define Period               (uint32_t) 32768
+#define DEBUG_MODE
+#define Period               (uint32_t) 32768 - 1
 #define Timeout              (uint32_t) 32768 - 1//(32768 - 1)
 /* USER CODE END PD */
 
@@ -77,13 +77,12 @@
 /* Private user code ---------------------------------------------------------*/
 /* USER CODE BEGIN 0 */
 uint32_t dev_id;
-extern uint8_t g_start_sync_flag;
 uint16_t heartbeat_timer,poll_timer,sync_timer;
 uint8_t aRxBuffer[1],group_id;
 
 void IdleTask(void)
 {
-	g_start_sync_flag=0;
+	
 			UART_CheckReceive();
 		UART_CheckSend();
 	
@@ -139,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;
@@ -165,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();
@@ -200,7 +230,7 @@
 uint32_t temp;
 extern uint8_t sleep_flag,m_bEUARTTxEn;
 uint16_t waitusart_timer;
-uint8_t bat_percent=0;
+uint8_t bat_percent=0,g_start_send_flag;
 //void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
 //{
 //  /* Set transmission flag: trasfer complete*/
@@ -244,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();
@@ -276,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();	
@@ -301,7 +333,7 @@
 		}
 			if(m_bEUARTTxEn==0&&waitusart_timer==0)
 			{	
-				HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
+			//	HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
 			}
 		
  
@@ -452,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 */
 
 }
@@ -605,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;
@@ -688,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