Index: trunk/fw_g473rct/SES/src/ah_counter.c
===================================================================
--- trunk/fw_g473rct/SES/src/ah_counter.c	(revision 25)
+++ trunk/fw_g473rct/SES/src/ah_counter.c	(revision 26)
@@ -175,25 +175,56 @@
   sys_data.s.values.mAhCounter = sys_data.s.values.mAsCounter / 3600LL;
 
-  static uint16_t lowVoltageCnt;
-  if (sys_data.s.values.batteryVoltage < sys_data.s.values.uBatEmptyTempComp && sys_data.s.values.batteryVoltage > 1000)
-  {
-	  lowVoltageCnt++;
-	  if ((lowVoltageCnt >= 10) && (startMeasurement == 1)) // 5 Sekunden fest
-	  {
-		  lowVoltageCnt = 10; //sys_data.s.parameter.tBatFull;
-
-		  if ((sys_data.s.values.lastTimeVbatFull >= 3600U) && (sys_data.s.values.lastTimeVbatFull <= 200U * 3600U)) 	// This line prevents from very high discharge-currents to be used to estimate battery capacity
-		  {
-			  // This line is not so important anymore, because we do not allow mAh_AutoMode to be greater than zero
-			  sys_data.s.values.detectedCapacity = sys_data.s.values.mAh_AutoMode >= 0 ? sys_data.s.values.mAh_AutoMode : -sys_data.s.values.mAh_AutoMode;
-			  WH_COUNTER_SetDetectedEnergy();
-			  startMeasurement = 0;
-			  EEPROM_storeConfig(&sys_data, 0);	// Saving detected values
-		  }
-		  sys_data.s.values.lastTimeVbatEmpty = 0U;
-	  }
-  }
-  else lowVoltageCnt = 0;
-
+  static uint16_t cnt;
+  if (sys_data.s.parameter.batteryEmptyDetectionMode == 0)
+  {
+    if (sys_data.s.values.batteryVoltage < sys_data.s.values.uBatEmptyTempComp && sys_data.s.values.batteryVoltage > 1000) // Verhindert das beim abziehen der Sense ein Batt Empty erkannt wird
+    {
+  	  cnt++;
+  	  if ((cnt >= 10) && (startMeasurement == 1)) // 5 Sekunden fest
+  	  {
+  		  cnt = 10; //sys_data.s.parameter.tBatFull;
+
+  		  if ((sys_data.s.values.lastTimeVbatFull >= 3600U) && (sys_data.s.values.lastTimeVbatFull <= 200U * 3600U)) 	// This line prevents from very high discharge-currents to be used to estimate battery capacity
+  		  {
+  			  // This line is not so important anymore, because we do not allow mAh_AutoMode to be greater than zero
+  			  sys_data.s.values.detectedCapacity = sys_data.s.values.mAh_AutoMode >= 0 ? sys_data.s.values.mAh_AutoMode : -sys_data.s.values.mAh_AutoMode;
+  			  WH_COUNTER_SetDetectedEnergy();
+  			  startMeasurement = 0;			  
+  		  }
+  		  sys_data.s.values.lastTimeVbatEmpty = 0U;
+  	  }
+    }
+    else 
+    {
+      cnt = 0;
+    }
+  }
+  else
+  {
+	// Neuer Modus. Spannungsmessung wird ignoriert. Erkannt wird Batt Leer mit LVP Signal von LiPro
+	// OVP darf nicht ausgehen, sonst handelt es sich um ein Temperaturabschaltung oder ein andere Fehler
+	// 1000mV als Schwelle um sicher vor rauschen um den Nullpunkt zu seinzu sein
+    if ((sys_data.s.values.ovp_sense > 1000) && (sys_data.s.values.lvp_sense < 1000)) 
+    {
+  	  cnt++;
+  	  if ((cnt >= 10) && (startMeasurement == 1)) // 5 Sekunden fest
+  	  {
+  		  cnt = 10; //sys_data.s.parameter.tBatFull;
+
+  		  if ((sys_data.s.values.lastTimeVbatFull >= 3600U) && (sys_data.s.values.lastTimeVbatFull <= 240U * 3600U)) 	// This line prevents from very high discharge-currents to be used to estimate battery capacity
+  		  {
+  			  // This line is not so important anymore, because we do not allow mAh_AutoMode to be greater than zero
+  			  sys_data.s.values.detectedCapacity = sys_data.s.values.mAh_AutoMode >= 0 ? sys_data.s.values.mAh_AutoMode : -sys_data.s.values.mAh_AutoMode;
+  			  WH_COUNTER_SetDetectedEnergy();
+  			  startMeasurement = 0;			  
+  		  }
+  		  sys_data.s.values.lastTimeVbatEmpty = 0U;
+  	  }
+    }
+    else 
+    {
+      cnt = 0;
+    }
+  }
 
 
Index: trunk/fw_g473rct/SES/src/eeprom.c
===================================================================
--- trunk/fw_g473rct/SES/src/eeprom.c	(revision 25)
+++ trunk/fw_g473rct/SES/src/eeprom.c	(revision 26)
@@ -4,4 +4,5 @@
 #include "stdio.h"
 #include "math.h"
