Index: trunk/firmware/CubeMX/Inc/main.h
===================================================================
--- trunk/firmware/CubeMX/Inc/main.h	(revision 10)
+++ trunk/firmware/CubeMX/Inc/main.h	(revision 1)
@@ -59,7 +59,5 @@
 /* USER CODE BEGIN EFP */
 
-#ifdef VARIANT_24V
-#define DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
-#endif
+//#define DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
 
 #ifdef DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
@@ -127,5 +125,5 @@
 #define BTN1_Pin GPIO_PIN_15
 #define BTN1_GPIO_Port GPIOA
-#define OUT_CTRL_Pin GPIO_PIN_0
+#define OUT_CTRL_Pin GPIO_PIN_1
 #define OUT_CTRL_GPIO_Port GPIOD
 #define SW_OFF_Pin GPIO_PIN_2
@@ -143,4 +141,6 @@
 /* USER CODE BEGIN Private defines */
 
+#define INVERTER_CAP_PRECHARGE
+
 #ifdef DEBUG
 	#define DEVICE_TYPE_ID						201
@@ -148,5 +148,4 @@
 	#ifdef VARIANT_24V
 		#define DEVICE_TYPE_ID					202
-		#define INVERTER_CAP_PRECHARGE
 	#else
 		#define DEVICE_TYPE_ID					201
@@ -184,12 +183,12 @@
 #ifdef VARIANT_24V
 	#define CONTROL_CURRENT_A					380		// [A] long-term current limit of the device.
-	#define SHORTCUT_CURRENT_mV					1100
-	#define INRUSH_CURRENT_mV					550		// [mV] Some big value to pass test sequence
-	#define COPPER_V_DROP_AT_CONTROL_CURRENT_mV	200		// [mV] - must be measured on the board
+	#define SHORTCUT_CURRENT_mV					800		// [mV] Some big value to pass test sequence
+	#define INRUSH_CURRENT_mV					1100	// [mV] Some big value to pass test sequence
+	#define COPPER_V_DROP_AT_CONTROL_CURRENT_mV	56		// [mV] - must be measured on the board
 #else
 	#define CONTROL_CURRENT_A					500		// [A] long-term current limit of the device.
-    #define SHORTCUT_CURRENT_mV					1500
-	#define INRUSH_CURRENT_mV					500
-	#define COPPER_V_DROP_AT_CONTROL_CURRENT_mV	100		// [mV] - must be measured during calibration
+    #define SHORTCUT_CURRENT_mV					1500	// [mV] Some big value to pass test sequence
+	#define INRUSH_CURRENT_mV					616		// [mV] Some big value to pass test sequence
+	#define COPPER_V_DROP_AT_CONTROL_CURRENT_mV	150		// [mV] - must be measured on the board
 #endif
 //#define COPPER_V_DROP_AT_INRUSH_CURRENT		((INRUSH_CURRENT_A * COPPER_V_DROP_AT_CONTROL_CURRENT_A)/CONTROL_CURRENT_A)	// ~391mV
@@ -203,5 +202,5 @@
 #define CURRENT_INTEGRAL_FREQ					320000U		// (7.5cycles + 12.5cycles)*1channels + (3.5cycles + 12.5cycles)*5channels = 20 + 80 = 100cycles @ 32MHz = 320 kHz
 #ifdef VARIANT_24V
-	#define CURRENT_INTEGRATION_PERIOD			1500		// µs
+	#define CURRENT_INTEGRATION_PERIOD			575			// µs
 #else
 	#define CURRENT_INTEGRATION_PERIOD			1700		// µs
@@ -287,12 +286,11 @@
 #define COMMAND_TURN_OVERLOAD_DETECTION_ON		30405
 
-//#define U_BAT_CALC_PERIOD				        5	// ms
+#define U_BAT_CALC_PERIOD				        5	// ms
 #ifdef VARIANT_24V
 #define U_BAT_CRITICAL_VOLTAGE			        20000 // mV	(If voltage on battery is lower than 10V, then there is a probability that MOSFETs will not open/close properly)
 #define U_BAT_RECOVERY_VOLTAGE			        24000 // mV	(If voltage on battery is higher than 12V, then it is fully recovered from possible previous discharge)
 #else
-#define U_BAT_CRITICAL_VOLTAGE_mV		        10000 // mV	(If voltage on battery is lower than 10V, then there is a probability that MOSFETs will not open/close properly)
-#define U_BAT_RECOVERY_VOLTAGE_mV		        12000 // mV	(If voltage on battery is higher than 12V, then it is fully recovered from possible previous discharge)
-#define ADC_BAT_CRITICAL_VOLTAGE				((((U_BAT_CRITICAL_VOLTAGE_mV / 1000) * R22) * (ADC_MAX_VALUE + 1)) / ((R23 + R22) * (ADC_VREF / 1000)))
+#define U_BAT_CRITICAL_VOLTAGE			        10000 // mV	(If voltage on battery is lower than 10V, then there is a probability that MOSFETs will not open/close properly)
+#define U_BAT_RECOVERY_VOLTAGE			        12000 // mV	(If voltage on battery is higher than 12V, then it is fully recovered from possible previous discharge)
 #endif
 
@@ -349,5 +347,5 @@
 #define FC_WRITE_MULTIPLE_REGISTER		        0x10
 
-#define HOUR_TIME_INTERVALL						(60U * 60000U)	// 2 min for DEBUG
+#define HOUR_TIME_INTERVALL						(2UL * 60000UL)	// 2 min for DEBUG
 
 #define DAC_HANDLE								hdac1
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 */
 
