Mercurial > public > ostc4
comparison 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 |
comparison
equal
deleted
inserted
replaced
642:c737cf5d9067 | 643:5149cd644fbc |
---|---|
32 #include "batteryGasGauge.h" | 32 #include "batteryGasGauge.h" |
33 #include "stm32f4xx_hal.h" | 33 #include "stm32f4xx_hal.h" |
34 #include "scheduler.h" | 34 #include "scheduler.h" |
35 | 35 |
36 | 36 |
37 /* Use This compile switch to select the new charger status control implementation */ | |
38 /* #define ENABLE_CHARGER_STATUS_V2 */ | |
39 | |
37 #define CHARGE_IN_PIN GPIO_PIN_2 | 40 #define CHARGE_IN_PIN GPIO_PIN_2 |
38 #define CHARGE_IN_GPIO_PORT GPIOC | 41 #define CHARGE_IN_GPIO_PORT GPIOC |
39 #define CHARGE_IN_GPIO_ENABLE() __GPIOC_CLK_ENABLE() | 42 #define CHARGE_IN_GPIO_ENABLE() __GPIOC_CLK_ENABLE() |
40 | 43 |
41 #define CHARGE_OUT_PIN GPIO_PIN_1 | 44 #define CHARGE_OUT_PIN GPIO_PIN_1 |
42 #define CHARGE_OUT_GPIO_PORT GPIOC | 45 #define CHARGE_OUT_GPIO_PORT GPIOC |
43 #define CHARGE_OUT_GPIO_ENABLE() __GPIOC_CLK_ENABLE() | 46 #define CHARGE_OUT_GPIO_ENABLE() __GPIOC_CLK_ENABLE() |
44 | 47 |
48 #define CHARGER_DEBOUNCE_SECONDS (120u) /* 120 seconds used to avoid problems with charger interrupts / disconnections */ | |
45 | 49 |
46 uint8_t battery_i_charge_status = 0; | 50 uint8_t battery_i_charge_status = 0; |
47 uint8_t battery_charger_counter = 0; | 51 uint16_t battery_charger_counter = 0; |
52 | |
53 #ifdef ENABLE_CHARGER_STATUS_V2 | |
54 typedef enum | |
55 { | |
56 Charger_NotConnected = 0, /* This is identified reading CHARGE_IN_PIN == HIGH */ | |
57 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) */ | |
58 Charger_Active, /* Charging identified by CHARGE_IN_PIN == LOW for a certain time */ | |
59 Charger_Finished, | |
60 Charger_LostConnection /* Intermediate state to debounce disconnecting events (including charging error state like over temperature) */ | |
61 } chargerState_t; | |
62 | |
63 static chargerState_t batteryChargerState = Charger_NotConnected; | |
64 #endif | |
48 | 65 |
49 /* can be 0, 1 or 255 | 66 /* can be 0, 1 or 255 |
50 * 0 is disconnected | 67 * 0 is disconnected |
51 * 1 is charging | 68 * 1 is charging |
52 * 255 is full | 69 * 255 is full |
120 to prevent the short disconnections the battery_charger_counter is used too including | 137 to prevent the short disconnections the battery_charger_counter is used too including |
121 upcounting again while battery_i_charge_status == 255 and the connection is established | 138 upcounting again while battery_i_charge_status == 255 and the connection is established |
122 | 139 |
123 */ | 140 */ |
124 | 141 |
125 void battery_charger_get_status_and_contral_battery_gas_gauge(uint8_t inSleepModeLessCounts) | 142 void battery_charger_get_status_and_contral_battery_gas_gauge(uint8_t cycleTimeBase) |
126 { | 143 { |
144 #ifdef ENABLE_CHARGER_STATUS_V2 | |
145 static uint8_t notifyChargeComplete = 0; | |
146 #endif | |
147 | |
127 #ifdef OSTC_ON_DISCOVERY_HARDWARE | 148 #ifdef OSTC_ON_DISCOVERY_HARDWARE |
128 return; | 149 return; |
129 #endif | 150 #endif |
130 | 151 |
152 #ifdef ENABLE_CHARGER_STATUS_V2 | |
153 /* on disconnection or while disconnected */ | |
154 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN)) | |
155 { | |
156 switch(batteryChargerState) | |
157 { | |
158 case Charger_Active: global.dataSendToMaster.chargeStatus = CHARGER_lostConnection; | |
159 global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection; | |
160 batteryChargerState = Charger_LostConnection; | |
161 battery_charger_counter = CHARGER_DEBOUNCE_SECONDS; | |
162 | |
163 if(get_voltage() >= 4.1f) /* the charger stops charging when charge current is 1/10. */ | |
164 { /* Basically it is OK to rate a charging as complete if a defined voltage is reached */ | |
165 batteryChargerState = Charger_Finished; | |
166 global.dataSendToMaster.chargeStatus = CHARGER_complete; | |
167 global.deviceDataSendToMaster.chargeStatus = CHARGER_complete; | |
168 battery_charger_counter = 30; | |
169 notifyChargeComplete = 1; | |
170 } | |
171 break; | |
172 case Charger_WarmUp: | |
173 case Charger_Finished: | |
174 case Charger_LostConnection: if(battery_charger_counter >= cycleTimeBase) | |
175 { | |
176 battery_charger_counter -= cycleTimeBase; | |
177 global.dataSendToMaster.chargeStatus = CHARGER_lostConnection; | |
178 global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection; | |
179 batteryChargerState = Charger_LostConnection; | |
180 } | |
181 else | |
182 { | |
183 battery_charger_counter = 0; | |
184 global.dataSendToMaster.chargeStatus = CHARGER_off; | |
185 global.deviceDataSendToMaster.chargeStatus = CHARGER_off; | |
186 | |
187 if(notifyChargeComplete) | |
188 { | |
189 battery_gas_gauge_set_charge_full(); | |
190 scheduleUpdateDeviceDataChargerFull(); | |
191 notifyChargeComplete = 0; | |
192 } | |
193 batteryChargerState = Charger_NotConnected; | |
194 } | |
195 break; | |
196 default: break; | |
197 } | |
198 } | |
199 else | |
200 { | |
201 /* connected */ | |
202 /* wait for disconnection to write and reset */ | |
203 switch(batteryChargerState) | |
204 { | |
205 case Charger_NotConnected: battery_i_charge_status = 1; | |
206 battery_charger_counter = 0; | |
207 batteryChargerState = Charger_WarmUp; | |
208 break; | |
209 case Charger_LostConnection: batteryChargerState = Charger_Active; | |
210 break; | |
211 case Charger_WarmUp: battery_charger_counter += cycleTimeBase; | |
212 if(battery_charger_counter >= CHARGER_DEBOUNCE_SECONDS ) | |
213 { | |
214 battery_i_charge_status = 2; | |
215 scheduleUpdateDeviceDataChargerCharging(); | |
216 batteryChargerState = Charger_Active; | |
217 } | |
218 /* no break */ | |
219 case Charger_Finished: | |
220 case Charger_Active: global.dataSendToMaster.chargeStatus = CHARGER_running; | |
221 global.deviceDataSendToMaster.chargeStatus = CHARGER_running; | |
222 | |
223 /* drive the output pin high to determine the state of the charger */ | |
224 GPIO_InitTypeDef GPIO_InitStructure; | |
225 GPIO_InitStructure.Pin = CHARGE_OUT_PIN; | |
226 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | |
227 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
228 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
229 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); | |
230 HAL_GPIO_WritePin(CHARGE_OUT_GPIO_PORT, CHARGE_OUT_PIN,GPIO_PIN_SET); | |
231 HAL_Delay(1); | |
232 | |
233 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN)) /* high => charger stopped charging */ | |
234 { | |
235 batteryChargerState = Charger_Finished; | |
236 global.dataSendToMaster.chargeStatus = CHARGER_complete; | |
237 global.deviceDataSendToMaster.chargeStatus = CHARGER_complete; | |
238 battery_charger_counter = 30; | |
239 notifyChargeComplete = 1; | |
240 } | |
241 else | |
242 { | |
243 if(batteryChargerState == Charger_Finished) /* voltage dropped below the hysteresis again => charging restarted */ | |
244 { | |
245 batteryChargerState = Charger_Active; | |
246 notifyChargeComplete = 0; | |
247 } | |
248 } | |
249 | |
250 /* restore high impedance to be able to detect disconnection */ | |
251 GPIO_InitStructure.Pin = CHARGE_OUT_PIN; | |
252 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; | |
253 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
254 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
255 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); | |
256 break; | |
257 | |
258 default: /* wait for disconnection */ | |
259 break; | |
260 } | |
261 } | |
262 #else | |
131 /* on disconnection or while disconnected */ | 263 /* on disconnection or while disconnected */ |
132 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN)) | 264 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN)) |
133 { | 265 { |
134 if(battery_charger_counter) | 266 if(battery_charger_counter) |
135 { | 267 { |
161 if(battery_i_charge_status == 255) | 293 if(battery_i_charge_status == 255) |
162 { | 294 { |
163 global.dataSendToMaster.chargeStatus = CHARGER_complete; | 295 global.dataSendToMaster.chargeStatus = CHARGER_complete; |
164 global.deviceDataSendToMaster.chargeStatus = CHARGER_complete; | 296 global.deviceDataSendToMaster.chargeStatus = CHARGER_complete; |
165 | 297 |
166 if((inSleepModeLessCounts && (battery_charger_counter < 127+5)) || (battery_charger_counter < 127+20)) | 298 if(((cycleTimeBase > 1) && (battery_charger_counter < 127+5)) || (battery_charger_counter < 127+20)) |
167 battery_charger_counter++; | 299 battery_charger_counter++; |
168 return; | 300 return; |
169 } | 301 } |
170 | 302 |
171 if(battery_charger_counter == 0) | 303 if(battery_charger_counter == 0) |
174 /* charger is connected and didn't signal full yet */ | 306 /* charger is connected and didn't signal full yet */ |
175 global.dataSendToMaster.chargeStatus = CHARGER_running; | 307 global.dataSendToMaster.chargeStatus = CHARGER_running; |
176 global.deviceDataSendToMaster.chargeStatus = CHARGER_running; | 308 global.deviceDataSendToMaster.chargeStatus = CHARGER_running; |
177 | 309 |
178 GPIO_InitTypeDef GPIO_InitStructure; | 310 GPIO_InitTypeDef GPIO_InitStructure; |
179 GPIO_InitStructure.Pin = CHARGE_OUT_PIN; | 311 GPIO_InitStructure.Pin = CHARGE_OUT_PIN; |
180 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; | 312 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
181 GPIO_InitStructure.Pull = GPIO_NOPULL; | 313 GPIO_InitStructure.Pull = GPIO_NOPULL; |
182 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | 314 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; |
183 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); | 315 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); |
184 HAL_GPIO_WritePin(CHARGE_OUT_GPIO_PORT, CHARGE_OUT_PIN,GPIO_PIN_SET); | 316 HAL_GPIO_WritePin(CHARGE_OUT_GPIO_PORT, CHARGE_OUT_PIN,GPIO_PIN_SET); |
185 HAL_Delay(1); | 317 HAL_Delay(1); |
186 | 318 |
187 | 319 |
188 if(battery_charger_counter < 120) | 320 if(battery_charger_counter < 120) |
189 { | 321 { |
190 if(!inSleepModeLessCounts) | 322 if(cycleTimeBase == 1) |
191 battery_charger_counter++; | 323 battery_charger_counter++; |
192 else | 324 else |
193 { | 325 { |
194 battery_charger_counter += 30; | 326 battery_charger_counter += 30; |
195 if(battery_charger_counter >= 127) | 327 if(battery_charger_counter >= 127) |
210 if(battery_charger_counter >= 127) | 342 if(battery_charger_counter >= 127) |
211 { | 343 { |
212 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN) || (get_voltage() >= 4.1f)) | 344 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN) || (get_voltage() >= 4.1f)) |
213 { | 345 { |
214 battery_charger_counter++; | 346 battery_charger_counter++; |
215 if((inSleepModeLessCounts && (battery_charger_counter > 127+5)) || (battery_charger_counter > 127+20)) | 347 if(((cycleTimeBase > 1) && (battery_charger_counter > 127+5)) || (battery_charger_counter > 127+20)) |
216 { | 348 { |
217 battery_charger_counter = 127; | 349 battery_charger_counter = 127; |
218 if(get_voltage() >= 4.1f) | 350 if(get_voltage() >= 4.1f) |
219 { | 351 { |
220 battery_i_charge_status = 255; | 352 battery_i_charge_status = 255; |
230 GPIO_InitStructure.Pin = CHARGE_OUT_PIN; | 362 GPIO_InitStructure.Pin = CHARGE_OUT_PIN; |
231 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; | 363 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; |
232 GPIO_InitStructure.Pull = GPIO_NOPULL; | 364 GPIO_InitStructure.Pull = GPIO_NOPULL; |
233 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | 365 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; |
234 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); | 366 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); |
367 #endif | |
235 } | 368 } |
236 | 369 |
237 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ | 370 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ |