From 53bb6e5ba5b3d3c1b0088a0e7e58f37cbba90cac Mon Sep 17 00:00:00 2001
From: WXK <287788329@qq.com>
Date: 星期二, 26 十二月 2023 15:45:54 +0800
Subject: [PATCH] 1

---
 Src/main.c |  612 +++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 459 insertions(+), 153 deletions(-)

diff --git a/Src/main.c b/Src/main.c
index 1184712..04306c4 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -30,10 +30,16 @@
 #include "TrackingDiff.h"
 #include "dw_app.h"
 #include "Flash.h"
+#include "user.h"
+#include "sx126x-board.h"
+#include "string.h"
+#include "stdio.h"
+#include "delay.h"
+#include <stdlib.h>
 #define NSH1 0x0001
 #define GP   0x0002
 #define FLASH_HARDWARE_VERSION_MAP		(uint32_t)0x08004F00    //硬件版本号和批次信息位置
-//#define DEBUG_MODE
+#define DEBUG_MODE
 /* USER CODE END Includes */
 
 /* Private typedef -----------------------------------------------------------*/
@@ -61,6 +67,8 @@
 
 SPI_HandleTypeDef hspi1;
 
+TIM_HandleTypeDef htim2;
+
 UART_HandleTypeDef huart1;
 DMA_HandleTypeDef hdma_usart1_rx;
 DMA_HandleTypeDef hdma_usart1_tx;
@@ -75,9 +83,10 @@
 static void MX_LPTIM1_Init(void);
 static void MX_USART1_UART_Init(void);
 static void MX_SPI1_Init(void);
-void MX_ADC_Init(void);
+static void MX_ADC_Init(void);
 static void MX_DMA_Init(void);
 static void MX_IWDG_Init(void);
+static void MX_TIM2_Init(void);
 /* USER CODE BEGIN PFP */
 
 /* USER CODE END PFP */
@@ -91,10 +100,15 @@
 extern u8 motor_state;
 uint16_t tyncpoll_time,lpsettime;
 uint16_t slottime,max_slotpos;
-uint16_t lastpoll_count,interval_count,slot_startcount,tag_frequency,lastpoll_time,current_time;
+uint16_t lastpoll_count,interval_count,slot_startcount,tag_frequency,lastpoll_time;
 extern uint8_t module_power;
 extern float nomove_count;
 uint8_t imu_enable,motor_enable;	
+uint32_t lp_time;
+extern  uint32_t lora_txtime_old;
+uint8_t anjian_flag;
+uint8_t  stationary_flag;
+uint16_t stationary_num;
 u16 GetLPTime(void)
 {
 	u16 count=HAL_LPTIM_ReadCounter(&hlptim1);
@@ -135,13 +149,25 @@
 }
 uint16_t hardware_version,hardware_pici,hardware_type;
 uint8_t state5v = 1;
+extern uint8_t du_flag;
+extern uint8_t xie_flag;
 float motor_keeptime;
 void IdleTask(void)
-{u16 current_time;
+{
 //if(huart1.Instance->ISR&USART_ISR_FE)
 //	{
 //	SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
-//	}	
+////	}
+//    if(du_flag==1)
+//    {
+//    du_flag=0;
+//        
+//    }
+//    if(xie_flag==1)
+//    {
+//    xie_flag=0;
+//    memcpy((uint8_t*)&g_com_map + pack_index, mUsartReceivePack, pack_datalen);    
+//    }
 	if(HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))
 	{
         	
@@ -156,11 +182,11 @@
 		}
 		if(bat_percent>99)
 		{
-			LED_LR_OFF;
-			LED_LG_ON;
+			LED2_TR_OFF;
+			LED2_TB_ON;	
 		}else{
-			LED_LR_ON;
-			LED_LG_OFF;		
+			LED2_TR_ON;
+			LED2_TB_OFF;
 		}		
 	}else{
         if(state5v==1)
@@ -168,8 +194,8 @@
             state5v=0;
             UsartDeInit();
         }
-		LED_LR_OFF;
-		LED_LG_OFF;
+		LED2_TR_OFF;
+		LED2_TB_OFF;
 //	}
 }
         if(g_com_map[MAP_SIGN_INDEX]!=0x55AA||g_com_map[COM_INTERVAL]==0)
