Mercurial > public > ostc4
annotate Small_CPU/Src/batteryCharger.c @ 644:ebe3fc302ab8
Compile with ENABLE_CHARGER_STATUS_V2
author | heinrichs weikamp |
---|---|
date | Tue, 30 Mar 2021 10:34:12 +0200 |
parents | 5149cd644fbc |
children | e58f81cb25a7 |
rev | line source |
---|---|
38 | 1 /** |
2 ****************************************************************************** | |
3 * @file batteryCharger.c | |
4 * @author heinrichs weikamp gmbh | |
5 * @date 09-Dec-2014 | |
6 * @version V0.0.1 | |
7 * @since 09-Dec-2014 | |
8 * @brief LTC4054 Battery Charger | |
9 * | |
10 @verbatim | |
11 ============================================================================== | |
12 ##### How to use ##### | |
13 ============================================================================== | |
14 | |
15 The bq5105x provides one status output, CHG. This output is an open-drain NMOS device that is rated to 20 V. | |
16 The open-drain FET connected to the CHG pin will be turned on whenever the output (BAT) of the charger is | |
17 enabled. As a note, the output of the charger supply will not be enabled if the VRECT-REG does not converge to the | |
18 no-load target voltage. | |
19 | |
644 | 20 CHG F4 7 O Open-drain output � active when BAT is enabled. Float if not used. |
38 | 21 |
22 @endverbatim | |
23 ****************************************************************************** | |
24 * @attention | |
25 * | |
26 * <h2><center>© COPYRIGHT(c) 2015 heinrichs weikamp</center></h2> | |
27 * | |
28 ****************************************************************************** | |
29 */ | |
30 /* Includes ------------------------------------------------------------------*/ | |
31 #include "batteryCharger.h" | |
32 #include "batteryGasGauge.h" | |
33 #include "stm32f4xx_hal.h" | |
34 #include "scheduler.h" | |
35 | |
36 | |
643
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
37 /* Use This compile switch to select the new charger status control implementation */ |
644 | 38 #define ENABLE_CHARGER_STATUS_V2 |
643
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
39 |
38 | 40 #define CHARGE_IN_PIN GPIO_PIN_2 |
41 #define CHARGE_IN_GPIO_PORT GPIOC | |
42 #define CHARGE_IN_GPIO_ENABLE() __GPIOC_CLK_ENABLE() | |
43 | |
44 #define CHARGE_OUT_PIN GPIO_PIN_1 | |
45 #define CHARGE_OUT_GPIO_PORT GPIOC | |
46 #define CHARGE_OUT_GPIO_ENABLE() __GPIOC_CLK_ENABLE() | |
47 | |
643
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
48 #define CHARGER_DEBOUNCE_SECONDS (120u) /* 120 seconds used to avoid problems with charger interrupts / disconnections */ |
38 | 49 |
50 uint8_t battery_i_charge_status = 0; | |
643
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
51 uint16_t battery_charger_counter = 0; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
52 |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
53 #ifdef ENABLE_CHARGER_STATUS_V2 |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
54 typedef enum |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
55 { |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
56 Charger_NotConnected = 0, /* This is identified reading CHARGE_IN_PIN == HIGH */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
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) */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
58 Charger_Active, /* Charging identified by CHARGE_IN_PIN == LOW for a certain time */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
59 Charger_Finished, |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
60 Charger_LostConnection /* Intermediate state to debounce disconnecting events (including charging error state like over temperature) */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
61 } chargerState_t; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
62 |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
63 static chargerState_t batteryChargerState = Charger_NotConnected; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
64 #endif |
38 | 65 |
66 /* can be 0, 1 or 255 | |
67 * 0 is disconnected | |
68 * 1 is charging | |
69 * 255 is full | |
70 */ | |
71 uint8_t get_charge_status(void) | |
72 { | |
73 return battery_i_charge_status; | |
74 } | |
75 | |
76 void init_battery_charger_status(void) | |
77 { | |
78 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
79 return; | |
80 #endif | |
81 | |
82 CHARGE_IN_GPIO_ENABLE(); | |
83 CHARGE_OUT_GPIO_ENABLE(); | |
84 | |
85 ReInit_battery_charger_status_pins(); | |
86 } | |
87 | |
88 void ReInit_battery_charger_status_pins(void) | |
89 { | |
90 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
91 return; | |
92 #endif | |
93 | |
94 GPIO_InitTypeDef GPIO_InitStructure; | |
95 | |
96 GPIO_InitStructure.Pin = CHARGE_IN_PIN; | |
97 GPIO_InitStructure.Mode = GPIO_MODE_INPUT; | |
98 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
99 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
100 HAL_GPIO_Init(CHARGE_IN_GPIO_PORT, &GPIO_InitStructure); | |
101 | |
102 GPIO_InitStructure.Pin = CHARGE_OUT_PIN; | |
103 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; | |
104 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
105 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
106 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); | |
107 } | |
108 | |
109 | |
110 void DeInit_battery_charger_status_pins(void) | |
111 { | |
112 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
113 return; | |
114 #endif | |
115 GPIO_InitTypeDef GPIO_InitStructure; | |
116 | |
117 | |
118 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; | |
119 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
120 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
121 | |
122 GPIO_InitStructure.Pin = CHARGE_IN_PIN; | |
123 HAL_GPIO_Init(CHARGE_IN_GPIO_PORT, &GPIO_InitStructure); | |
124 | |
125 GPIO_InitStructure.Pin = CHARGE_OUT_PIN; | |
126 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); | |
127 } | |
128 | |
129 /* static counter is used to avoid multiple counts of charge startings | |
130 and after that it is used, starting at 127 to count for the charge full signal | |
131 | |
132 there a short disconnections with the QI charger | |
133 therefore the battery_charger_counter has a countdown instead of = 0. | |
134 | |
135 battery_gas_gauge_set_charge_full and scheduleUpdateDeviceDataChargerFull are | |
136 set after disconnection as the charging process continues as long as not disconnected | |
137 to prevent the short disconnections the battery_charger_counter is used too including | |
138 upcounting again while battery_i_charge_status == 255 and the connection is established | |
139 | |
140 */ | |
141 | |
643
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
142 void battery_charger_get_status_and_contral_battery_gas_gauge(uint8_t cycleTimeBase) |
38 | 143 { |
643
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
144 #ifdef ENABLE_CHARGER_STATUS_V2 |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
145 static uint8_t notifyChargeComplete = 0; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
146 #endif |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
147 |
38 | 148 #ifdef OSTC_ON_DISCOVERY_HARDWARE |
149 return; | |
150 #endif | |
151 | |
643
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
152 #ifdef ENABLE_CHARGER_STATUS_V2 |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
153 /* on disconnection or while disconnected */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
154 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN)) |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
155 { |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
156 switch(batteryChargerState) |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
157 { |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
158 case Charger_Active: global.dataSendToMaster.chargeStatus = CHARGER_lostConnection; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
159 global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
160 batteryChargerState = Charger_LostConnection; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
161 battery_charger_counter = CHARGER_DEBOUNCE_SECONDS; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
162 |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
163 if(get_voltage() >= 4.1f) /* the charger stops charging when charge current is 1/10. */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
164 { /* Basically it is OK to rate a charging as complete if a defined voltage is reached */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
165 batteryChargerState = Charger_Finished; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
166 global.dataSendToMaster.chargeStatus = CHARGER_complete; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
167 global.deviceDataSendToMaster.chargeStatus = CHARGER_complete; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
168 battery_charger_counter = 30; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
169 notifyChargeComplete = 1; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
170 } |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
171 break; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
172 case Charger_WarmUp: |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
173 case Charger_Finished: |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
174 case Charger_LostConnection: if(battery_charger_counter >= cycleTimeBase) |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
175 { |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
176 battery_charger_counter -= cycleTimeBase; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
177 global.dataSendToMaster.chargeStatus = CHARGER_lostConnection; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
178 global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
179 batteryChargerState = Charger_LostConnection; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
180 } |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
181 else |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
182 { |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
183 battery_charger_counter = 0; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
184 global.dataSendToMaster.chargeStatus = CHARGER_off; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
185 global.deviceDataSendToMaster.chargeStatus = CHARGER_off; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
186 |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
187 if(notifyChargeComplete) |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
188 { |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
189 battery_gas_gauge_set_charge_full(); |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
190 scheduleUpdateDeviceDataChargerFull(); |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
191 notifyChargeComplete = 0; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
192 } |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
193 batteryChargerState = Charger_NotConnected; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
194 } |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
195 break; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
196 default: break; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
197 } |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
198 } |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
199 else |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
200 { |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
201 /* connected */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
202 /* wait for disconnection to write and reset */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
203 switch(batteryChargerState) |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
204 { |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
205 case Charger_NotConnected: battery_i_charge_status = 1; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
206 battery_charger_counter = 0; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
207 batteryChargerState = Charger_WarmUp; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
208 break; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
209 case Charger_LostConnection: batteryChargerState = Charger_Active; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
210 break; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
211 case Charger_WarmUp: battery_charger_counter += cycleTimeBase; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
212 if(battery_charger_counter >= CHARGER_DEBOUNCE_SECONDS ) |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
213 { |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
214 battery_i_charge_status = 2; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
215 scheduleUpdateDeviceDataChargerCharging(); |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
216 batteryChargerState = Charger_Active; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
217 } |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
218 /* no break */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
219 case Charger_Finished: |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
220 case Charger_Active: global.dataSendToMaster.chargeStatus = CHARGER_running; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
221 global.deviceDataSendToMaster.chargeStatus = CHARGER_running; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
222 |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
223 /* drive the output pin high to determine the state of the charger */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
224 GPIO_InitTypeDef GPIO_InitStructure; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
225 GPIO_InitStructure.Pin = CHARGE_OUT_PIN; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
226 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
227 GPIO_InitStructure.Pull = GPIO_NOPULL; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
228 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
229 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
230 HAL_GPIO_WritePin(CHARGE_OUT_GPIO_PORT, CHARGE_OUT_PIN,GPIO_PIN_SET); |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
231 HAL_Delay(1); |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
232 |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
233 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN)) /* high => charger stopped charging */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
234 { |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
235 batteryChargerState = Charger_Finished; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
236 global.dataSendToMaster.chargeStatus = CHARGER_complete; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
237 global.deviceDataSendToMaster.chargeStatus = CHARGER_complete; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
238 battery_charger_counter = 30; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
239 notifyChargeComplete = 1; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
240 } |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
241 else |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
242 { |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
243 if(batteryChargerState == Charger_Finished) /* voltage dropped below the hysteresis again => charging restarted */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
244 { |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
245 batteryChargerState = Charger_Active; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
246 notifyChargeComplete = 0; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
247 } |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
248 } |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
249 |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
250 /* restore high impedance to be able to detect disconnection */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
251 GPIO_InitStructure.Pin = CHARGE_OUT_PIN; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
252 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
253 GPIO_InitStructure.Pull = GPIO_NOPULL; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
254 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
255 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
256 break; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
257 |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
258 default: /* wait for disconnection */ |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
259 break; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
260 } |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
261 } |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
262 #else |
38 | 263 /* on disconnection or while disconnected */ |
264 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN)) | |
265 { | |
266 if(battery_charger_counter) | |
267 { | |
268 battery_charger_counter--; | |
269 global.dataSendToMaster.chargeStatus = CHARGER_lostConnection; | |
270 global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection; | |
271 } | |
272 /* max count down to 127+5 or 127+20 */ | |
273 if((battery_i_charge_status == 255) && battery_charger_counter < 127) | |
274 { | |
275 // battery_gas_gauge_set_charge_full(); | |
276 // scheduleUpdateDeviceDataChargerFull(); | |
277 battery_charger_counter = 0; | |
278 } | |
279 | |
280 if(battery_charger_counter == 0) | |
281 { | |
282 battery_i_charge_status = 0; | |
283 global.dataSendToMaster.chargeStatus = CHARGER_off; | |
284 global.deviceDataSendToMaster.chargeStatus = CHARGER_off; | |
285 | |
286 } | |
287 return; | |
288 } | |
289 | |
290 /* connected */ | |
291 | |
292 /* wait for disconnection to write and reset */ | |
293 if(battery_i_charge_status == 255) | |
294 { | |
295 global.dataSendToMaster.chargeStatus = CHARGER_complete; | |
296 global.deviceDataSendToMaster.chargeStatus = CHARGER_complete; | |
297 | |
643
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
298 if(((cycleTimeBase > 1) && (battery_charger_counter < 127+5)) || (battery_charger_counter < 127+20)) |
38 | 299 battery_charger_counter++; |
300 return; | |
301 } | |
302 | |
303 if(battery_charger_counter == 0) | |
304 battery_i_charge_status = 1; | |
305 | |
306 /* charger is connected and didn't signal full yet */ | |
307 global.dataSendToMaster.chargeStatus = CHARGER_running; | |
308 global.deviceDataSendToMaster.chargeStatus = CHARGER_running; | |
309 | |
310 GPIO_InitTypeDef GPIO_InitStructure; | |
643
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
311 GPIO_InitStructure.Pin = CHARGE_OUT_PIN; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
312 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
313 GPIO_InitStructure.Pull = GPIO_NOPULL; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
314 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; |
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
315 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); |
38 | 316 HAL_GPIO_WritePin(CHARGE_OUT_GPIO_PORT, CHARGE_OUT_PIN,GPIO_PIN_SET); |
317 HAL_Delay(1); | |
318 | |
319 | |
320 if(battery_charger_counter < 120) | |
321 { | |
643
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
322 if(cycleTimeBase == 1) |
38 | 323 battery_charger_counter++; |
324 else | |
325 { | |
326 battery_charger_counter += 30; | |
327 if(battery_charger_counter >= 127) | |
328 battery_charger_counter = 126; | |
329 } | |
330 } | |
331 else | |
332 if(battery_charger_counter < 127) | |
333 { | |
334 battery_charger_counter = 127; | |
335 if(battery_i_charge_status < 2) | |
336 { | |
337 battery_i_charge_status = 2; | |
338 scheduleUpdateDeviceDataChargerCharging(); | |
339 } | |
340 } | |
341 | |
342 if(battery_charger_counter >= 127) | |
343 { | |
344 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN) || (get_voltage() >= 4.1f)) | |
345 { | |
346 battery_charger_counter++; | |
643
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
347 if(((cycleTimeBase > 1) && (battery_charger_counter > 127+5)) || (battery_charger_counter > 127+20)) |
38 | 348 { |
349 battery_charger_counter = 127; | |
350 if(get_voltage() >= 4.1f) | |
351 { | |
352 battery_i_charge_status = 255; | |
353 battery_gas_gauge_set_charge_full(); | |
354 scheduleUpdateDeviceDataChargerFull(); | |
355 } | |
356 } | |
357 } | |
358 else | |
359 battery_charger_counter = 127; | |
360 } | |
361 | |
362 GPIO_InitStructure.Pin = CHARGE_OUT_PIN; | |
363 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG; | |
364 GPIO_InitStructure.Pull = GPIO_NOPULL; | |
365 GPIO_InitStructure.Speed = GPIO_SPEED_LOW; | |
366 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure); | |
643
5149cd644fbc
Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents:
38
diff
changeset
|
367 #endif |
38 | 368 } |
369 | |
370 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ |