From b5cf79a0523697b760579eeac2e3ac60af62c55c Mon Sep 17 00:00:00 2001
From: WXK <287788329@qq.com>
Date: 星期二, 29 四月 2025 18:06:35 +0800
Subject: [PATCH] Merge branch 'L051K8U6-SS定位-临近-防撞基站' of http://47.108.70.204:60062/r/XRange_Tag into L051K8U6-SS定位-临近-防撞基站

---
 Src/main.c |  333 +++++++++++++++++++++++++++----------------------------
 1 files changed, 162 insertions(+), 171 deletions(-)

diff --git a/Src/main.c b/Src/main.c
index 2b26d4c..9208771 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -115,9 +115,15 @@
 uint16_t hardware_version,hardware_pici,hardware_type;
 u16 current_time;
 uint8_t state5v;
+uint8_t uart_time;
+uint8_t uart_init_flag;
 void IdleTask(void)
 {
-	
+	if(uart_init_flag)
+    {
+    UsartInit();
+    uart_init_flag=0;    
+    }
 			UART_CheckReceive();
 		UART_CheckSend();
 
@@ -133,31 +139,31 @@
 //	LED_TR_OFF;
 	current_time=GetLPTime();
 	//motor_state=2;
-
-    bat_percent=Get_Battary();
-	if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
-	{
-//		nomove_count = 0;
-		if(state5v==0)
-		{
-            UWB_RXOFF();
-			state5v=1;
-			UsartInit();
-		}
-		if(HAL_GPIO_ReadPin(PWR_ON_GPIO_Port,PWR_ON_Pin))
-		{
-			LED_LR_OFF;
-			LED_LG_ON;
-		}else{
-			LED_LR_ON;
-			LED_LG_OFF;		
-		}		
-	}else{
-		state5v=0;
-		LED_LR_OFF;
-		LED_LG_OFF;
-//	}
-}
+    
+//    bat_percent=Get_Battary();
+//	if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
+//	{
+////		nomove_count = 0;
+//		if(state5v==0)
+//		{
+//            UWB_RXOFF();
+//			state5v=1;
+//			UsartInit();
+//		}
+//		if(HAL_GPIO_ReadPin(PWR_ON_GPIO_Port,PWR_ON_Pin))
+//		{
+//			LED_LR_OFF;
+//			LED_LG_ON;
+//		}else{
+//			LED_LR_ON;
+//			LED_LG_OFF;		
+//		}		
+//	}else{
+//		state5v=0;
+//		LED_LR_OFF;
+//		LED_LG_OFF;
+////	}
+//}
 	if(g_com_map[CNT_UPDATE]==1)
 	{
 	uint32_t result = 0;
@@ -239,9 +245,9 @@
 	hardware_pici= STMFLASH_ReadHalfWord(FLASH_HARDWARE_VERSION_MAP+2);
 	hardware_type= STMFLASH_ReadHalfWord(FLASH_HARDWARE_VERSION_MAP+4);
 	//deca_sleep(1000);
-	g_com_map[VERSION] = (2<<8)|13;
+	g_com_map[VERSION] = (1<<8)|2;
     
-//	g_com_map[DEV_ID]=0x7;
+//	g_com_map[DEV_ID]=0x0011;
 #ifdef DEBUG_MODE
 //	g_com_map[DEV_ROLE]=1;
 	//	g_com_map[DEV_ID]=0x4009;
@@ -369,6 +375,7 @@
 }
 
 uint8_t userkey_num;
+extern u32 id,error_times;
 /* USER CODE END 0 */
 
 /**
@@ -398,22 +405,21 @@
   /* USER CODE END SysInit */
 
   /* Initialize all configured peripherals */
-  MX_IWDG_Init();
   MX_GPIO_Init();
-  MX_LPTIM1_Init();
   MX_DMA_Init();
   MX_USART1_UART_Init();
   MX_SPI1_Init();
   MX_ADC_Init();
-  
+  MX_LPTIM1_Init();
   MX_TIM2_Init();
-  
+  MX_IWDG_Init();
   /* USER CODE BEGIN 2 */
-	LED_LG_ON;
+//	LED_LG_ON;
 	Program_Init();
-	Accelerometer_Init();	
+//	Accelerometer_Init();	
    // BarInit();
 	Dw1000_Init();
+    id =  dwt_readdevid() ;
 	Dw1000_App_Init();
 	HardWareTypeDiffConfig();
 //	dwt_configuresleep(DWT_PRESRV_SLEEP | DWT_CONFIG, DWT_WAKE_CS | DWT_WAKE_WK| DWT_SLP_EN);
@@ -422,14 +428,14 @@
 //    dwt_setinterrupt(  DWT_INT_RFCG | (DWT_INT_ARFE | DWT_INT_RFSL | DWT_INT_SFDT | DWT_INT_RPHE | DWT_INT_RFCE | DWT_INT_RFTO | DWT_INT_RXPTO), 1);
 //    dwt_setrxtimeout(0);//设定接收超时时间,0位没有
 //    dwt_rxenable(0);
-    if(uwb_onflag)
-    {
-    UWB_RXON();
-    }
-    else
-    {
-    UWB_RXOFF();
-    }
+//    if(uwb_onflag)
+//    {
+//    UWB_RXON();
+//    }
+//    else
+//    {
+//    UWB_RXOFF();
+//    }
 	DW_DISABLE;
 	 if(HAL_UART_Receive_DMA(&huart1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE))
 	{
@@ -455,13 +461,13 @@
 //		{
 //		MODBUS_Poll();
 //		}
-if(m_bEUARTTxEn==0&&(motor_state==0||motor_state==3))
-			{	
-#ifndef DEBUG_MODE	
-				if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
-				HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
-#endif
-			}
+//if(m_bEUARTTxEn==0&&(motor_state==0||motor_state==3))
+//			{	
+//#ifndef DEBUG_MODE	
+//				if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
+//				HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
+//#endif
+//			}
 		if(g_start_send_flag/*&&uwb_onflag*/)
 	{ static uint16_t blink_count=0;
 		g_start_send_flag = 0;	
@@ -474,22 +480,28 @@
 //		}
 //		LED_TR_BLINK;
 			__disable_irq();
-		if(bat_percent>15)
-		{	LED_TB_ON;
+//		if(bat_percent>15)
+//		{	
+////            LED_TB_ON;
+//			Tag_App();
+////			LED_TB_OFF;
+//		}
+//        else
+//            {
+//			LED_TR_ON;
 			Tag_App();
-			LED_TB_OFF;
-		}else{
-			LED_TR_ON;
-			Tag_App();
-			LED_TR_OFF;		
-		}
+//        HAL_IWDG_Refresh(&hiwdg);
+//                id =  dwt_readdevid() ;
+//                IdleTask();	
+//			LED_TR_OFF;		
+//		}
 			__enable_irq();
 		//LED0_BLINK;		
 		IdleTask();	
-		if(waitusart_timer>0)
-		{
-			waitusart_timer--;
-		}
+//		if(waitusart_timer>0)
+//		{
+//			waitusart_timer--;
+//		}
 			
 	}else{
 	IdleTask();	
@@ -857,36 +869,13 @@
   __HAL_RCC_GPIOB_CLK_ENABLE();
 
   /*Configure GPIO pin Output Level */
-  HAL_GPIO_WritePin(GPIOA, SCL_Pin|SDA_Pin|SPI_CS_Pin, GPIO_PIN_SET);
-
-  /*Configure GPIO pin Output Level */
   HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin|PWR_CTL_Pin, GPIO_PIN_SET);
 
   /*Configure GPIO pin Output Level */
-  HAL_GPIO_WritePin(GPIOB, LED1_G_Pin|LED1_R_Pin|LED2_R_Pin, GPIO_PIN_RESET);
+  HAL_GPIO_WritePin(GPIOA, DW_WKUP_Pin|DW_CTRL_Pin|SPI_CS_Pin, GPIO_PIN_RESET);
 
   /*Configure GPIO pin Output Level */
-  HAL_GPIO_WritePin(GPIOA, DW_WKUP_Pin|DW_CTRL_Pin|LED2_G_Pin, GPIO_PIN_RESET);
-
-  /*Configure GPIO pin : PA0 */
-  GPIO_InitStruct.Pin = GPIO_PIN_0;
-  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
-  GPIO_InitStruct.Pull = GPIO_NOPULL;
-  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
-  /*Configure GPIO pins : SCL_Pin SDA_Pin */
-  GPIO_InitStruct.Pin = SCL_Pin|SDA_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
-  GPIO_InitStruct.Pull = GPIO_NOPULL;
-  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
-  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
-
-  /*Configure GPIO pin : SPI_CS_Pin */
-  GPIO_InitStruct.Pin = SPI_CS_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
-  GPIO_InitStruct.Pull = GPIO_NOPULL;
-  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
-  HAL_GPIO_Init(SPI_CS_GPIO_Port, &GPIO_InitStruct);
+  HAL_GPIO_WritePin(GPIOB, SPI_RST_Pin|SPI_WAKEUP_Pin, GPIO_PIN_RESET);
 
   /*Configure GPIO pin : BAT_MEAS_GND_Pin */
   GPIO_InitStruct.Pin = BAT_MEAS_GND_Pin;
@@ -907,30 +896,24 @@
   GPIO_InitStruct.Pull = GPIO_PULLDOWN;
   HAL_GPIO_Init(INPUT_5V_GPIO_Port, &GPIO_InitStruct);
 
-  /*Configure GPIO pins : LED1_G_Pin LED1_R_Pin LED2_R_Pin PWR_CTL_Pin */
-  GPIO_InitStruct.Pin = LED1_G_Pin|LED1_R_Pin|LED2_R_Pin|PWR_CTL_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
-  GPIO_InitStruct.Pull = GPIO_NOPULL;
-  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
-  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
-
-  /*Configure GPIO pins : DW_WKUP_Pin DW_CTRL_Pin LED2_G_Pin */
-  GPIO_InitStruct.Pin = DW_WKUP_Pin|DW_CTRL_Pin|LED2_G_Pin;
+  /*Configure GPIO pins : DW_WKUP_Pin DW_CTRL_Pin SPI_CS_Pin */
+  GPIO_InitStruct.Pin = DW_WKUP_Pin|DW_CTRL_Pin|SPI_CS_Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
-  /*Configure GPIO pin : PWR_ON_Pin */
-  GPIO_InitStruct.Pin = PWR_ON_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
-  GPIO_InitStruct.Pull = GPIO_PULLUP;
-  HAL_GPIO_Init(PWR_ON_GPIO_Port, &GPIO_InitStruct);
-
-  /*Configure GPIO pin : PB8 */
-  GPIO_InitStruct.Pin = GPIO_PIN_8;
+  /*Configure GPIO pin : PB6 */
+  GPIO_InitStruct.Pin = GPIO_PIN_6;
   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
+  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
+
+  /*Configure GPIO pins : SPI_RST_Pin SPI_WAKEUP_Pin PWR_CTL_Pin */
+  GPIO_InitStruct.Pin = SPI_RST_Pin|SPI_WAKEUP_Pin|PWR_CTL_Pin;
+  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+  GPIO_InitStruct.Pull = GPIO_NOPULL;
+  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
   /* EXTI interrupt init*/
@@ -973,6 +956,8 @@
 extern uint8_t tx_near_msg[80],stationary_flag,gotosleep_flag;
 float key_keeptime;
 extern float motor_ontime,button_delay;
+uint8_t uart_time;
+uint8_t uart_init_flag;
 void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
 {
 	static uint8_t i=0;
@@ -984,75 +969,81 @@
     if(button_delay<1)
     button_delay+=1/(float)tag_frequency;
 	nomove_count+=1/(float)tag_frequency;
-	if(motor_keeptime>0)
-	{	
-        motor_keeptime-=1/(float)tag_frequency;
-	}
-    if(motor_keeptime<0)
-    {motor_keeptime = 0;}
-	if(active_flag==0)
-	{
-	if(!GET_USERKEY)
-	{
-		LED_LG_ON;
-			key_keeptime+=1/(float)tag_frequency;
-		if(key_keeptime>3)
-		{
-			u8 i=250,keystate=1;
-			while(i--)
-			{
-				if(i%10==0)
-				{
-				LED_LG_BLINK;
-				}
-				if(GET_USERKEY)
-				{keystate = 0;}
-			if(keystate==0&&!GET_USERKEY)
-			{
-				parameter_init();
-				g_com_map[ACTIVE_INDEX] = 1;
-				save_com_map_to_flash();
-				delay_ms(100);
-				SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
-			}
-			delay_ms(10);
-			}		
-		}
-	}else{
-		LED_LG_OFF;
-		key_keeptime=0;
-	}
-		
-	}
+    uart_time++;
+    if(uart_time>60)
+    {
+    uart_init_flag=1;
+        uart_time=0;
+    }
+//	if(motor_keeptime>0)
+//	{	
+//        motor_keeptime-=1/(float)tag_frequency;
+//	}
+//    if(motor_keeptime<0)
+//    {motor_keeptime = 0;}
+//	if(active_flag==0)
+//	{
+//	if(!GET_USERKEY)
+//	{
+////		LED_LG_ON;
+//			key_keeptime+=1/(float)tag_frequency;
+//		if(key_keeptime>3)
+//		{
+//			u8 i=250,keystate=1;
+//			while(i--)
+//			{
+//				if(i%10==0)
+//				{
+////				LED_LG_BLINK;
+//				}
+//				if(GET_USERKEY)
+//				{keystate = 0;}
+//			if(keystate==0&&!GET_USERKEY)
+//			{
+//				parameter_init();
+//				g_com_map[ACTIVE_INDEX] = 1;
+//				save_com_map_to_flash();
+//				delay_ms(100);
+//				SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
+//			}
+//			delay_ms(10);
+//			}		
+//		}
+//	}else{
+////    LED_LG_OFF;
+//		key_keeptime=0;
+//	}
+//		
+//	}
 
-	if(!GET_USERKEY)
-	{
-		key_keeptime+=1/(float)tag_frequency;
-		if(key_keeptime>=KEY_KEEPRESET_TIME)
-		{
-			SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; 
-		}
-	}else{
-		key_keeptime=0;
-	}
-	if(nomove_count>g_com_map[STATIONARY_TIME])
-	{
-		stationary_flag = 1;
-	}else{
-		stationary_flag = 0;
-	}
-if(nomove_count>=nomovesleep_time-1&&imu_enable)
-	{
-		gotosleep_flag=1;
-	}else{
-		gotosleep_flag=0;
-	}
-//#ifndef DEBUG_MODE    
-	if(nomove_count>g_com_map[NOMOVESLEEP_TIME]&&imu_enable)
-	{
-		mcu_sleep();
-	}
-//#endif
+//	if(!GET_USERKEY)
+//	{
+//		key_keeptime+=1/(float)tag_frequency;
+//		if(key_keeptime>=KEY_KEEPRESET_TIME)
+//		{
+//			SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; 
+//		}
+//	}else{
+//		key_keeptime=0;
+//	}
+//	if(nomove_count>g_com_map[STATIONARY_TIME])
+//	{
+//		stationary_flag = 1;
+//	}else{
+//		stationary_flag = 0;
+//	}
+//if(nomove_count>=nomovesleep_time-1&&imu_enable)
+//	{
+//		gotosleep_flag=1;
+//	}else{
+//		gotosleep_flag=0;
+//	}
+////#ifndef DEBUG_MODE    
+//	if(nomove_count>g_com_map[NOMOVESLEEP_TIME]&&imu_enable)
+//	{
+//		mcu_sleep();
+//	}
+////#endif
 }
 /* USER CODE END 4 */
 

--
Gitblit v1.9.3