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 |  702 +++++++++++++++++++++++++++++++++++++++++-----------------
 1 files changed, 495 insertions(+), 207 deletions(-)

diff --git a/Src/main.c b/Src/main.c
index 6fc9e4a..04306c4 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -30,6 +30,12 @@
 #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    //硬件版本号和批次信息位置
@@ -55,6 +61,8 @@
 /* Private variables ---------------------------------------------------------*/
 ADC_HandleTypeDef hadc;
 
+IWDG_HandleTypeDef hiwdg;
+
 LPTIM_HandleTypeDef hlptim1;
 
 SPI_HandleTypeDef hspi1;
@@ -75,8 +83,9 @@
 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 */
 
@@ -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);
@@ -110,15 +124,50 @@
 	 Error_Handler();
 	}
 }
+static void MX_DMA_DeInit(void)
+{
+
+  /* DMA controller clock enable */
+  __HAL_RCC_DMA1_CLK_DISABLE();
+
+  /* DMA interrupt init */
+  /* DMA1_Channel2_3_IRQn interrupt configuration */
+ // HAL_NVIC_SetPriority(DMA1_Channel2_3_IRQn, 1, 0);
+  HAL_NVIC_DisableIRQ(DMA1_Channel2_3_IRQn);
+
+}
+void UsartDeInit(void)
+{
+    GPIO_InitTypeDef GPIO_InitStruct = {0};
+    waitusart_timer = 0;
+    MX_DMA_DeInit();
+    HAL_UART_DeInit(&huart1);
+    GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
+    GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
+    GPIO_InitStruct.Pull = GPIO_NOPULL;
+    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+}
 uint16_t hardware_version,hardware_pici,hardware_type;
-uint8_t state5v;
+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))
 	{
         	
@@ -133,21 +182,25 @@
 		}
 		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{
-		state5v=0;
-		LED_LR_OFF;
-		LED_LG_OFF;
+        if(state5v==1)
+        {
+            state5v=0;
+            UsartDeInit();
+        }
+		LED2_TR_OFF;
+		LED2_TB_OFF;
 //	}
 }
-			if(g_com_map[MAP_SIGN_INDEX]!=0x55AA||g_com_map[COM_INTERVAL]==0)
+        if(g_com_map[MAP_SIGN_INDEX]!=0x55AA||g_com_map[COM_INTERVAL]==0)
 		{
-				SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
+            SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
 		}
 
 	}
@@ -155,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;
 //	
@@ -174,40 +227,106 @@
 //	{
 //	lpcount+=LPTIMER_1S_COUNT;
 //	}
-    lpcount = last_lpcount+32768/tag_frequency;
-    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);
 
 }
 
-void HardWareTypeDiffConfig(void)
+/* 返回ch字符在sign数组中的序号 */
+int getIndexOfSigns(char ch)
 {
-GPIO_InitTypeDef GPIO_InitStruct = {0};
-//hardware_type=2;
-if(hardware_type == 0)
-    hardware_type = 1;
-	switch(hardware_type)
-		{
-        case 1:
-	  
-		 HAL_GPIO_WritePin(GPIOB, MOTOR_Pin, GPIO_PIN_SET);
-  GPIO_InitStruct.Pin = MOTOR_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);
-			break;
-		case 2:
-			MX_TIM2_Init();
-		//BarInit();
-			break;
-		}
+    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;
@@ -218,21 +337,25 @@
 	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)|51;
-    
+	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[GROUP_ID]=21;
 //    		g_com_map[DEV_ID]=4;
     //	g_com_map[COM_INTERVAL]=100;
     //	g_com_map[MAX_REPORT_ANC_NUM]=1;
     //	g_com_map[NOMOVESLEEP_TIME]=10;
     #endif
-    
+    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;
@@ -252,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;}
@@ -260,15 +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("标签ID: %d .\r\n",dev_id);
-	printf("通讯间隔: %d ms.\r\n",g_com_map[COM_INTERVAL]);
-	printf("单次通讯基站数量: %d个.\r\n",g_com_map[MAX_REPORT_ANC_NUM]);
+    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]);
@@ -298,12 +420,6 @@
 {
 		//SCB->AIRCR = 0X05FA0000|(unsigned int)0x04; //软复位回到bootloader   
 }
-/* USER CODE END 0 */
-
-/**
-  * @brief  The application entry point.
-  * @retval int
-  */
 uint16_t temp2; 
 float dw_vbat,last_vbat;
 uint8_t Get_Battary_UWB(void)
@@ -337,6 +453,141 @@
     }
 	return last_value;	       
 }
+void MX_ADC_DeInit(void)
+{
+    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 */
+
+/**
+  * @brief  The application entry point.
+  * @retval int
+  */
 int main(void)
 {
   /* USER CODE BEGIN 1 */
@@ -362,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_TIM2_Init();
+  MX_ADC_Init();
+//  MX_IWDG_Init();
+  MX_TIM2_Init();
   /* USER CODE BEGIN 2 */
-	LED_LG_ON;
-	Program_Init();
-//	LIS3DH_Data_Init();	
-	Dw1000_Init();
-	Dw1000_App_Init();
-//	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);
+//    LED_LG_ON;
+    Program_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();
+     if(HAL_UART_Receive_DMA(&huart1,m_EUART_DMA_RXBuf,EUART_RX_BUF_SIZE))
+    {
+     Error_Handler();
+    }
+    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 */
@@ -396,56 +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;	
-		
-//		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--;
-		}
-			if(m_bEUARTTxEn==0&&waitusart_timer==0)
-			{
-             if(memcmp(g_com_map2,g_com_map,200)!=0)
-             {
-                SCB->AIRCR = 0X05FA0000|(unsigned int)0x04;
-             }
-#ifndef DEBUG_MODE	
-if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))				
-				HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
-#endif
-			}
-		
- 
-	}else{
-	IdleTask();	
-	}
+//Lora_rx_open();
+//      Lora_Tx_Poll();
+      Lora_Tx_Poll();
 