+#include "modbus.h"
 
 #define CONCAT(a, b) CONCAT_INNER(a, b)											// These three macros
@@ -57,4 +58,7 @@
   uint32_t	  currentGain;
 
+  int32_t	  currentOffsetFast;
+  uint32_t	  currentGainFast;
+
   int64_t	  mAsCounter;
   int32_t	  detectedCapacity;
@@ -97,5 +101,4 @@
   uint16_t	  cefW;
 
-
 } eeprom_data_t;
 
@@ -136,5 +139,5 @@
 
 // Data to store reated defines
-//#define SIZEOF_DEFAULT_EEPROM_DATA                (sizeof(eeprom_new_data_t))
+//#define SIZEOF_DEFAULT_EEPROM_DATA              (sizeof(eeprom_new_data_t))
 #define SIZEOF_CHANGED_EEPROM_DATA                (sizeof(eeprom_data_t))
 #define SIZEOF_DEVICE_INFO                        (sizeof(device_info_t))
@@ -156,5 +159,4 @@
 static uint32_t GetPage(uint32_t Address);
 static HAL_StatusTypeDef getEEPROMData(uint32_t address, uint8_t * data, uint32_t len);
-
 void EEPROM_Read (uint16_t page, uint16_t offset, uint8_t *data, uint16_t size);
 void EEPROM_Write (uint16_t page, uint16_t offset, uint8_t *data, uint16_t size);
@@ -162,5 +164,4 @@
 
 
-
 // muss modulo 8 noch hinzufügen wg 8 byte alignement
 static uint8_t eepromData[SIZE_OF_DATA_TO_STORE];
@@ -174,5 +175,5 @@
 
 	/* baudrate		*/														19200,			// uint32_t   baudrate;
-	/* parityMode	*/														0,				// uint16_t   parityMode;
+	/* parityMode	*/														MODBUS_UART_PARITY_EVEN,  // uint16_t   parityMode;
 	/* stopBits		*/														1,				// uint16_t   stopBits;
 	/* slave_adress	*/														1,				// uint16_t   slave_adress;
@@ -202,4 +203,7 @@
 	/* currentGain	  */													1000000,		//uint32_t  currentGain;
 
+	/* currentOffsetFast  */												0,				//int32_t   currentOffset;
+	/* currentGainFast  */													1000000,		//uint32_t  currentGain;
+
 	/* mAsCounter		*/													0,				// mAsCounter
 	/* detectedCapacity	*/													-1,				// detectedCapacity
@@ -212,10 +216,10 @@
 	/* cef						*/											99,				// cef
 	/* peukert					*/											105,			// peukert
-	/* cellCapacity				*/											100000,			// cell Capacity in mAh
-	/* cellEnergy				*/											2640000,		// cell energy in mWh
-	/* iBatFull					*/											10,				// I-batt full 4% 4A bei 100Ah akku
+	/* cellCapacity				*/											160000,			// cell Capacity in mAh
+	/* cellEnergy				*/											2048000,		// cell energy in mWh
+	/* iBatFull					*/											10,				// I-batt full 10%, 10A bei 100Ah akku
 	/* tBatFull					*/											2,				// t-batt full 2 Sekunden
