From 47a816b370d561834e190875f10802ce55c62c27 Mon Sep 17 00:00:00 2001
From: YunHorn Technology <dp.s@yunhorn.com>
Date: Wed, 22 May 2024 12:14:29 +0800
Subject: [PATCH] minor improve for low power and do_fft_flag

---
 Core/Inc/yunhorn_sts_prd_conf.h |  4 +-
 Core/Src/stm32_lpm_if.c         |  1 +
 Core/Src/sys_app.c              |  2 +-
 LoRaWAN/App/lora_app.c          | 74 ++++++++++++++++++++-------------
 4 files changed, 49 insertions(+), 32 deletions(-)

diff --git a/Core/Inc/yunhorn_sts_prd_conf.h b/Core/Inc/yunhorn_sts_prd_conf.h
index 5c293de..31ef516 100644
--- a/Core/Inc/yunhorn_sts_prd_conf.h
+++ b/Core/Inc/yunhorn_sts_prd_conf.h
@@ -59,8 +59,8 @@
 
 #ifdef    YUNHORN_STS_M7_ENABLED
 #define MajorVer													24U
-#define MinorVer													03U
-#define SubMinorVer 											26U
+#define MinorVer													05U
+#define SubMinorVer 											22U
 #define FirmwareVersion										3U
 /*
 #define	YUNHORN_STS_M7_NVM_CFG_SIZE				3U
diff --git a/Core/Src/stm32_lpm_if.c b/Core/Src/stm32_lpm_if.c
index a378230..103533d 100644
--- a/Core/Src/stm32_lpm_if.c
+++ b/Core/Src/stm32_lpm_if.c
@@ -97,6 +97,7 @@ void PWR_EnterStopMode(void)
   /* USER CODE END EnterStopMode_1 */
 	HAL_GPIO_WritePin(GPIOB,GPIO_PIN_All, GPIO_PIN_RESET);
 	HAL_GPIO_WritePin(GPIOC,GPIO_PIN_All, GPIO_PIN_RESET);
+	HAL_GPIO_WritePin(GPIOA,~(GPIO_PIN_0|GPIO_PIN_1), GPIO_PIN_RESET);
   HAL_SuspendTick();
   /* Clear Status Flag before entering STOP/STANDBY Mode */
   LL_PWR_ClearFlag_C1STOP_C1STB();
diff --git a/Core/Src/sys_app.c b/Core/Src/sys_app.c
index 619e64b..81bd606 100644
--- a/Core/Src/sys_app.c
+++ b/Core/Src/sys_app.c
@@ -100,7 +100,7 @@ void SystemApp_Init(void)
   UTIL_TIMER_Init();
   SYS_TimerInitialisedFlag = 1;
   /* Initializes the SW probes pins and the monitor RF pins via Alternate Function */
-  DBG_Init();
+  //DBG_Init();
 
   /*Initialize the terminal */
   UTIL_ADV_TRACE_Init();
diff --git a/LoRaWAN/App/lora_app.c b/LoRaWAN/App/lora_app.c
index 3502442..80858c3 100644
--- a/LoRaWAN/App/lora_app.c
+++ b/LoRaWAN/App/lora_app.c
@@ -1125,33 +1125,47 @@ static void SendTxData(void)
 	AppData.Buffer[i++] = (uint8_t)(0xFF & sts_mtmcode2); //mtmcode2;					//#02
 	AppData.Buffer[i++] = (uint8_t)(0xFF & sts_hardware_ver); //hardware_Ver; //#03
 	AppData.Buffer[i++] = (uint8_t)(99*batteryLevel/254);											//#04  		/* 99% (very low) to 254 (fully charged) */