-// HAL_Delay(100);
-    //  Get_Battary();
+//    HAL_IWDG_Refresh(&hiwdg);
+    IdleTask();	
+      Lora_rece_error=0;
+//	Radio.IrqProcess( ); // Process Radio IRQ
+//     Anchor_App();
 
-	//	LIS3DH_Data_Init();	
+    #ifndef DEBUG_MODE	
+    if(!HAL_GPIO_ReadPin(INPUT_5V_GPIO_Port,INPUT_5V_Pin))				
+    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);  
+    #endif
+    
   }
   /* USER CODE END 3 */
 }
@@ -466,13 +701,15 @@
   /** Configure LSE Drive Capability
   */
   HAL_PWR_EnableBkUpAccess();
-  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);
+  __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_HIGH);
   /** Initializes the CPU, AHB and APB busses clocks
   */
-  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
+  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI
+                              |RCC_OSCILLATORTYPE_LSE;
   RCC_OscInitStruct.LSEState = RCC_LSE_ON;
   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
+  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
@@ -509,12 +746,7 @@
   * @param None
   * @retval None
   */
-void MX_ADC_DeInit(void)
-{
-    HAL_ADC_DeInit(&hadc);
-    HAL_ADCEx_DisableVREFINT();
-}
-void MX_ADC_Init(void)
+static void MX_ADC_Init(void)
 {
 
   /* USER CODE BEGIN ADC_Init 0 */
@@ -532,7 +764,7 @@
   hadc.Init.OversamplingMode = DISABLE;
   hadc.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
   hadc.Init.Resolution = ADC_RESOLUTION_12B;
-  hadc.Init.SamplingTime = ADC_SAMPLETIME_160CYCLES_5;
+  hadc.Init.SamplingTime = ADC_SAMPLETIME_1CYCLE_5;
   hadc.Init.ScanConvMode = ADC_SCAN_DIRECTION_FORWARD;
   hadc.Init.DataAlign = ADC_DATAALIGN_RIGHT;
   hadc.Init.ContinuousConvMode = DISABLE;
@@ -561,6 +793,35 @@
     HAL_ADCEx_EnableVREFINT();
     HAL_ADCEx_Calibration_Start(&hadc,ADC_SINGLE_ENDED);
   /* USER CODE END ADC_Init 2 */
+
+}
+
+/**
+  * @brief IWDG Initialization Function
+  * @param None
+  * @retval None
+  */
+static void MX_IWDG_Init(void)
+{
+
+  /* USER CODE BEGIN IWDG_Init 0 */
+
+  /* USER CODE END IWDG_Init 0 */
+
+  /* USER CODE BEGIN IWDG_Init 1 */
+
+  /* USER CODE END IWDG_Init 1 */
+  hiwdg.Instance = IWDG;
+  hiwdg.Init.Prescaler = IWDG_PRESCALER_256;
+  hiwdg.Init.Window = 4095;
+  hiwdg.Init.Reload = 4095;
+  if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  /* USER CODE BEGIN IWDG_Init 2 */
+
+  /* USER CODE END IWDG_Init 2 */
 
 }
 
@@ -657,9 +918,9 @@
 
   /* USER CODE END TIM2_Init 1 */
   htim2.Instance = TIM2;
-  htim2.Init.Prescaler = 9;
+  htim2.Init.Prescaler = 32-1;
   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
-  htim2.Init.Period = 1171;
+  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)
@@ -682,7 +943,7 @@
     Error_Handler();
   }
   sConfigOC.OCMode = TIM_OCMODE_PWM1;
-  sConfigOC.Pulse = 599;
+  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)
@@ -690,11 +951,7 @@
     Error_Handler();
   }
   /* USER CODE BEGIN TIM2_Init 2 */
-//if(HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_3) != HAL_OK)
-//  {
-//    /* Starting Error */
-//    Error_Handler();
-//  }
+
   /* USER CODE END TIM2_Init 2 */
   HAL_TIM_MspPostInit(&htim2);
 
@@ -779,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 */
@@ -823,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);
 
 }
 
@@ -880,54 +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;
+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;
-	i++;
-	g_start_send_flag=1;
-	current_slotnum++;
-    freqlost_count+=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;
+        Beepchixutime--;
+        if(Beepchixutime==0)
+        {Beep_Off();}
     }
-	SetNextPollTime(tyncpoll_time);
-	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(no_yingdaflag==1)
+    {
+    no_yingdatime++;
+        if(no_yingdatime>=5)
+        {
+            no_yingdaflag=0;
+            no_yingdatime=0;
+        }
+    }
+
+    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)
+    {
+    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();
+    }
+    }
+
+    if(stationary_num>=10)
+    {stationary_num=0;
+    stationary_flag=1;
+    }
+    
 }
 /* USER CODE END 4 */
 

--
Gitblit v1.9.3