Index: trunk/firmware/CubeMX/Src/gpio.c
===================================================================
--- trunk/firmware/CubeMX/Src/gpio.c	(revision 10)
+++ trunk/firmware/CubeMX/Src/gpio.c	(revision 1)
@@ -74,7 +74,6 @@
   /*Configure GPIO pin : PtPin */
   GPIO_InitStruct.Pin = TP4_Pin;
-  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
+  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
   GPIO_InitStruct.Pull = GPIO_NOPULL;
-  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
   HAL_GPIO_Init(TP4_GPIO_Port, &GPIO_InitStruct);
 
Index: trunk/firmware/CubeMX/Src/main.c
===================================================================
--- trunk/firmware/CubeMX/Src/main.c	(revision 10)
+++ trunk/firmware/CubeMX/Src/main.c	(revision 1)
@@ -193,5 +193,4 @@
 void ExternalRedLED2ShortOnThen2LongOnThenLongPauseBlinking(void);
 void RS485DisableButtonManagement(uint32_t new_time);
-static void BatteryLowVoltageProtection(/*uint32_t current_time*/);
 
 /* USER CODE END PFP */
@@ -279,5 +278,4 @@
   SEGGER_RTT_printf(0, "Free space for statistics in fake EEPROM: %u bytes\n", FEEPROM_StatFreeBytes());
   SEGGER_RTT_printf(0, "MAX_POSSIBLE_DIFF_TO_MEASURE: %u\n", MAX_POSSIBLE_DIFF_TO_MEASURE);
-  SEGGER_RTT_printf(0, "ADC_BAT_CRITICAL_VOLTAGE: %u\n", ADC_BAT_CRITICAL_VOLTAGE);
   SEGGER_RTT_printf(0, "CPU Freq: %u Hz\n", HAL_RCC_GetSysClockFreq());
 
@@ -376,11 +374,5 @@
   while (1)																		// Main loop
   {
-	  TP4_GPIO_Port->BSRR = TP4_Pin;
-
 	  ABVoltageDropCalculation();
-
-	  BatteryLowVoltageProtection();
-
-	  TP4_GPIO_Port->BRR = TP4_Pin;
 
 	  //TIM2->CNT = 0;
@@ -397,5 +389,5 @@
 	  old_time = new_time;														// Saving current time value
 
-	  HAL_IWDG_Refresh(&hiwdg);	// 0.5s RESET
+	  //HAL_IWDG_Refresh(&hiwdg);	// 0.5s RESET
 
 	  Callibration();
@@ -456,23 +448,5 @@
 		  case SWITCH_AUTO:
 			  // Checking whether temperature, current and battery voltage are within limits
-			  if (temperature_shutdown_is_active == 1)
-			  {
-				  if (!restartAutoMode)
-				  {
-					  // If so, opening both MOSFETs (i.e. disconnecting load/charger from battery)
-					  /*
-#ifdef DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
-					  DisableShortCutDetection();
-#endif
-					  HAL_NVIC_DisableIRQ(ADC_DMA_IRQ);
-					  MOSFETS_Management = &ADC_Open_Both_MOSFETs;
-					  sys_data.s.relay_status = RELAY_IS_OPENED;
-					  HAL_NVIC_EnableIRQ(ADC_DMA_IRQ);
-					  ExternalRedLED_Management = &ExternalRedLED1ShortOnThenLongPauseBlinking;
-					  */
-					  restartAutoMode = 1;
-				  }
-			  }
-			  else if (low_bat_shutdown_is_active == 1)
+			  if ((temperature_shutdown_is_active == 1) || (low_bat_shutdown_is_active == 1))
 			  {
 				  if (!restartAutoMode)
@@ -483,9 +457,9 @@
 #endif
 					  HAL_NVIC_DisableIRQ(ADC_DMA_IRQ);
-					  MOSFETS_Management = &DoNothing;
-					  OpenBothMOSFETSVeryFast();
+					  MOSFETS_Management = &ADC_Open_Both_MOSFETs;
 					  sys_data.s.relay_status = RELAY_IS_OPENED;
 					  HAL_NVIC_EnableIRQ(ADC_DMA_IRQ);
-					  ExternalRedLED_Management = &ExternalRedLED5ShortOnThenLongPauseBlinking;
+					  if (temperature_shutdown_is_active == 1) ExternalRedLED_Management = &ExternalRedLED1ShortOnThenLongPauseBlinking;
+					  if (low_bat_shutdown_is_active == 1) ExternalRedLED_Management = &ExternalRedLED5ShortOnThenLongPauseBlinking;
 					  restartAutoMode = 1;
 				  }
@@ -748,5 +722,4 @@
   sys_data.s.command = 0;
   sys_data.s.device_status = 0;
-  sys_data.s.ubsenseb_voltage = 1;
 }
 