-	/* uBatFull					*/											14000,			// 14Volt Ubatt full
-	/* uBatEmpty				*/											12500,			// 11,312V Ubatt Empty
+	/* uBatFull					*/											0,				// 14V olt Ubatt full, Neu: Bei 0: Erkung von Lipro LVP als 0% 
+	/* uBatEmpty				*/											-1,				// 11,312V Ubatt Empty
 	/* socCalcMode				*/											1,				// SoC calculation mode: 0(default)
 	/* cellRatedDischargeTime	*/											2,				// cell rated current discharge time [C/x]. For example, if 40Ah cell is rated as 0.5c, then rated discharge time is 2
@@ -223,5 +227,5 @@
 	/* lvpStart	*/															12000,			// uint16_t lvpStart; Spannung ab der die LOW Voltage Protection aktiv wird in mV
 	/* lvpStop	*/															12500,			// uint16_t lvpStop; Spannung ab der die LOW Voltage Protection wieder inaktiv wird
-	/* ovpStart	*/															15000,			// uint16_t  ovpStart; Spannung ab der die OVER Voltage Protection aktiv wird in mV
+	/* ovpStart	*/															14800,			// uint16_t  ovpStart; Spannung ab der die OVER Voltage Protection aktiv wird in mV
 	/* ovpStop	*/															14000,			// uint16_t  ovpStop; Spannung ab der die OVER Voltage Protection wieder inaktiv wird
 
@@ -238,10 +242,10 @@
 #error No valid device type
 #endif
-	/* chargeStopHighTemperatureStart	*/									7500,			// 80°C int16_t chargeStopHighTemperatureStart;	Abschalttemperatur Ladung wegen zu hoher Temperatur
-	/* chargeStopHighTemperatureStop	*/									7000,			// 75°C int16_t chargeStopHighTemperatureStop;	Wiedereinschalttemperatur
-	/* chargeStopLowTemperatureStart	*/									-3500,			// -35°C int16_t chargeStopLowTemperatureStart;	Abschalttemperatur Ladung wegen zu niedriger Temperatur
-	/* chargeStopLowTemperatureStop		*/									-3000,			// -30°C int16_t chargeStopLowTemperatureStop; Wiedereinschalttemperatur
-	/* dischargeStopHighTemperatureStart*/									7500,			// 80°C int16_t dischargeStopHighTemperatureStart; Abschalttemperatur Entladung wegen zu hoher Temperatur
-	/* dischargeStopHighTemperatureStop	*/									7000,			// 75°C int16_t dischargeStopHighTemperatureStop; Wiedereinschalttemperatur
+	/* chargeStopHighTemperatureStart	*/									6000,			// 80°C int16_t chargeStopHighTemperatureStart;	Abschalttemperatur Ladung wegen zu hoher Temperatur
+	/* chargeStopHighTemperatureStop	*/									5500,			// 75°C int16_t chargeStopHighTemperatureStop;	Wiedereinschalttemperatur
+	/* chargeStopLowTemperatureStart	*/									-3000,			// -35°C int16_t chargeStopLowTemperatureStart;	Abschalttemperatur Ladung wegen zu niedriger Temperatur
+	/* chargeStopLowTemperatureStop		*/									-2500,			// -30°C int16_t chargeStopLowTemperatureStop; Wiedereinschalttemperatur
+	/* dischargeStopHighTemperatureStart*/									6000,			// 80°C int16_t dischargeStopHighTemperatureStart; Abschalttemperatur Entladung wegen zu hoher Temperatur
+	/* dischargeStopHighTemperatureStop	*/									5500,			// 75°C int16_t dischargeStopHighTemperatureStop; Wiedereinschalttemperatur
 	/* dischargeStopLowTemperatureStart	*/									-3500,			// -35°C int16_t dischargeStopLowTemperatureStart; Abschalttemperatur EntLadung wegen zu niedriger Temperatur
 	/* dischargeStopLowTemperatureStop	*/									-3000,			// -30°C int16_t dischargeStopLowTemperatureStop; Wiedereinschalttemperatur
@@ -330,4 +334,7 @@
   dataToStore->changedData.currentGain												= defaultEepromData.currentGain;
 