@@ -182,11 +208,11 @@
 
 u16 total_slotnum,current_slotnum;
 extern int32_t offsettimeus;
-u16 last_lpcount = 0;
+//u16 last_lpcount = 0;
 void SetNextPollTime(u16 time)
 {
-  int32_t lpcount,lptime,target_time;
-
+  int32_t lpcount1,lptime1,target_time1;
+int32_t last_lpcount1;
 //	if(current_slotnum>=total_slotnum)
 //		current_slotnum-=total_slotnum;
 //	
@@ -201,16 +227,106 @@
 //	{
 //	lpcount+=LPTIMER_1S_COUNT;
 //	}
-    lpcount = last_lpcount+32768/tag_frequency-time*LPTIMER_LSB;
-    while(lpcount>LPTIMER_1S_COUNT)
+    last_lpcount1=HAL_LPTIM_ReadCounter(&hlptim1);
+    lpcount1 = last_lpcount1+32.768*time;
+    while(lpcount1>LPTIMER_1S_COUNT)
     {
-        lpcount-=LPTIMER_1S_COUNT;
+        lpcount1-=LPTIMER_1S_COUNT;
     }
-    last_lpcount = lpcount;
-	__HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount);
+//    last_lpcount = lpcount;
+	__HAL_LPTIM_COMPARE_SET(&hlptim1, lpcount1+32768);
 
 }
+
+/* 返回ch字符在sign数组中的序号 */
+int getIndexOfSigns(char ch)
+{
+    if(ch >= '0' && ch <= '9')
+    {
+        return ch - '0';
+    }
+    if(ch >= 'A' && ch <='F') 
+    {
+        return ch - 'A' + 10;
+    }
+    if(ch >= 'a' && ch <= 'f')
+    {
+        return ch - 'a' + 10;
+    }
+    return -1;
+}
+/* 十六进制数转换为十进制数 */
+long hexToDec(char *source)
+{
+    long sum = 0;
+    long t = 1;
+    int i, len;
+ 
+    len = strlen(source);
+    for(i=len-1; i>=0; i--)
+    {
+        sum += t * getIndexOfSigns(*(source + i));
+        t *= 16;
+    }  
+ 
+    return sum;
+}
+    
+u32 Loratx_frequency;
 u8 active_flag,nomovesleeptime;
+void GetLoratx_frequency(void)
+{
+//if(g_com_map[NEARBASE_ID1]==0x470)
+//{Loratx_frequency=470000000;}
+//if(g_com_map[NEARBASE_ID1]==0x471)
+//{Loratx_frequency=471000000;}
+//if(g_com_map[NEARBASE_ID1]==0x472)
+//{Loratx_frequency=472000000;}
+//if(g_com_map[NEARBASE_ID1]==0x473)
+//{Loratx_frequency=473000000;}
+//if(g_com_map[NEARBASE_ID1]==0x474)
+//{Loratx_frequency=474000000;}
+//if(g_com_map[NEARBASE_ID1]==0x475)
+//{Loratx_frequency=475000000;}
+//if(g_com_map[NEARBASE_ID1]==0x476)
+//{Loratx_frequency=476000000;}
+//if(g_com_map[NEARBASE_ID1]==0x477)
+//{Loratx_frequency=477000000;}
+//if(g_com_map[NEARBASE_ID1]==0x478)
+//{Loratx_frequency=478000000;}
+//if(g_com_map[NEARBASE_ID1]==0x479)
+//{Loratx_frequency=479000000;}
+//if(g_com_map[NEARBASE_ID1]==0x480)
+//{Loratx_frequency=480000000;}
+//if(g_com_map[NEARBASE_ID1]==0x481)
+//{Loratx_frequency=481000000;}
+//if(g_com_map[NEARBASE_ID1]==0x482)
+//{Loratx_frequency=482000000;}
+//if(g_com_map[NEARBASE_ID1]==0x483)
+//{Loratx_frequency=483000000;}
+//if(g_com_map[NEARBASE_ID1]==0x484)
+//{Loratx_frequency=484000000;}
+//if(g_com_map[NEARBASE_ID1]==0x485)
+//{Loratx_frequency=485000000;}
+//if(g_com_map[NEARBASE_ID1]==0x486)
+//{Loratx_frequency=486000000;}
+//if(g_com_map[NEARBASE_ID1]==0x487)
+//{Loratx_frequency=487000000;}
+//if(g_com_map[NEARBASE_ID1]==0x488)
+//{Loratx_frequency=488000000;}
+//if(g_com_map[NEARBASE_ID1]==0x489)
+//{Loratx_frequency=489000000;}
+//if(g_com_map[NEARBASE_ID1]==0x490)
+//{Loratx_frequency=490000000;}
+Loratx_frequency=470000000;
+srand(HAL_LPTIM_ReadCounter(&hlptim1));
+        Loratx_frequency=((rand()%5)+470)*1000000; 
+        if(470000000<=Loratx_frequency&&Loratx_frequency<480000000)
+        g_com_map[NEARBASE_ID1]=1136+(Loratx_frequency/1000000-470);
+        if(480000000<=Loratx_frequency&&Loratx_frequency<490000000)
+        g_com_map[NEARBASE_ID1]=1152+(Loratx_frequency/1000000-480);
+Loratx_frequency=470000000;
+}
 void Program_Init(void)
 {	float temp;
 	uint16_t temp2;
@@ -221,8 +337,8 @@
 	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] = (1<<8)|67;