@@ -1356,5 +1329,5 @@
 	InternalGreenLED_Management();
 	InternalBlueLED_Management();
-	//InternalRedLED_Management();
+	InternalRedLED_Management();
 	ExternalGreenLED_Management();
 	ExternalRedLED_Management();
@@ -2005,4 +1978,5 @@
 void StartAutoMode(void)
 {
+
 	uint32_t current_time = HAL_GetTick();
 
@@ -2137,5 +2111,5 @@
 
 				// We disable shortcut detecting interrupt
-				//HAL_NVIC_DisableIRQ(ADC1_COMP_IRQn);
+				HAL_NVIC_DisableIRQ(ADC1_COMP_IRQn);
 			//}
 		}
@@ -2207,12 +2181,8 @@
 static inline __attribute__((always_inline)) void CalculatingAndAveragingVoltageOnContactB(void)
 {
-	const uint32_t AVG_VALUE = 64U;
-	static int32_t ubsenseb_voltage_accum = U_BAT_RECOVERY_VOLTAGE_mV * AVG_VALUE;		// << ubsenseb_voltage_accum_avg;
-
-	// Special case for Battery undervoltage condition. During normal operation ubsenseb_voltage will never be zero
-	//if (sys_data.s.ubsenseb_voltage == 0) ubsenseb_voltage_accum = 0;
+	static int32_t ubsenseb_voltage_accum = U_BAT_RECOVERY_VOLTAGE * 32;		// << ubsenseb_voltage_accum_avg;
 
 	// Calculating and averaging battery side voltage
-	int32_t temp = ADC_values[U_BAT_CHANNEL];									
+	int32_t temp = ADC_values[U_BAT_CHANNEL];									// Converting ADC value into int32_t type
 	temp = (temp*ADC_VREF)>>ADC_RESOLUTION;										// Getting voltage value on ADC input [0:3000]mV
 #ifdef VARIANT_24V
@@ -2224,57 +2194,27 @@
 	if (ubsenseb_voltage_accum < 0) ubsenseb_voltage_accum = 0;
 	ubsenseb_voltage_accum += temp;
-	sys_data.s.ubsenseb_voltage = ubsenseb_voltage_accum / AVG_VALUE;			//>> ubsenseb_voltage_accum_avg;
-}
-
-//------------------------------------------------------------------------------
-
-static inline __attribute__((always_inline)) void TestingForLowVoltageShutdown(void)
-{
-	const uint32_t REACTIVATION_DELAY_ms = 10000;
-	static uint32_t low_bat_event_last_time; 
-
+	sys_data.s.ubsenseb_voltage = ubsenseb_voltage_accum / 32;					//>> ubsenseb_voltage_accum_avg;
+}
+
+//------------------------------------------------------------------------------
+
+inline __attribute__((always_inline)) void TestingForLowVoltageShutdown(void)
+{
 	// If voltage on contact B is lower than critical one, then we must initiate "low voltage shutdown"
-	if (sys_data.s.ubsenseb_voltage < U_BAT_CRITICAL_VOLTAGE_mV)
+	if (sys_data.s.ubsenseb_voltage < U_BAT_CRITICAL_VOLTAGE)
 	{
 		if (low_bat_shutdown_is_active == 0)
 		{
-#ifdef DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
-			DisableShortCutDetection();
-#endif
-			HAL_NVIC_DisableIRQ(ADC_DMA_IRQ);
-			MOSFETS_Management = &DoNothing;
-			OpenBothMOSFETSVeryFast();
-			sys_data.s.relay_status = RELAY_IS_OPENED;
-			HAL_NVIC_EnableIRQ(ADC_DMA_IRQ);
-			ExternalRedLED_Management = &ExternalRedLED5ShortOnThenLongPauseBlinking;
-
 			low_bat_shutdown_is_active = 1;
 			sys_data.s.device_status |= (1 << LOWBAT_ERROR);
 			sys_data.s.lowbat_error_cnt++;
 			statDataChanged = 1;
-
-			low_bat_event_last_time = HAL_GetTick();
-		}
-	}
-	else if (sys_data.s.ubsenseb_voltage > U_BAT_RECOVERY_VOLTAGE_mV)
-	{
-		if (low_bat_event_last_time + REACTIVATION_DELAY_ms < HAL_GetTick())
-		{
-			low_bat_shutdown_is_active = 0;
-			sys_data.s.device_status &= ~(1 << LOWBAT_ERROR);
-			if (ExternalRedLED_Management == ExternalRedLED5ShortOnThenLongPauseBlinking) ExternalRedLED_Management = &DoNothing;
-		}
-	}
-
-	/*if (low_bat_shutdown_is_active == 1)
-	{
-		
-	}
-
-	if (sys_data.s.ubsenseb_voltage > U_BAT_RECOVERY_VOLTAGE_mV)
+		}
+	}
+	else if (sys_data.s.ubsenseb_voltage > U_BAT_RECOVERY_VOLTAGE)
 	{
 		low_bat_shutdown_is_active = 0;
 		sys_data.s.device_status &= ~(1 << LOWBAT_ERROR);
-	}*/
+	}
 }
 
