diff Small_CPU/Src/batteryCharger.c @ 643:5149cd644fbc

Reimplemented charger status generation (deactivated): Sometimes the original implementation never reached the state fully charged for unknown reason. The code already contained a function which set the status to fully charger even it was not signaled by the charger (in case voltage is > 4.1V). The new implementation changed the handling of the detection and the OSTC showing the problem now reaches the fully charger status. As the new implementation is still under test it is deactivated => original implementation is active per default
author Ideenmodellierer
date Wed, 24 Mar 2021 22:06:00 +0100
parents 5f11787b4f42
children ebe3fc302ab8
line wrap: on
line diff
--- a/Small_CPU/Src/batteryCharger.c	Wed Mar 24 21:28:41 2021 +0100
+++ b/Small_CPU/Src/batteryCharger.c	Wed Mar 24 22:06:00 2021 +0100
@@ -34,6 +34,9 @@
 #include "scheduler.h"
 
 
+/* Use This compile switch to select the new charger status control implementation */
+/* #define ENABLE_CHARGER_STATUS_V2 */
+
 #define CHARGE_IN_PIN							GPIO_PIN_2
 #define CHARGE_IN_GPIO_PORT				GPIOC
 #define CHARGE_IN_GPIO_ENABLE()		__GPIOC_CLK_ENABLE()
@@ -42,9 +45,23 @@
 #define CHARGE_OUT_GPIO_PORT			GPIOC
 #define CHARGE_OUT_GPIO_ENABLE()	__GPIOC_CLK_ENABLE()
 
+#define CHARGER_DEBOUNCE_SECONDS	(120u)		/* 120 seconds used to avoid problems with charger interrupts / disconnections */
 
 uint8_t battery_i_charge_status = 0;
-uint8_t battery_charger_counter = 0;
+uint16_t battery_charger_counter = 0;
+
+#ifdef ENABLE_CHARGER_STATUS_V2
+typedef enum
+{
+	Charger_NotConnected = 0,		/* This is identified reading CHARGE_IN_PIN == HIGH */
+	Charger_WarmUp,					/* Charging started but counter did not yet reach a certain limit (used to debounce connect / disconnect events to avoid multiple increases of statistic charging cycle counter) */
+	Charger_Active,					/* Charging identified by  CHARGE_IN_PIN == LOW for a certain time */
+	Charger_Finished,
+	Charger_LostConnection			/* Intermediate state to debounce disconnecting events (including charging error state like over temperature) */
+} chargerState_t;
+
+static chargerState_t batteryChargerState = Charger_NotConnected;
+#endif
 
 /* can be 0, 1 or 255
  * 0 is disconnected
@@ -122,12 +139,127 @@
 
  */
 