-    
+	g_com_map[VERSION] = (1<<8)|0;
+//    g_com_map[DEV_ID]=1158;
     #ifdef DEBUG_MODE
    // g_com_map[GROUP_ID]=21;
 //    		g_com_map[DEV_ID]=4;
@@ -230,14 +346,16 @@
     //	g_com_map[MAX_REPORT_ANC_NUM]=1;
     //	g_com_map[NOMOVESLEEP_TIME]=10;
     #endif
-    g_com_map[GROUP_ID]=3;
-    g_com_map[COM_INTERVAL] = 1000;
+    g_com_map[GROUP_ID]=5;
+//    g_com_map[COM_INTERVAL] = 1000;
+    if(g_com_map[COM_INTERVAL]<1000)
+    {g_com_map[COM_INTERVAL] = 1000;}
     active_flag = g_com_map[ACTIVE_INDEX];
     module_power = g_com_map[POWER];
     g_com_map[IMU_ENABLE] = 0;
 	imu_enable=g_com_map[IMU_ENABLE];
 	motor_enable=g_com_map[MOTOR_ENABLE];
-	
+
 	group_id=g_com_map[GROUP_ID];
 	dev_id = g_com_map[DEV_ID];
 	slottime=ceil((double)g_com_map[MAX_REPORT_ANC_NUM]*SLOT_SCALE)+3;
@@ -257,7 +375,7 @@
  //   g_com_map[COM_INTERVAL]=100;
 		if(g_com_map[COM_INTERVAL]==0)
 	{
-	  g_com_map[COM_INTERVAL]=100;
+	  g_com_map[COM_INTERVAL]=1000;
 	}
 	 if(module_power>67)
 		{module_power=67;}
@@ -265,13 +383,14 @@
 		{module_power=0;}
 
 	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
+//	current_time=GetLPTime();
+		if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, LPTIMER_1S_COUNT, 32767) != HAL_OK)  //system time is 1010ms   1 puls=30.518us
   {
     Error_Handler();
   }
+  
 //g_com_map[NOMOVESLEEP_TIME]=5;
-    printf("固件版本: 免布线基站 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
+    printf("固件版本: 免布线基站LORA版本 V%d.%d. \r\n",g_com_map[VERSION]>>8,g_com_map[VERSION]&0xff);
 	memcpy(g_com_map2,g_com_map,COM_MAP_SIZE);
 //	printf("DEVICE PAIRID: %d .\r\n",g_com_map[PAIR_ID]);
 //	printf("DEVICE ALARM DISTANCE: 1.%d 2.%d 3.%d .\r\n",g_com_map[ALARM_DISTANCE1],g_com_map[ALARM_DISTANCE2],g_com_map[ALARM_DISTANCE3]);
@@ -339,6 +458,130 @@
     HAL_ADC_DeInit(&hadc);
     HAL_ADCEx_DisableVREFINT();
 }
