comparison Small_CPU/Src/batteryCharger.c @ 668:079bb5b22c06 Betatest

Rework charge cycle: The charge counter is increasing decreasing also if the real value is maybe not defined (definition is done for example by a completed charging cycle). This caused some problems with invalid displayed charge per centage values. To avoid this the state of an unknow counter value was introduced. The indication is done by converting the counter into a negativ value.
author Ideenmodellierer
date Sat, 12 Mar 2022 22:48:45 +0100
parents 1b995079c045
children b1e24513b83e
comparison
equal deleted inserted replaced
667:8c3d495afc69 668:079bb5b22c06
37 /* Use This compile switch to select the new charger status control implementation */ 37 /* Use This compile switch to select the new charger status control implementation */
38 #define ENABLE_CHARGER_STATUS_V2 38 #define ENABLE_CHARGER_STATUS_V2
39 39
40 #define CHARGER_DEBOUNCE_SECONDS (6u) /* 6 seconds used to avoid problems with charger interrupts / disconnections */ 40 #define CHARGER_DEBOUNCE_SECONDS (6u) /* 6 seconds used to avoid problems with charger interrupts / disconnections */
41 41
42 uint8_t battery_i_charge_status = 0; 42 static uint8_t battery_i_charge_status = 0;
43 uint16_t battery_charger_counter = 0; 43 static uint16_t battery_charger_counter = 0;
44 44
45 #ifdef ENABLE_CHARGER_STATUS_V2 45 #ifdef ENABLE_CHARGER_STATUS_V2
46 static chargerState_t batteryChargerState = Charger_NotConnected; 46 static chargerState_t batteryChargerState = Charger_NotConnected;
47 #endif 47 #endif
48 48
139 139
140 void battery_charger_get_status_and_contral_battery_gas_gauge(uint8_t cycleTimeBase) 140 void battery_charger_get_status_and_contral_battery_gas_gauge(uint8_t cycleTimeBase)
141 { 141 {
142 #ifdef ENABLE_CHARGER_STATUS_V2 142 #ifdef ENABLE_CHARGER_STATUS_V2
143 static uint8_t notifyChargeComplete = 0; 143 static uint8_t notifyChargeComplete = 0;
144 static float chargeValueAtStart = 0;
145 #endif 144 #endif
146 145
147 #ifdef OSTC_ON_DISCOVERY_HARDWARE 146 #ifdef OSTC_ON_DISCOVERY_HARDWARE
148 return; 147 return;
149 #endif 148 #endif
155 if(global.lifeData.battery_voltage != BATTERY_DEFAULT_VOLTAGE) 154 if(global.lifeData.battery_voltage != BATTERY_DEFAULT_VOLTAGE)
156 { 155 {
157 if(global.lifeData.battery_voltage > BATTERY_ENDOF_CHARGE_VOLTAGE) /* Voltage close to full state => maybe new battery inserted */ 156 if(global.lifeData.battery_voltage > BATTERY_ENDOF_CHARGE_VOLTAGE) /* Voltage close to full state => maybe new battery inserted */
158 { 157 {
159 battery_gas_gauge_set_charge_full(); 158 battery_gas_gauge_set_charge_full();
160 }
161 else /* unknown state => reset to 0% */
162 {
163 battery_gas_gauge_set(0);
164 } 159 }
165 batteryChargerState = Charger_NotConnected; 160 batteryChargerState = Charger_NotConnected;
166 } 161 }
167 } 162 }
168 else 163 else
174 case Charger_Active: global.dataSendToMaster.chargeStatus = CHARGER_lostConnection; 169 case Charger_Active: global.dataSendToMaster.chargeStatus = CHARGER_lostConnection;
175 global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection; 170 global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection;
176 batteryChargerState = Charger_LostConnection; 171 batteryChargerState = Charger_LostConnection;
177 battery_charger_counter = CHARGER_DEBOUNCE_SECONDS; 172 battery_charger_counter = CHARGER_DEBOUNCE_SECONDS;
178 break; 173 break;
179 case Charger_LostConnection: if(get_voltage() >= BATTERY_ENDOF_CHARGE_VOLTAGE) /* the charger stops charging when charge current is 1/10 */ 174 case Charger_LostConnection: /* the charger stops charging when charge current is 1/10 */
180 /* Basically it is OK to rate a charging as complete if a defined voltage is reached */ 175 /* Basically it is OK to rate a charging as complete if a defined voltage is reached */
176 if(((battery_gas_gauge_isChargeValueValid() == 0) || (global.lifeData.battery_charge < 90)) && (get_voltage() >= BATTERY_ENDOF_CHARGE_VOLTAGE))
181 { 177 {
182 batteryChargerState = Charger_Finished; 178 batteryChargerState = Charger_Finished;
183 global.dataSendToMaster.chargeStatus = CHARGER_complete; 179 global.dataSendToMaster.chargeStatus = CHARGER_complete;
184 global.deviceDataSendToMaster.chargeStatus = CHARGER_complete; 180 global.deviceDataSendToMaster.chargeStatus = CHARGER_complete;
185 notifyChargeComplete = 1; 181 notifyChargeComplete = 1;
182 battery_charger_counter = 0;
186 } 183 }
187 /* no break */ 184 /* no break */
188 case Charger_WarmUp: 185 case Charger_WarmUp:
189 case Charger_Finished: if(battery_charger_counter >= cycleTimeBase) 186 case Charger_Finished: if(battery_charger_counter >= cycleTimeBase)
190 { 187 {
208 if(cycleTimeBase > 2) 205 if(cycleTimeBase > 2)
209 { 206 {
210 HAL_Delay(50); /* I2C operations are pending in the background. Wait to avoid data loose in caused to potential change to sleep state */ 207 HAL_Delay(50); /* I2C operations are pending in the background. Wait to avoid data loose in caused to potential change to sleep state */
211 } 208 }
212 } 209 }
213 else
214 {
215 if(chargeValueAtStart < 1.0) /* charging started with unknown value => reset charge state reported by charger */
216 {
217 battery_gas_gauge_set(0);
218 }
219 }
220 batteryChargerState = Charger_NotConnected; 210 batteryChargerState = Charger_NotConnected;
221 } 211 }
222 break; 212 break;
223 default: break; 213 default: break;
224 } 214 }
230 switch(batteryChargerState) 220 switch(batteryChargerState)
231 { 221 {
232 case Charger_NotConnected: battery_i_charge_status = 1; 222 case Charger_NotConnected: battery_i_charge_status = 1;
233 battery_charger_counter = 0; 223 battery_charger_counter = 0;
234 batteryChargerState = Charger_WarmUp; 224 batteryChargerState = Charger_WarmUp;
235 chargeValueAtStart = global.lifeData.battery_charge;
236 break; 225 break;
237 case Charger_LostConnection: batteryChargerState = Charger_Active; 226 case Charger_LostConnection: batteryChargerState = Charger_Active;
238 break; 227 break;
239 case Charger_WarmUp: battery_charger_counter += cycleTimeBase; 228 case Charger_WarmUp: battery_charger_counter += cycleTimeBase;
240 if(battery_charger_counter >= CHARGER_DEBOUNCE_SECONDS ) 229 if(battery_charger_counter >= CHARGER_DEBOUNCE_SECONDS )
266 battery_charger_counter = 30; 255 battery_charger_counter = 30;
267 notifyChargeComplete = 1; 256 notifyChargeComplete = 1;
268 } 257 }
269 else 258 else
270 { 259 {
260 if(global.lifeData.battery_charge > 100.0) /* still charging but indicator is set to full => decrease to 99% to keep count increasing */
261 {
262 battery_gas_gauge_set(99.0);
263 }
271 if(batteryChargerState == Charger_Finished) /* voltage dropped below the hysteresis again => charging restarted */ 264 if(batteryChargerState == Charger_Finished) /* voltage dropped below the hysteresis again => charging restarted */
272 { 265 {
273 batteryChargerState = Charger_Active; 266 batteryChargerState = Charger_Active;
274 notifyChargeComplete = 0; 267 notifyChargeComplete = 0;
275 } 268 }