-	AppData.Buffer[i++] = 0x0D;		//temp																			//#05
-	
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vibration_duration);  			//06
-	
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_x_freq >> 8);						//07      Freq Max 256 Hz Given sample rate & FFT Length 512
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_x_freq);								//08      Freq Max 256 Hz Given sample rate & FFT Length 512
-	
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_y_freq >> 8);						//09			Freq Max 256 Hz Given sample rate & FFT Length 512
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_y_freq);								//10			Freq Max 256 Hz Given sample rate & FFT Length 512
-	
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_z_freq >> 8);						//11			Freq Max 256 Hz Given sample rate & FFT Length 512
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_z_freq);								//12			Freq Max 256 Hz Given sample rate & FFT Length 512
-	
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_x_rms >> 8 );						//13			Freq Max 256 Hz Given sample rate & FFT Length 512
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_x_rms);									//14
-		
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_y_rms >> 8);						//15
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_y_rms);									//16
-		
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_z_rms >>8 );						//17
-	AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_z_rms);									//18
+	if (do_fft_flag !=0) 
+		{
+			AppData.Buffer[i++] = 0x0D;		//temp																			//#05
+			
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vibration_duration);  			//06
+			
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_x_freq >> 8);						//07      Freq Max 256 Hz Given sample rate & FFT Length 512
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_x_freq);								//08      Freq Max 256 Hz Given sample rate & FFT Length 512
+			
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_y_freq >> 8);						//09			Freq Max 256 Hz Given sample rate & FFT Length 512
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_y_freq);								//10			Freq Max 256 Hz Given sample rate & FFT Length 512
+			
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_z_freq >> 8);						//11			Freq Max 256 Hz Given sample rate & FFT Length 512
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_z_freq);								//12			Freq Max 256 Hz Given sample rate & FFT Length 512
+			
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_x_rms >> 8 );						//13			Freq Max 256 Hz Given sample rate & FFT Length 512
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_x_rms);									//14
+				
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_y_rms >> 8);						//15
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_y_rms);									//16
+				
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_z_rms >>8 );						//17
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vb_z_rms);									//18
+		} else {
+			
+			AppData.Buffer[i++] = 0x01;		//temp																			//#05
+			
+			AppData.Buffer[i++] = (uint8_t)(0xFF & m7_data.vibration_duration);  			//06
+			
+		}
 	
 	
-	
-	APP_LOG(TS_OFF, VLEVEL_H, "###### Duration: %02d seconds | FREQ_X:%d Hz | FREQ_Y:%d Hz | FREQ_Z:%d Hz |\r\n###### | RMS_X:%d | RMS_Y:%d | RMS_Z:%d | BATTERY_B:%02d%% |\r\n", 
-		AppData.Buffer[6], (AppData.Buffer[7]*255+AppData.Buffer[8]), (AppData.Buffer[9]*255+AppData.Buffer[10]), (AppData.Buffer[11]*255+AppData.Buffer[12]), 
-		(AppData.Buffer[13]*255+AppData.Buffer[14]),(AppData.Buffer[15]*255+AppData.Buffer[16]),(AppData.Buffer[17]*255+AppData.Buffer[18]),AppData.Buffer[04]);
+	if (do_fft_flag !=0) 
+		{
+			APP_LOG(TS_OFF, VLEVEL_H, "###### Duration: %02d seconds | FREQ_X:%d Hz | FREQ_Y:%d Hz | FREQ_Z:%d Hz |\r\n###### | RMS_X:%d | RMS_Y:%d | RMS_Z:%d | BATTERY_B:%02d%% |\r\n", 
+			AppData.Buffer[6], (AppData.Buffer[7]*255+AppData.Buffer[8]), (AppData.Buffer[9]*255+AppData.Buffer[10]), (AppData.Buffer[11]*255+AppData.Buffer[12]), 
+			(AppData.Buffer[13]*255+AppData.Buffer[14]),(AppData.Buffer[15]*255+AppData.Buffer[16]),(AppData.Buffer[17]*255+AppData.Buffer[18]),AppData.Buffer[04]);
+		} else {
+			APP_LOG(TS_OFF, VLEVEL_H, "###### Duration: %02d seconds | BATTERY_B:%02d%% |\r\n", 
+						AppData.Buffer[6], AppData.Buffer[04]);			
+		}
 	}
 
 /*
@@ -1748,9 +1762,9 @@ void STS_SENSOR_Function_Test_Process(void)
 	{
 #ifdef YUNHORN_STS_M7_ENABLED
 	int16_t self_test_result[9]={0,0,0,0, 0,0,0, 0,0};	
-	
+	do_fft_flag = 1; 
 	STS_MOTION_SENSOR_Function_Test_Process(&self_test_result[0], count);	
-	
+	do_fft_flag = 0; 
 	outbuf[i++] = (uint8_t) 0X12;	//SIZE OF FOLLOWING DATA
 	
 	outbuf[i++] = (uint8_t) (self_test_result[0]>>8)&0xff;
@@ -1789,7 +1803,7 @@ void STS_SENSOR_Function_Test_Process(void)
 
 void STS_YunhornAuthenticationCode_Process(void)
 {
-	if (sts_ac_code[0] == 0x00) {
+	if ((sts_ac_code[0] == 0x00) && (sts_ac_code[19] == 0x00)) {
 		APP_LOG(TS_OFF,VLEVEL_M, "Initial AC CODE blank... \r\n");
 		return;
 	}
@@ -1797,13 +1811,14 @@ void STS_YunhornAuthenticationCode_Process(void)
 	sts_service_mask = (sts_hmac_verify()!= 0)? STS_SERVICE_MASK_L2:STS_SERVICE_MASK_L0;
 	if (sts_service_mask == STS_SERVICE_MASK_L2) {
 		sts_ac_code[0] = 0x0;
+		sts_ac_code[19] = 0x0;
 	}
 	APP_LOG(TS_OFF, VLEVEL_M, "STS_SERVICE_MASK:%d \r\n",sts_service_mask);
 }
 
 void STS_YunhornSTSEventRFAC_Process(void)
 {
-	if (sts_ac_code[0] ==0x0)
+	if ((sts_ac_code[0] ==0x0) && (sts_ac_code[19] == 0x00))
 	{
 		if ((rfac_timer >= STS_BURN_IN_RFAC) && (rfac_timer < (STS_BURN_IN_RFAC +3)))
 		{
@@ -1816,6 +1831,7 @@ void STS_YunhornSTSEventRFAC_Process(void)
 			sts_service_mask = (sts_hmac_verify()!= 0)? STS_SERVICE_MASK_L2:STS_SERVICE_MASK_L0;
 			if (sts_service_mask == STS_SERVICE_MASK_L2) {
 				sts_ac_code[0] = 0x0;
+					sts_ac_code[19] = 0x0;
 			}
 		}
 	}