+  dataToStore->changedData.currentOffsetFast										= defaultEepromData.currentOffsetFast;
+  dataToStore->changedData.currentGainFast											= defaultEepromData.currentGainFast;
+
   // AH Counter Parameter
   dataToStore->changedData.cef														= defaultEepromData.cef;
@@ -403,5 +410,5 @@
 
   // Offset und Gain
-  dataToStore->changedData.currentOffset											= data->s.parameter.batteryCurrentOffset;
+  
   dataToStore->changedData.batteryCurrentOffsetRefTemperatureShunt					= data->s.parameter.batteryCurrentOffsetRefTemperatureShunt;
   dataToStore->changedData.batteryCurrentOffsetRefTemperatureChip					= data->s.parameter.batteryCurrentOffsetRefTemperatureChip;
@@ -411,5 +418,9 @@
   dataToStore->changedData.batteryCurrentOffsetTemperatureCalibrationTemperature	= data->s.parameter.batteryCurrentOffsetTemperatureCalibrationTemperature;
   dataToStore->changedData.batteryCurrentOffsetTemperatureCompensationFactor		= data->s.parameter.batteryCurrentOffsetTemperatureCompensationFactor;
+  dataToStore->changedData.currentOffset											= data->s.parameter.batteryCurrentOffset;
   dataToStore->changedData.currentGain												= data->s.parameter.batteryCurrentGainCorrectionFaktor;
+  dataToStore->changedData.currentOffsetFast										= data->s.parameter.batteryCurrentOffsetFast;
+  dataToStore->changedData.currentGainFast											= data->s.parameter.batteryCurrentGainCorrectionFaktorFast;
+  
   dataToStore->changedData.batteryCurrentGainRefTempShunt							= data->s.parameter.batteryCurrentGainRefTempShunt;
   dataToStore->changedData.batteryCurrentGainRefTempChip							= data->s.parameter.batteryCurrentGainRefTempChip;
@@ -520,4 +531,8 @@
   dataToStore->changedData.currentOffset = data->s.parameter.batteryCurrentOffset;
   dataToStore->changedData.currentGain = data->s.parameter.batteryCurrentGainCorrectionFaktor;
+
+  dataToStore->changedData.currentOffsetFast = data->s.parameter.batteryCurrentOffsetFast;
+  dataToStore->changedData.currentGainFast = data->s.parameter.batteryCurrentGainCorrectionFaktorFast;
+
 
   // AH COUNTER Einstellungen
@@ -616,4 +631,7 @@
   data->s.parameter.batteryCurrentGainCorrectionFaktor							= dataToStore->changedData.currentGain;
 
+  data->s.parameter.batteryCurrentOffsetFast									= dataToStore->changedData.currentOffsetFast;
+  data->s.parameter.batteryCurrentGainCorrectionFaktorFast						= dataToStore->changedData.currentGainFast;
+
   //Einstellungenm für AH counter
   data->s.parameter.cef															= dataToStore ->changedData.cef;
