diff Small_CPU/Src/batteryCharger.c @ 686:b1e24513b83e Betatest

Bugfix Battery charge complete event: In previous version it could happen that the battery charge state was set to 100% even the battery was not completly charged. Rootcause was that the external charger IC signals sometimes completion because e.g. the connection between charger unit and OSTC is disconnected within a short time slot. This may happen while the user is trying to get the OSTC in a good charging position. To avoid this the state machine now checks the voltage everytime for valid charging complete range before a charging complete is signaled.
author Ideenmodellierer
date Fri, 05 Aug 2022 14:56:17 +0200
parents 079bb5b22c06
children d32901746950
line wrap: on
line diff
--- a/Small_CPU/Src/batteryCharger.c	Wed Aug 03 18:19:03 2022 +0200
+++ b/Small_CPU/Src/batteryCharger.c	Fri Aug 05 14:56:17 2022 +0200
@@ -151,7 +151,7 @@
 
 	if(batteryChargerState == Charger_ColdStart)	/* wait for the first valid voltage meassurement */
 	{
-		if(global.lifeData.battery_voltage != BATTERY_DEFAULT_VOLTAGE)
+		if((global.lifeData.battery_voltage != BATTERY_DEFAULT_VOLTAGE) && (global.lifeData.battery_voltage < BATTERY_CHARGER_CONNECTED_VOLTAGE))
 		{
 			if(global.lifeData.battery_voltage > BATTERY_ENDOF_CHARGE_VOLTAGE) 						/* Voltage close to full state => maybe new battery inserted 	*/
 			{
@@ -166,29 +166,35 @@
 		{
 			switch(batteryChargerState)
 			{
+				case Charger_WarmUp:
 				case Charger_Active:				global.dataSendToMaster.chargeStatus = CHARGER_lostConnection;
 													global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection;
 													batteryChargerState = Charger_LostConnection;
-													battery_charger_counter = CHARGER_DEBOUNCE_SECONDS;
+													if(cycleTimeBase > CHARGER_DEBOUNCE_SECONDS)	/* adapt connection lost detection to sleep mode */
+													{
+														battery_charger_counter = cycleTimeBase + 1;
+													}
+													else
+													{
+														battery_charger_counter = CHARGER_DEBOUNCE_SECONDS;
+													}
 											break;
+				case Charger_Finished:				battery_charger_counter = 0;
+													batteryChargerState = Charger_LostConnection;
+					/* no break */
 				case Charger_LostConnection:		/* the charger stops charging when charge current is 1/10 	*/
 													/* Basically it is OK to rate a charging as complete if a defined voltage is reached */
-													if(((battery_gas_gauge_isChargeValueValid() == 0) || (global.lifeData.battery_charge < 90)) && (get_voltage() >= BATTERY_ENDOF_CHARGE_VOLTAGE))
+													if(((battery_gas_gauge_isChargeValueValid() == 0) || (global.lifeData.battery_charge < 90)) && (get_voltage() >= BATTERY_ENDOF_CHARGE_VOLTAGE) && (get_voltage() < BATTERY_CHARGER_CONNECTED_VOLTAGE))
 													{
-														batteryChargerState = Charger_Finished;
-														global.dataSendToMaster.chargeStatus = CHARGER_complete;
-														global.deviceDataSendToMaster.chargeStatus = CHARGER_complete;
 														notifyChargeComplete = 1;
-														battery_charger_counter = 0;
 													}
-				/* no break */
-				case Charger_WarmUp:
-				case Charger_Finished:				if(battery_charger_counter >= cycleTimeBase)
+													else
+													{
+														notifyChargeComplete = 0;
+													}
+													if(battery_charger_counter >= cycleTimeBase)
 													{
 														battery_charger_counter -= cycleTimeBase;
-														global.dataSendToMaster.chargeStatus = CHARGER_lostConnection;
-														global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection;
-														batteryChargerState = Charger_LostConnection;
 													}
 													else
 													{
@@ -202,10 +208,6 @@
 															battery_gas_gauge_set_charge_full();
 															scheduleUpdateDeviceDataChargerFull();
 															notifyChargeComplete = 0;
-															if(cycleTimeBase > 2)
-															{
-																HAL_Delay(50);		/* I2C operations are pending in the background. Wait to avoid data loose in caused to potential change to sleep state */
-															}
 														}
 														batteryChargerState = Charger_NotConnected;
 													}