comparison Small_CPU/Src/batteryCharger.c @ 38:5f11787b4f42

include in ostc4 repository
author heinrichsweikamp
date Sat, 28 Apr 2018 11:52:34 +0200
parents
children 5149cd644fbc
comparison
equal deleted inserted replaced
37:ccc45c0e1ea2 38:5f11787b4f42
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
20 CHG F4 7 O Open-drain output – active when BAT is enabled. Float if not used.
21
22 @endverbatim
23 ******************************************************************************
24 * @attention
25 *
26 * <h2><center>&copy; 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
37 #define CHARGE_IN_PIN GPIO_PIN_2
38 #define CHARGE_IN_GPIO_PORT GPIOC
39 #define CHARGE_IN_GPIO_ENABLE() __GPIOC_CLK_ENABLE()
40
41 #define CHARGE_OUT_PIN GPIO_PIN_1
42 #define CHARGE_OUT_GPIO_PORT GPIOC
43 #define CHARGE_OUT_GPIO_ENABLE() __GPIOC_CLK_ENABLE()
44
45
46 uint8_t battery_i_charge_status = 0;
47 uint8_t battery_charger_counter = 0;
48
49 /* can be 0, 1 or 255
50 * 0 is disconnected
51 * 1 is charging
52 * 255 is full
53 */
54 uint8_t get_charge_status(void)
55 {
56 return battery_i_charge_status;
57 }
58
59 void init_battery_charger_status(void)
60 {
61 #ifdef OSTC_ON_DISCOVERY_HARDWARE
62 return;
63 #endif
64
65 CHARGE_IN_GPIO_ENABLE();
66 CHARGE_OUT_GPIO_ENABLE();
67
68 ReInit_battery_charger_status_pins();
69 }
70
71 void ReInit_battery_charger_status_pins(void)
72 {
73 #ifdef OSTC_ON_DISCOVERY_HARDWARE
74 return;
75 #endif
76
77 GPIO_InitTypeDef GPIO_InitStructure;
78
79 GPIO_InitStructure.Pin = CHARGE_IN_PIN;
80 GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
81 GPIO_InitStructure.Pull = GPIO_NOPULL;
82 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
83 HAL_GPIO_Init(CHARGE_IN_GPIO_PORT, &GPIO_InitStructure);
84
85 GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
86 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
87 GPIO_InitStructure.Pull = GPIO_NOPULL;
88 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
89 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
90 }
91
92
93 void DeInit_battery_charger_status_pins(void)
94 {
95 #ifdef OSTC_ON_DISCOVERY_HARDWARE
96 return;
97 #endif
98 GPIO_InitTypeDef GPIO_InitStructure;
99
100
101 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
102 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
103 GPIO_InitStructure.Pull = GPIO_NOPULL;
104
105 GPIO_InitStructure.Pin = CHARGE_IN_PIN;
106 HAL_GPIO_Init(CHARGE_IN_GPIO_PORT, &GPIO_InitStructure);
107
108 GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
109 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
110 }
111
112 /* static counter is used to avoid multiple counts of charge startings
113 and after that it is used, starting at 127 to count for the charge full signal
114
115 there a short disconnections with the QI charger
116 therefore the battery_charger_counter has a countdown instead of = 0.
117
118 battery_gas_gauge_set_charge_full and scheduleUpdateDeviceDataChargerFull are
119 set after disconnection as the charging process continues as long as not disconnected
120 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
122
123 */
124
125 void battery_charger_get_status_and_contral_battery_gas_gauge(uint8_t inSleepModeLessCounts)
126 {
127 #ifdef OSTC_ON_DISCOVERY_HARDWARE
128 return;
129 #endif
130
131 /* on disconnection or while disconnected */
132 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN))
133 {
134 if(battery_charger_counter)
135 {
136 battery_charger_counter--;
137 global.dataSendToMaster.chargeStatus = CHARGER_lostConnection;
138 global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection;
139 }
140 /* max count down to 127+5 or 127+20 */
141 if((battery_i_charge_status == 255) && battery_charger_counter < 127)
142 {
143 // battery_gas_gauge_set_charge_full();
144 // scheduleUpdateDeviceDataChargerFull();
145 battery_charger_counter = 0;
146 }
147
148 if(battery_charger_counter == 0)
149 {
150 battery_i_charge_status = 0;
151 global.dataSendToMaster.chargeStatus = CHARGER_off;
152 global.deviceDataSendToMaster.chargeStatus = CHARGER_off;
153
154 }
155 return;
156 }
157
158 /* connected */
159
160 /* wait for disconnection to write and reset */
161 if(battery_i_charge_status == 255)
162 {
163 global.dataSendToMaster.chargeStatus = CHARGER_complete;
164 global.deviceDataSendToMaster.chargeStatus = CHARGER_complete;
165
166 if((inSleepModeLessCounts && (battery_charger_counter < 127+5)) || (battery_charger_counter < 127+20))
167 battery_charger_counter++;
168 return;
169 }
170
171 if(battery_charger_counter == 0)
172 battery_i_charge_status = 1;
173
174 /* charger is connected and didn't signal full yet */
175 global.dataSendToMaster.chargeStatus = CHARGER_running;
176 global.deviceDataSendToMaster.chargeStatus = CHARGER_running;
177
178 GPIO_InitTypeDef GPIO_InitStructure;
179 GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
180 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
181 GPIO_InitStructure.Pull = GPIO_NOPULL;
182 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
183 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
184 HAL_GPIO_WritePin(CHARGE_OUT_GPIO_PORT, CHARGE_OUT_PIN,GPIO_PIN_SET);
185 HAL_Delay(1);
186
187
188 if(battery_charger_counter < 120)
189 {
190 if(!inSleepModeLessCounts)
191 battery_charger_counter++;
192 else
193 {
194 battery_charger_counter += 30;
195 if(battery_charger_counter >= 127)
196 battery_charger_counter = 126;
197 }
198 }
199 else
200 if(battery_charger_counter < 127)
201 {
202 battery_charger_counter = 127;
203 if(battery_i_charge_status < 2)
204 {
205 battery_i_charge_status = 2;
206 scheduleUpdateDeviceDataChargerCharging();
207 }
208 }
209
210 if(battery_charger_counter >= 127)
211 {
212 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN) || (get_voltage() >= 4.1f))
213 {
214 battery_charger_counter++;
215 if((inSleepModeLessCounts && (battery_charger_counter > 127+5)) || (battery_charger_counter > 127+20))
216 {
217 battery_charger_counter = 127;
218 if(get_voltage() >= 4.1f)
219 {
220 battery_i_charge_status = 255;
221 battery_gas_gauge_set_charge_full();
222 scheduleUpdateDeviceDataChargerFull();
223 }
224 }
225 }
226 else
227 battery_charger_counter = 127;
228 }
229
230 GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
231 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
232 GPIO_InitStructure.Pull = GPIO_NOPULL;
233 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
234 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
235 }
236
237 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/