+uint16_t irq_num;
+void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
+{
+    if(GPIO_Pin == GPIO_PIN_3)
+    {
+        SystemClock_Config();
+        HAL_GPIO_WritePin(GPIOA, GPIO_PIN_15, GPIO_PIN_SET);
+//        HAL_GPIO_WritePin(RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, GPIO_PIN_RESET); //片选lora有效
+//        l_bIsVibration = HIDO_TRUE;
+//        nomove_time=0;
+//        printf("RXD :77777\r\n");
+        irq_num++;
+        RadioIrqProcess();
+    }
+    if(GPIO_Pin == GPIO_PIN_1)//按键
+    {
+      anjian_flag=1;
+    }
+    if(GPIO_Pin == GPIO_PIN_0)//加速度
+    {
+     stationary_flag =0;
+        stationary_num=0;
+    }
+}
+void Uwb_Lora_Switch(uint8_t uwb, uint8_t lora)
+{
+if(uwb)
+HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET);
+else
+HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET);    
+
+if(lora)
+HAL_GPIO_WritePin(GPIOB, RADIO_NSS_Pin, GPIO_PIN_SET);
+else
+HAL_GPIO_WritePin(GPIOB, RADIO_NSS_Pin, GPIO_PIN_RESET); 
+}
+extern void Lora_tx_zubao( void );
+extern void Lora_tx_mode( void );
+extern     uint8_t Lora_tx_ancnum;
+extern   u8 lora_seq_nb2;
+extern uint16_t lora_yingda_num;
+uint8_t lora_tx_flag;
+uint8_t lora_txanchor_power_flag;
+extern uint16_t Lora_tx_ancid[50];
+extern uint16_t Lora_tx_ancdist[50];
+extern uint8_t Lora_tx_anc_electricity[50];
+u8 lora_seq_nb1;
+static uint8_t send_frame1[EUART_RX_BUF_SIZE];
+uint8_t data_length1;
+void LoraSendComMap(uint8_t data_length, uint8_t index)
+{
+	uint16_t checksum = 0;
+	send_frame1[0] = 0x55;
+	send_frame1[1] = 0xAA;
+	send_frame1[2] = 0x39;
+	send_frame1[3] = data_length+5;
+	send_frame1[4] = 3;
+	send_frame1[5] = index;
+	send_frame1[6] = data_length;
+	memcpy(&send_frame1[7], &g_com_map[index], data_length);
+	for(int i = 0; i<(data_length+5); i++)
+	{
+		checksum += send_frame1[2+i];
+	}
+	checksum = Checksum_u16(&send_frame1[2],5+data_length);
+	memcpy(&send_frame1[7+data_length],&checksum,2);
+	data_length1=data_length+9;
+//	UART_PushFrame(send_frame1, data_length+9);	
+}
+uint8_t no_yingdaflag;
+void Lora_Tx_Poll()
+{
+
+    if(lora_tx_flag)
+    {  
+        lora_tx_flag=0;
+       if(du_flag==1)
+       {    
+    Lora47X_Init();    
+    du_flag=0;
+    Radio.Send( send_frame1, data_length1);    
+        no_yingdaflag=1;
+       }
+       else
+       {
+        Lora47X_Init();
+        if(bat_percent>20)
+        {LED_TR_ON;}
+        else
+        {LED_TB_ON;}
+        
+    lora_seq_nb2++;
+    Lora_tx_zubao();
+//    Lora_tx_mode();            
+//        if(usart_send[2]==0x99)
+//        {LED_TB_ON;}
+    Radio.Send( usart_send, 15+Lora_tx_ancnum*4);
+    Lora_tx_ancnum=0;    
+    memset(Lora_tx_ancdist,0,sizeof(Lora_tx_ancdist));
+    memset(Lora_tx_ancid,0,sizeof(Lora_tx_ancid));
+    anjian_flag=0;
+       }
+    }
+     
+}
+void MX_ADC1_Init()
+{
+MX_ADC_Init();
+}
+extern uint16_t Lora_rece_error;
+extern uint16_t Beepchixutime;
+void Beep_Off()
+{
+    __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, 0);
+    HAL_TIM_PWM_Stop(&htim2, TIM_CHANNEL_3);
+    HAL_TIM_Base_Stop_IT(&htim2);
+}
+void Beep_On()
+{
+    HAL_TIM_Base_Start_IT(&htim2);
+    HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3);
+    __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_3, 60);
+}
+
 /* USER CODE END 0 */
 
 /**
@@ -370,31 +613,50 @@
   /* Initialize all configured peripherals */
   MX_GPIO_Init();
   MX_LPTIM1_Init();