Index: trunk/fw_g473rct/SES/src/esr.c
===================================================================
--- trunk/fw_g473rct/SES/src/esr.c	(revision 25)
+++ trunk/fw_g473rct/SES/src/esr.c	(revision 26)
@@ -104,5 +104,5 @@
   {
     //ESR berechnen!
-    sys_data.s.values.esr = ( (double)dU / (double) dI) * 1000;
+    sys_data.s.values.esr = ( (double)dU / (double) dI) * 10000;
     last_refresh = sys_data.s.values.onTime;
 
@@ -129,5 +129,6 @@
 
   //Anzeige vor wieviel Sekunden zuletzt aktualisiert wurd.
-  sys_data.s.values.esrCalcTime = sys_data.s.values.onTime - last_refresh;
+  //Aktuell erfolgt nur die Anze der low speed Methode
+  //sys_data.s.values.esrCalcTime = sys_data.s.values.onTime - last_refresh;
 
 
@@ -153,15 +154,19 @@
 
   
-  //Suche Zeitpunkt der größten Änderung in I
+
 
   //Delta berechnen
   int32_t dI = maxI - minI;
-  int32_t dU = maxU - minU;
+  
+  //Nehme nicht mehr die gesamte maximale Differenz der Spannungen, sondern nehme das delt U wo auch das Delta I gemessen wurde
+  //Funktioniert nur bei Synchroner Messug von Strom und Spannung
+  //int32_t dU = maxU - minU;
+  int32_t dU = adc12Data[maxdIPos][1]  - adc12Data[minIPos][1];
 
   //Umrechnung in mV / mA
-  dI = dI * ((int64_t) VREF / FAST_CURRENT_SHUNT_RESISTOR /  FAST_CURRENT_I_SENSE_GAIN /  FAST_CURRENT_ADC_RESOLUTION);
+  dI = dI * ((double) VREF / FAST_CURRENT_SHUNT_RESISTOR /  FAST_CURRENT_I_SENSE_GAIN /  FAST_CURRENT_ADC_RESOLUTION);
   dI = dI * (sys_data.s.parameter.batteryCurrentGainCorrectionFaktor / 1000000.0);
 
-  dU = dU  * VREF * BATTERY_VOLTAGE_VOLTAGE_DIVIDER / BATTERY_VOLTAGE_ADC_RESOLUTION ;
+  dU = dU  * (double )VREF * BATTERY_VOLTAGE_VOLTAGE_DIVIDER / BATTERY_VOLTAGE_ADC_RESOLUTION ;
   
 
@@ -204,12 +209,12 @@
   }
 
-  if ((dIMaxPos < 5 ) || (dIMaxPos > (SAMPLE_ARRAY_SIZE-5) ))
-  {
-	return -3;
-  }
+  //if ((dIMaxPos < 5 ) || (dIMaxPos > (SAMPLE_ARRAY_SIZE-5) ))
+  //{
+//	return -3;
+ // }
 
 
   //ESR berechnen!
-  sys_data.s.values.esr_fast = ( (double)dU / (double) dI) * 1000;
+  sys_data.s.values.esr_fast = ( (double)dU / (double) dI) * 10000;
   last_refresh = sys_data.s.values.onTime;
 	
Index: trunk/fw_g473rct/SES/src/fast_current.c
===================================================================
--- trunk/fw_g473rct/SES/src/fast_current.c	(revision 25)
+++ trunk/fw_g473rct/SES/src/fast_current.c	(revision 26)
@@ -32,6 +32,23 @@
 
 //	--- GLOBALE FUNKTIONEN - bitte in Header dokumentieren------------------------
+void CurrentOffsetCal(uint32_t newVal)
+{
+  sys_data.s.parameter.batteryCurrentOffsetFast = newVal-FAST_CURRENT_ADC_OFFSET;
+}
 
+void CurrentGainCal(uint32_t newVal)
+{
+  double correction;
+  double valWithoutGainCorrection;
 
+  valWithoutGainCorrection = ((int32_t) newVal - FAST_CURRENT_ADC_OFFSET - sys_data.s.parameter.batteryCurrentOffsetFast) * VREF ;
+  valWithoutGainCorrection = valWithoutGainCorrection / FAST_CURRENT_ADC_RESOLUTION;
+  valWithoutGainCorrection = valWithoutGainCorrection / FAST_CURRENT_I_SENSE_GAIN ;
+  valWithoutGainCorrection = valWithoutGainCorrection / FAST_CURRENT_SHUNT_RESISTOR ;
+
+  correction = (double)  sys_data.s.parameter.batteryCurrentGainRefCurrent / valWithoutGainCorrection;
+  sys_data.s.parameter.batteryCurrentGainCorrectionFaktorFast = correction * 1000000;
+
+}
 void FAST_CURRENT_Exec(uint32_t newVal )
 {
@@ -39,9 +56,9 @@
   //Umrechung auf Strom
   double temp_current;
-  temp_current = ((int32_t) newVal - FAST_CURRENT_ADC_OFFSET) * VREF ;
+  temp_current = ((int32_t) newVal - FAST_CURRENT_ADC_OFFSET - sys_data.s.parameter.batteryCurrentOffsetFast) * VREF ;
   temp_current = temp_current / FAST_CURRENT_ADC_RESOLUTION;
   temp_current = temp_current / FAST_CURRENT_I_SENSE_GAIN ;
   temp_current = temp_current / FAST_CURRENT_SHUNT_RESISTOR ;
-  sys_data.s.values.fast_current = temp_current * (sys_data.s.parameter.batteryCurrentGainCorrectionFaktor / 1000000.0);
+  sys_data.s.values.fast_current = temp_current * (sys_data.s.parameter.batteryCurrentGainCorrectionFaktorFast / 1000000.0);
  
 }