@@ -2370,21 +2310,4 @@
 //------------------------------------------------------------------------------
 
-static inline __attribute__((always_inline)) void BatteryLowVoltageProtection(/*uint32_t current_time*/)
-{
-	//static uint32_t last_time = 0;
-
-	// During first start, somtimes we read 0s from ADC, so we need to wait for a while before calculating Min & Max values, especially Min values
-	//if (current_time - last_time >= 0U)
-	{
-		//last_time = current_time;
-
-		CalculatingAndAveragingVoltageOnContactB();
-
-		TestingForLowVoltageShutdown();
-	}
-}
-
-//------------------------------------------------------------------------------
-
 void HeavyCalculations(uint32_t current_time)
 {
@@ -2410,12 +2333,12 @@
 		CalculatingMinMaxVoltagesForContactA();
 
-		//CalculatingAndAveragingVoltageOnContactB();
-
-		//TestingForLowVoltageShutdown();
+		CalculatingAndAveragingVoltageOnContactB();
+
+		TestingForLowVoltageShutdown();
 
 		CalculatingMinMaxVoltagesForContactB();
 
 		// Sliding average calculation for board temperature
-		current_temperature = (((MAX_TEMP - MIN_TEMP)*((int32_t)ADC_values[TEMP_CHANNEL] - TEMP_SENSOR_ADC_AT_MINUS30))/(TEMP_SENSOR_ADC_AT_PLUS100 - TEMP_SENSOR_ADC_AT_MINUS30)) + MIN_TEMP;
+		current_temperature = (((MAX_TEMP - MIN_TEMP)*((int)ADC_values[TEMP_CHANNEL] - TEMP_SENSOR_ADC_AT_MINUS30))/(TEMP_SENSOR_ADC_AT_PLUS100 - TEMP_SENSOR_ADC_AT_MINUS30)) + MIN_TEMP;
 		temperature_accum -= sys_data.s.temperature;
 		temperature_accum += current_temperature;
@@ -2930,7 +2853,7 @@
 void OVP_not_present__LVP_present(void)
 {
-//#ifdef DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
-//	DisableShortCutDetection();
-//#endif
+#ifdef DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
+	DisableShortCutDetection();
+#endif
 	HAL_NVIC_DisableIRQ(ADC_DMA_IRQ);
 	MOSFETS_Management = &ADC_OVP_not_present__LVP_present;
@@ -3210,5 +3133,5 @@
 	// Recording ADC value drop under control current
 	sys_data.s.copper_v_drop_adc = ubbsense_adc;
-	sys_data.s.copper_v_drop_adc_limit = (sys_data.s.copper_v_drop_adc * 110) / 100;  // +10%
+	sys_data.s.copper_v_drop_adc_limit = (sys_data.s.copper_v_drop_adc * 110) / 100;
 
 	SEGGER_RTT_printf(0, "\t\t\tFinal voltage-drop ADC value: %4u. Final voltage-drop value: %3u mV\n", sys_data.s.copper_v_drop_adc, sys_data.s.copper_v_drop);
@@ -3220,5 +3143,4 @@
 	Callibration = &DoNothing;
 	//InrushCurrentManagement = &InrushCurrentDetected; // Test program disables this, so we must re-enable it after callibration
-	MX_IWDG_Init();
 }
 
Index: trunk/firmware/CubeMX/Src/stm32g0xx_it.c
===================================================================
--- trunk/firmware/CubeMX/Src/stm32g0xx_it.c	(revision 10)
+++ trunk/firmware/CubeMX/Src/stm32g0xx_it.c	(revision 1)
@@ -100,5 +100,4 @@
 //extern uint32_t overload_shutdown_time;
 extern void (*InrushCurrentManagement)(void);
-extern int low_bat_shutdown_is_active;
 
 //extern uint16_t i_samples[I_RMS_SAMPLES_COUNT];
@@ -215,22 +214,9 @@
 {
   /* USER CODE BEGIN DMA1_Channel1_IRQn 0 */
-  TP2_GPIO_Port->BSRR = TP2_Pin;
+  //R2_GPIO_Port->BSRR = R2_Pin;
 
   static uint32_t current_integral = 0;
 
   rawMOSFETsVoltageDrop = ADC_values[MOSFETS_VDROP_CHANNEL];
-
-  /*if (ADC_values[U_BAT_CHANNEL] < ADC_BAT_CRITICAL_VOLTAGE)
-  {
-	  MOSFETS_Management = &OpenBothMOSFETSVeryFast;
-	  if (low_bat_shutdown_is_active == 0)
-	  {
-			sys_data.s.ubsenseb_voltage = 0;
-			low_bat_shutdown_is_active = 1;
-			sys_data.s.device_status |= (1 << LOWBAT_ERROR);
-			sys_data.s.lowbat_error_cnt++;
-			statDataChanged = 1;
-	  }
-  }*/
 
   MOSFETS_Management();
@@ -254,5 +240,5 @@
   else current_integral = 0;
 
-  TP2_GPIO_Port->BRR = TP2_Pin;
+  //R2_GPIO_Port->BRR = R2_Pin;
 
   return;
Index: trunk/firmware/CubeMX/Src/tim.c
===================================================================
--- trunk/firmware/CubeMX/Src/tim.c	(revision 10)
+++ trunk/firmware/CubeMX/Src/tim.c	(revision 1)
@@ -178,6 +178,4 @@
   /* USER CODE BEGIN TIM15_Init 0 */
 
-  // This timer is used to turn on again overcurrent detection after short turn-off.
-
   /* USER CODE END TIM15_Init 0 */
 