-    MX_DMA_Init();
+  MX_DMA_Init();
   MX_USART1_UART_Init();
   MX_SPI1_Init();
   MX_ADC_Init();
-
-  MX_IWDG_Init();
+//  MX_IWDG_Init();
+  MX_TIM2_Init();
   /* USER CODE BEGIN 2 */
-    LED_LG_ON;
+//    LED_LG_ON;
     Program_Init();
-    //	LIS3DH_Data_Init();	
+    LIS3DH_Data_Init();	
+//    Uwb_Lora_Switch(1,0);
     Dw1000_Init();
     Dw1000_App_Init();
+    Delay_Ms(10);
+//    Uwb_Lora_Switch(0,0);
     //	HardWareTypeDiffConfig();
     dwt_configuresleep(DWT_PRESRV_SLEEP | DWT_CONFIG, DWT_WAKE_CS | DWT_WAKE_WK| DWT_SLP_EN);
     dwt_entersleep();
-    DW_DISABLE;
      if(HAL_UART_Receive_DMA(&huart1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE))
     {
      Error_Handler();
     }
-    HAL_Delay(2000);
+    Delay_Ms(2);
+//    Uwb_Lora_Switch(0,1);
+//    Lora433rx_Init();
+//    Lora47xrx_Init();
+//    Lora_Init();
+//    Lora47xrx_Init();
+//    Radio.Sleep( );
+    Delay_Ms(10);
+//    Uwb_Lora_Switch(0,0);
+    printf("RXD :66666\r\n");
+    usart_send[0]=0x55;
+    usart_send[1]=0xaa;
+    bat_percent=Get_VDDVlotage();
+      GetLoratx_frequency();
+//    Lora47X_Init();
 //	mcu_sleep();
 	//LED0_BLINK;		
 //	SystemPower_Config();
 //HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);   
+//    HAL_GPIO_WritePin(RADIO_NSS_GPIO_Port, RADIO_NSS_Pin, GPIO_PIN_SET); //片选lora无效
+//    lora_tx_flag=1;
+//     Lora_Tx_Poll();
   /* USER CODE END 2 */
 
   /* Infinite loop */
@@ -404,53 +666,21 @@
     /* USER CODE END WHILE */
 
     /* USER CODE BEGIN 3 */
-		if(g_start_send_flag)
-	{ static uint16_t blink_count=0;
-		SystemClock_Config();
-		g_start_send_flag = 0;	
-		HAL_IWDG_Refresh(&hiwdg);
-//		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;s
-		
-			
-		if(bat_percent>15)
-		{	LED_TB_ON;
-			Tag_App();
-			LED_TB_OFF;
-		}else{
-			LED_TR_ON;
-			Tag_App();
-			LED_TR_OFF;		
-		}
-		//LED0_BLINK;		
-		IdleTask();	
-		if(waitusart_timer>0)
-		{
-			waitusart_timer--;
-		}		 
-	}else{
-	IdleTask();	
-	}
-    if(m_bEUARTTxEn==0&&waitusart_timer==0)
-    {
-     if(memcmp(g_com_map2,g_com_map,200)!=0)
-     {
-        SCB->AIRCR = 0X05FA0000|(unsigned int)0x04;
-     }
+//Lora_rx_open();
+//      Lora_Tx_Poll();
+      Lora_Tx_Poll();
+
+//    HAL_IWDG_Refresh(&hiwdg);
+    IdleTask();	
+      Lora_rece_error=0;
+//	Radio.IrqProcess( ); // Process Radio IRQ
+//     Anchor_App();
+
     #ifndef DEBUG_MODE	
     if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))				
-                    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
+    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
     #endif
-    }
-// HAL_Delay(100);
-    //  Get_Battary();
-
-	//	LIS3DH_Data_Init();	
+    
   }
   /* USER CODE END 3 */
 }
@@ -516,7 +746,7 @@
   * @param None
   * @retval None
   */
