annotate Small_CPU/Src/batteryCharger.c @ 89:ff7775cc34c4 kittz

temp! full cyclic SPI
author Dmitry Romanov <kitt@bk.ru>
date Fri, 23 Nov 2018 16:52:21 +0300
parents 5f11787b4f42
children 5149cd644fbc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1 /**
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3 * @file batteryCharger.c
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
4 * @author heinrichs weikamp gmbh
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
5 * @date 09-Dec-2014
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
6 * @version V0.0.1
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
7 * @since 09-Dec-2014
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
8 * @brief LTC4054 Battery Charger
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
9 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
10 @verbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
11 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
12 ##### How to use #####
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
13 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
14
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
15 The bq5105x provides one status output, CHG. This output is an open-drain NMOS device that is rated to 20 V.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
16 The open-drain FET connected to the CHG pin will be turned on whenever the output (BAT) of the charger is
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
17 enabled. As a note, the output of the charger supply will not be enabled if the VRECT-REG does not converge to the
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
18 no-load target voltage.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
19
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
20 CHG F4 7 O Open-drain output – active when BAT is enabled. Float if not used.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
21
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
22 @endverbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
23 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
24 * @attention
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
25 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26 * <h2><center>&copy; COPYRIGHT(c) 2015 heinrichs weikamp</center></h2>
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
30 /* Includes ------------------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31 #include "batteryCharger.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32 #include "batteryGasGauge.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33 #include "stm32f4xx_hal.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34 #include "scheduler.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 #define CHARGE_IN_PIN GPIO_PIN_2
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 #define CHARGE_IN_GPIO_PORT GPIOC
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39 #define CHARGE_IN_GPIO_ENABLE() __GPIOC_CLK_ENABLE()
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41 #define CHARGE_OUT_PIN GPIO_PIN_1
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 #define CHARGE_OUT_GPIO_PORT GPIOC
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 #define CHARGE_OUT_GPIO_ENABLE() __GPIOC_CLK_ENABLE()
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 uint8_t battery_i_charge_status = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 uint8_t battery_charger_counter = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 /* can be 0, 1 or 255
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 * 0 is disconnected
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51 * 1 is charging
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52 * 255 is full
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54 uint8_t get_charge_status(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 return battery_i_charge_status;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59 void init_battery_charger_status(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 CHARGE_IN_GPIO_ENABLE();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 CHARGE_OUT_GPIO_ENABLE();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 ReInit_battery_charger_status_pins();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 void ReInit_battery_charger_status_pins(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 GPIO_InitTypeDef GPIO_InitStructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79 GPIO_InitStructure.Pin = CHARGE_IN_PIN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 GPIO_InitStructure.Pull = GPIO_NOPULL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 HAL_GPIO_Init(CHARGE_IN_GPIO_PORT, &GPIO_InitStructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87 GPIO_InitStructure.Pull = GPIO_NOPULL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 void DeInit_battery_charger_status_pins(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 GPIO_InitTypeDef GPIO_InitStructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 GPIO_InitStructure.Pull = GPIO_NOPULL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 GPIO_InitStructure.Pin = CHARGE_IN_PIN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106 HAL_GPIO_Init(CHARGE_IN_GPIO_PORT, &GPIO_InitStructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 /* static counter is used to avoid multiple counts of charge startings
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 and after that it is used, starting at 127 to count for the charge full signal
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 there a short disconnections with the QI charger
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 therefore the battery_charger_counter has a countdown instead of = 0.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 battery_gas_gauge_set_charge_full and scheduleUpdateDeviceDataChargerFull are
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 set after disconnection as the charging process continues as long as not disconnected
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 to prevent the short disconnections the battery_charger_counter is used too including
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 upcounting again while battery_i_charge_status == 255 and the connection is established
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125 void battery_charger_get_status_and_contral_battery_gas_gauge(uint8_t inSleepModeLessCounts)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 /* on disconnection or while disconnected */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 if(battery_charger_counter)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
135 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136 battery_charger_counter--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
137 global.dataSendToMaster.chargeStatus = CHARGER_lostConnection;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
138 global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
139 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 /* max count down to 127+5 or 127+20 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 if((battery_i_charge_status == 255) && battery_charger_counter < 127)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
142 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
143 // battery_gas_gauge_set_charge_full();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144 // scheduleUpdateDeviceDataChargerFull();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145 battery_charger_counter = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
146 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
147
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
148 if(battery_charger_counter == 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
149 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 battery_i_charge_status = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151 global.dataSendToMaster.chargeStatus = CHARGER_off;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 global.deviceDataSendToMaster.chargeStatus = CHARGER_off;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
157
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158 /* connected */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
160 /* wait for disconnection to write and reset */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
161 if(battery_i_charge_status == 255)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
162 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
163 global.dataSendToMaster.chargeStatus = CHARGER_complete;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
164 global.deviceDataSendToMaster.chargeStatus = CHARGER_complete;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
165
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
166 if((inSleepModeLessCounts && (battery_charger_counter < 127+5)) || (battery_charger_counter < 127+20))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
167 battery_charger_counter++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
168 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
169 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
170
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
171 if(battery_charger_counter == 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
172 battery_i_charge_status = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
173
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
174 /* charger is connected and didn't signal full yet */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
175 global.dataSendToMaster.chargeStatus = CHARGER_running;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
176 global.deviceDataSendToMaster.chargeStatus = CHARGER_running;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
177
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
178 GPIO_InitTypeDef GPIO_InitStructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
179 GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
180 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
181 GPIO_InitStructure.Pull = GPIO_NOPULL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
182 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
183 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
184 HAL_GPIO_WritePin(CHARGE_OUT_GPIO_PORT, CHARGE_OUT_PIN,GPIO_PIN_SET);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
185 HAL_Delay(1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
186
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
187
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
188 if(battery_charger_counter < 120)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
189 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
190 if(!inSleepModeLessCounts)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
191 battery_charger_counter++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
192 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
193 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
194 battery_charger_counter += 30;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
195 if(battery_charger_counter >= 127)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
196 battery_charger_counter = 126;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
197 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
198 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
199 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
200 if(battery_charger_counter < 127)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
201 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
202 battery_charger_counter = 127;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
203 if(battery_i_charge_status < 2)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
204 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
205 battery_i_charge_status = 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
206 scheduleUpdateDeviceDataChargerCharging();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
207 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
208 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
209
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
210 if(battery_charger_counter >= 127)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
211 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
212 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN) || (get_voltage() >= 4.1f))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
213 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
214 battery_charger_counter++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
215 if((inSleepModeLessCounts && (battery_charger_counter > 127+5)) || (battery_charger_counter > 127+20))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
216 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
217 battery_charger_counter = 127;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
218 if(get_voltage() >= 4.1f)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
219 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
220 battery_i_charge_status = 255;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
221 battery_gas_gauge_set_charge_full();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
222 scheduleUpdateDeviceDataChargerFull();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
223 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
224 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
225 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
226 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
227 battery_charger_counter = 127;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
228 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
229
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
230 GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
231 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
232 GPIO_InitStructure.Pull = GPIO_NOPULL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
233 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
234 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
235 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
236
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
237 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/