-void battery_charger_get_status_and_contral_battery_gas_gauge(uint8_t inSleepModeLessCounts)
+void battery_charger_get_status_and_contral_battery_gas_gauge(uint8_t cycleTimeBase)
 {
+#ifdef ENABLE_CHARGER_STATUS_V2
+	static uint8_t notifyChargeComplete = 0;
+#endif 
+
 	#ifdef OSTC_ON_DISCOVERY_HARDWARE
 		return;
 	#endif
 	
+#ifdef ENABLE_CHARGER_STATUS_V2
+	/* on disconnection or while disconnected */
+	if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN))
+	{
+		switch(batteryChargerState)
+		{
+			case Charger_Active:				global.dataSendToMaster.chargeStatus = CHARGER_lostConnection;
+												global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection;
+												batteryChargerState = Charger_LostConnection;
+												battery_charger_counter = CHARGER_DEBOUNCE_SECONDS;
+
+												if(get_voltage() >= 4.1f)			/* 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 */
+													batteryChargerState = Charger_Finished;
+													global.dataSendToMaster.chargeStatus = CHARGER_complete;
+													global.deviceDataSendToMaster.chargeStatus = CHARGER_complete;
+													battery_charger_counter = 30;
+													notifyChargeComplete = 1;
+												}
+										break;
+			case Charger_WarmUp:
+			case Charger_Finished:
+			case Charger_LostConnection:		if(battery_charger_counter >= cycleTimeBase)
+												{
+													battery_charger_counter -= cycleTimeBase;
+													global.dataSendToMaster.chargeStatus = CHARGER_lostConnection;
+													global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection;
+													batteryChargerState = Charger_LostConnection;
+												}
+												else
+												{
+													battery_charger_counter = 0;
+													global.dataSendToMaster.chargeStatus = CHARGER_off;
+													global.deviceDataSendToMaster.chargeStatus = CHARGER_off;
+
+													if(notifyChargeComplete)
+													{
+														battery_gas_gauge_set_charge_full();
+														scheduleUpdateDeviceDataChargerFull();
+														notifyChargeComplete = 0;
+													}
+													batteryChargerState = Charger_NotConnected;
+												}
+										break;
+			default: break;
+		}
+	}
+	else
+	{
+		/* connected */
+		/* wait for disconnection to write and reset */
+		switch(batteryChargerState)
+		{
+				case Charger_NotConnected:		battery_i_charge_status = 1;
+												battery_charger_counter = 0;
+												batteryChargerState = Charger_WarmUp;
+										break;
+				case Charger_LostConnection:		batteryChargerState = Charger_Active;
+										break;
+				case Charger_WarmUp:			battery_charger_counter += cycleTimeBase;
+												if(battery_charger_counter >= CHARGER_DEBOUNCE_SECONDS )
+												{
+													battery_i_charge_status = 2;
+													scheduleUpdateDeviceDataChargerCharging();
+													batteryChargerState = Charger_Active;
+												}
+						/* no break */
+				case Charger_Finished:
+				case Charger_Active:			global.dataSendToMaster.chargeStatus = CHARGER_running;
+												global.deviceDataSendToMaster.chargeStatus = CHARGER_running;
+
+												/* drive the output pin high to determine the state of the charger */
+												GPIO_InitTypeDef   GPIO_InitStructure;
+												GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
+												GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
+												GPIO_InitStructure.Pull = GPIO_NOPULL;
+												GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
+												HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
+												HAL_GPIO_WritePin(CHARGE_OUT_GPIO_PORT, CHARGE_OUT_PIN,GPIO_PIN_SET);
+												HAL_Delay(1);
+
+												if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN))		/* high => charger stopped charging */
+												{
+													batteryChargerState = Charger_Finished;
+													global.dataSendToMaster.chargeStatus = CHARGER_complete;
+													global.deviceDataSendToMaster.chargeStatus = CHARGER_complete;
+													battery_charger_counter = 30;
+													notifyChargeComplete = 1;
+												}
+												else
+												{
+													if(batteryChargerState == Charger_Finished)				/* voltage dropped below the hysteresis again => charging restarted */
+													{
+														batteryChargerState = Charger_Active;
+														notifyChargeComplete = 0;
+													}
+												}
+
+												/* restore high impedance to be able to detect disconnection */
+												GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
+												GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
+												GPIO_InitStructure.Pull = GPIO_NOPULL;
+												GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
+												HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
+										break;
+
+				default:						/* wait for disconnection */
+					break;
+		}
+	}
+#else
 	/* on disconnection or while disconnected */
 	if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN))
 	{
@@ -163,7 +295,7 @@
 		global.dataSendToMaster.chargeStatus = CHARGER_complete;
 		global.deviceDataSendToMaster.chargeStatus = CHARGER_complete;
 		
-		if((inSleepModeLessCounts && (battery_charger_counter < 127+5)) || (battery_charger_counter < 127+20))
+		if(((cycleTimeBase > 1) && (battery_charger_counter < 127+5)) || (battery_charger_counter < 127+20))
 		battery_charger_counter++;
 		return;
 	}
@@ -176,18 +308,18 @@
 	global.deviceDataSendToMaster.chargeStatus = CHARGER_running;
 
 	GPIO_InitTypeDef   GPIO_InitStructure;
-  GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
-  GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
-  GPIO_InitStructure.Pull = GPIO_NOPULL;
-  GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
-  HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); 
+    GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
+    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
+    GPIO_InitStructure.Pull = GPIO_NOPULL;
+    GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
+    HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); 
 	HAL_GPIO_WritePin(CHARGE_OUT_GPIO_PORT, CHARGE_OUT_PIN,GPIO_PIN_SET);
 	HAL_Delay(1);
 
 	
 	if(battery_charger_counter < 120)
 	{
-		if(!inSleepModeLessCounts)
+		if(cycleTimeBase == 1)
 			battery_charger_counter++;
 		else
 		{
@@ -212,7 +344,7 @@
 		if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN) || (get_voltage() >= 4.1f))
 		{
 			battery_charger_counter++;
-			if((inSleepModeLessCounts && (battery_charger_counter > 127+5)) || (battery_charger_counter > 127+20))
+			if(((cycleTimeBase > 1) && (battery_charger_counter > 127+5)) || (battery_charger_counter > 127+20))
 			{
 				battery_charger_counter = 127;
 				if(get_voltage() >= 4.1f)
@@ -232,6 +364,7 @@
   GPIO_InitStructure.Pull = GPIO_NOPULL;
   GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
   HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); 
+#endif
 }
 
 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/