-void MX_ADC_Init(void)
+static void MX_ADC_Init(void)
 {
 
   /* USER CODE BEGIN ADC_Init 0 */
@@ -669,6 +899,65 @@
 }
 
 /**
+  * @brief TIM2 Initialization Function
+  * @param None
+  * @retval None
+  */
+static void MX_TIM2_Init(void)
+{
+
+  /* USER CODE BEGIN TIM2_Init 0 */
+
+  /* USER CODE END TIM2_Init 0 */
+
+  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
+  TIM_MasterConfigTypeDef sMasterConfig = {0};
+  TIM_OC_InitTypeDef sConfigOC = {0};
+
+  /* USER CODE BEGIN TIM2_Init 1 */
+
+  /* USER CODE END TIM2_Init 1 */
+  htim2.Instance = TIM2;
+  htim2.Init.Prescaler = 32-1;
+  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
+  htim2.Init.Period = 250-1;
+  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
+  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
+  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
+  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
+  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
+  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  sConfigOC.OCMode = TIM_OCMODE_PWM1;
+  sConfigOC.Pulse = 0;
+  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
+  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
+  if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  /* USER CODE BEGIN TIM2_Init 2 */
+
+  /* USER CODE END TIM2_Init 2 */
+  HAL_TIM_MspPostInit(&htim2);
+
+}
+
+/**
   * @brief USART1 Initialization Function
   * @param None
   * @retval None
@@ -747,28 +1036,22 @@
   __HAL_RCC_GPIOB_CLK_ENABLE();
 
   /*Configure GPIO pin Output Level */
-  HAL_GPIO_WritePin(GPIOA, SCL_Pin|SDA_Pin|SPI_CS_Pin, GPIO_PIN_SET);
+  HAL_GPIO_WritePin(SPI_CS_GPIO_Port, 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);
+  HAL_GPIO_WritePin(GPIOB, BAT_MEAS_GND_Pin|SDA_Pin|SCL_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(GPIOB, LED1_G_Pin|LED1_R_Pin|GPIO_PIN_14|RADIO_nRESET_Pin
+                          |RADIO_NSS_Pin|GPIO_PIN_7, GPIO_PIN_RESET);
 
   /*Configure GPIO pin Output Level */
-  HAL_GPIO_WritePin(GPIOA, DW_WKUP_Pin|DW_CTRL_Pin|LED2_G_Pin, GPIO_PIN_RESET);
+  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8|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 */
@@ -791,35 +1074,53 @@
   GPIO_InitStruct.Pull = GPIO_PULLUP;
   HAL_GPIO_Init(USER_KEY_GPIO_Port, &GPIO_InitStruct);
 
+  /*Configure GPIO pin : RADIO_BUSY_Pin */
+  GPIO_InitStruct.Pin = RADIO_BUSY_Pin;
+  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
+  GPIO_InitStruct.Pull = GPIO_NOPULL;
+  HAL_GPIO_Init(RADIO_BUSY_GPIO_Port, &GPIO_InitStruct);
+
   /*Configure GPIO pin : INPUT_5V_Pin */
   GPIO_InitStruct.Pin = INPUT_5V_Pin;
   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
   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;
+  /*Configure GPIO pins : LED1_G_Pin LED1_R_Pin PB14 RADIO_nRESET_Pin
+                           RADIO_NSS_Pin PB7 */
+  GPIO_InitStruct.Pin = LED1_G_Pin|LED1_R_Pin|GPIO_PIN_14|RADIO_nRESET_Pin
+                          |RADIO_NSS_Pin|GPIO_PIN_7;
   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 : PA8 DW_CTRL_Pin LED2_G_Pin */
+  GPIO_InitStruct.Pin = GPIO_PIN_8|DW_CTRL_Pin|LED2_G_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 : RADIO_DIO1_Pin */
+  GPIO_InitStruct.Pin = RADIO_DIO1_Pin;
+  GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
+  GPIO_InitStruct.Pull = GPIO_NOPULL;
+  HAL_GPIO_Init(RADIO_DIO1_GPIO_Port, &GPIO_InitStruct);
+
+  /*Configure GPIO pins : SDA_Pin SCL_Pin */
+  GPIO_InitStruct.Pin = SDA_Pin|SCL_Pin;
+  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
+  GPIO_InitStruct.Pull = GPIO_NOPULL;
+  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
+  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
   /* EXTI interrupt init*/
-  HAL_NVIC_SetPriority(EXTI0_1_IRQn, 1, 0);
+  HAL_NVIC_SetPriority(EXTI0_1_IRQn, 2, 0);
   HAL_NVIC_EnableIRQ(EXTI0_1_IRQn);
+
+  HAL_NVIC_SetPriority(EXTI2_3_IRQn, 1, 0);
+  HAL_NVIC_EnableIRQ(EXTI2_3_IRQn);
 
 }
 