Index: trunk/fw_g473rct/SES/src/modbus.c
===================================================================
--- trunk/fw_g473rct/SES/src/modbus.c	(revision 25)
+++ trunk/fw_g473rct/SES/src/modbus.c	(revision 26)
@@ -34,5 +34,5 @@
   #define ILLEGAL_DATA_VALUE              0x03
   #define SLAVE_DEVICE_FAILURE            0x04
-  #define SERVER_FAILURE                    0x04
+  #define SERVER_FAILURE                  0x04
   #define ACKNOWLEDGE                     0x05
   #define SLAVE_DEVICE_BUSY               0x06
@@ -41,5 +41,5 @@
   #define MEMORY_PARITY_ERROR             0x08
   #define GATEWAY_PROBLEM_PATH            0x0A
-  #define GATEWAY_PROBLEM_TARGET            0x0B
+  #define GATEWAY_PROBLEM_TARGET          0x0B
 
   /* Local Error codes */
@@ -185,5 +185,15 @@
 	
 	HAL_UART_EnableReceiverTimeout( usart);
-	HAL_UART_ReceiverTimeout_Config(usart,  3.5 * nrOfBitsPerChar);
+	if (baudrate <= 19200)
+	{
+	  HAL_UART_ReceiverTimeout_Config(usart,  3.5 * nrOfBitsPerChar);
+	}
+	else
+	{
+      uint32_t fixedDelayInBitDurations = (FAST_BAUDRATE_INTERFRAME_DELAY_us * baudrate) / 1000000UL + 1UL;
+	  HAL_UART_ReceiverTimeout_Config(usart, fixedDelayInBitDurations);
+	}
+
+
 	SET_BIT(usart->Instance->CR1, USART_CR1_RTOIE); 
 
Index: trunk/fw_g473rct/SES/src/sysdata.c
===================================================================
--- trunk/fw_g473rct/SES/src/sysdata.c	(revision 25)
+++ trunk/fw_g473rct/SES/src/sysdata.c	(revision 26)
@@ -13,9 +13,9 @@
 	// Gertetyp angeben
 #if (DEVICETYPE == 500)
-	sys_data.s.parameter.device_type			= 510;
+	sys_data.s.parameter.device_type			= 520;
 #elif (DEVICETYPE == 250)
-	sys_data.s.parameter.device_type			= 511;
+	sys_data.s.parameter.device_type			= 521;
 #elif (DEVICETYPE == 125)
-	sys_data.s.parameter.device_type			= 512;
+	sys_data.s.parameter.device_type			= 522;
 #else
 #error "No valid device type"
Index: trunk/fw_g473rct/SES/src/wh_counter.c
===================================================================
--- trunk/fw_g473rct/SES/src/wh_counter.c	(revision 25)
+++ trunk/fw_g473rct/SES/src/wh_counter.c	(revision 26)
@@ -78,15 +78,9 @@
   sys_data.s.values.mWhCounter = sys_data.s.values.mWsCounter / 3600LL;
 
-
-
-
   // Counting Total Power
   if (sys_data.s.values.batteryPower < 0)
   {
 	  totalDischarge += -sys_data.s.values.batteryPower;
-	  sys_data.s.values.dischargeTotalWh = totalDischarge / 3600000; //Umrechnung von mWs auf Wh
-
-
-	  sys_data.s.values.fullCyclesCnt = (uint16_t) ((sys_data.s.values.dischargeTotalAh * 1000) / sys_data.s.parameter.cellCapacity);
+	  sys_data.s.values.dischargeTotalWh = totalDischarge / 3600000; //Umrechnung von mWs auf Wh	
   }
   else
@@ -95,7 +89,4 @@
 	  sys_data.s.values.chargeTotalWh = totalCharge / 3600000; //Umrechnung von mWs auf Wh
   }
-
-
-
 }
 