@@ -848,68 +1149,73 @@
   /*Enter the Standby mode*/
   HAL_PWR_EnterSTANDBYMode();	
 }
-extern uint8_t tx_near_msg[80],stationary_flag;
+extern uint8_t tx_near_msg[80];
 float key_keeptime;
 extern float freqlost_count,range_lost_time;
+extern uint8_t lora_yingda_flag;
+uint8_t lora_yingdatime;
+extern uint8_t lora_chongfuyingda_flag;
+uint16_t no_data_chongqi_num;
+uint16_t anchor_send_num;
+uint8_t no_yingdatime;
 void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)
 {
-	static uint8_t i=0,lost_jumpcount=0;
-	i++;
-	
-	current_slotnum++;
-    freqlost_count+=1/(float)tag_frequency;
-    range_lost_time +=1/(float)tag_frequency;
-    
-    if(freqlost_count>FREQ_LOST_TIME)
+//    uint8_t pinlv;
+    SystemClock_Config();
+    lp_time++;
+    anchor_send_num++;
+    no_data_chongqi_num++;
+    stationary_num++;
+    if(Beepchixutime>0)
     {
-        tag_frequency = NOTAG_FREQ;
-        if(lost_jumpcount++>=4) //无测距情况下,每5秒发一次;
+        Beepchixutime--;
+        if(Beepchixutime==0)
+        {Beep_Off();}
+    }
+    if(no_yingdaflag==1)
+    {
+    no_yingdatime++;
+        if(no_yingdatime>=5)
         {
-            lost_jumpcount = 0;
-            g_start_send_flag=1;
+            no_yingdaflag=0;
+            no_yingdatime=0;
         }
-    }else{
-        g_start_send_flag=1;
     }
-    if(range_lost_time<1||freqlost_count>FREQ_LOST_TIME)
+
+    if( no_data_chongqi_num>600)
+    {NVIC_SystemReset();}
+    SetNextPollTime(0);
+//    if(lora_yingda_flag)
+//    {
+//    lora_yingdatime++;
+//        if(lora_yingdatime>=2)
+//        {lora_yingdatime=0;
+//        lora_yingda_flag=0;            
+//          Lora_tx_mode();
+//            Radio.Send( usart_send, 2);
+//        }
+//    }
+//    else
+//    {
+//    pinlv=g_com_map[COM_MAP_SIZE]/1000;
+    if(lp_time%(g_com_map[COM_INTERVAL]/1000)==0)
     {
-        SetNextPollTime(0);
-    }else{
-        SetNextPollTime(5);
+    lora_tx_flag=1;
+    lora_txanchor_power_flag=0;
+//    }
+    if(anchor_send_num>=3600)
+    {anchor_send_num=0;
+    lora_txanchor_power_flag=1;
+        lora_tx_flag=0;
+        bat_percent=Get_VDDVlotage();
     }
-	nomove_count+=1/(float)tag_frequency;
-    if(motor_keeptime>0)
-	{	
-        nomove_count+=1/(float)tag_frequency;
-	}
-	if(!GET_USERKEY)
-	{
-		key_keeptime+=1/(float)tag_frequency;
-		if(key_keeptime>=KEY_KEEPRESET_TIME)
-		{
-            g_com_map[ACTIVE_INDEX]=!active_flag;
-            save_com_map_to_flash();
-            MOTOR_ON;
-            HAL_Delay(2000);
-			SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; 
-		}
-	}else{
-		key_keeptime=0;
-	}
-//	if(nomove_count>STATIONARY_TIME)
-//	{
-//		stationary_flag = 1;
-//	}else{
-//		stationary_flag = 0;
-//	}
-//if(nomove_count>=nomovesleeptime-1&&imu_enable)
-//	{
-//		tx_near_msg[TAGSTATE] = tx_near_msg[TAGSTATE]|GOINGSLEEPBIT;
-//	}
-//	if(nomove_count>nomovesleeptime&&imu_enable)
-//	{
-//		mcu_sleep();
-//	}
+    }
+
+    if(stationary_num>=10)
+    {stationary_num=0;
+    stationary_flag=1;
+    }
+    
 }
 /* USER CODE END 4 */
 

--
Gitblit v1.9.3