annotate Small_CPU/Src/batteryCharger.c @ 949:c6b858f2e025 Evo_2_23 tip

GNSS UTC in Date-Time menu: The UTC time provided by the gnss module is not shown in the date-time settings menu. If a valid time signal is available then the OSTC RTC may be set to it by selecting the UTC menu entry. The time zone has to be selected manually. This is possible using a separate edit line.
author Ideenmodellierer
date Sun, 22 Dec 2024 21:19:21 +0100
parents d32901746950
children
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
644
ebe3fc302ab8 Compile with ENABLE_CHARGER_STATUS_V2
heinrichs weikamp
parents: 643
diff changeset
20 CHG F4 7 O Open-drain output � active when BAT is enabled. Float if not used.
38
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
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
37 #define CHARGER_DEBOUNCE_SECONDS (6u) /* 6 seconds used to avoid problems with charger interrupts / disconnections */
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38
668
079bb5b22c06 Rework charge cycle:
Ideenmodellierer
parents: 662
diff changeset
39 static uint16_t battery_charger_counter = 0;
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
40 static chargerState_t batteryChargerState = Charger_NotConnected;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
42 void set_charge_state(uint8_t newState)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
43 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
44 if(newState < Charger_END)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
45 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
46 batteryChargerState = newState;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
47 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
48 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
49
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
50 uint8_t get_charge_state(void)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
51 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
52 return batteryChargerState;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
53 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
54
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 void init_battery_charger_status(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 CHARGE_IN_GPIO_ENABLE();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 CHARGE_OUT_GPIO_ENABLE();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 ReInit_battery_charger_status_pins();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67 void ReInit_battery_charger_status_pins(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73 GPIO_InitTypeDef GPIO_InitStructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75 GPIO_InitStructure.Pin = CHARGE_IN_PIN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 GPIO_InitStructure.Mode = GPIO_MODE_INPUT;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 GPIO_InitStructure.Pull = GPIO_NOPULL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79 HAL_GPIO_Init(CHARGE_IN_GPIO_PORT, &GPIO_InitStructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 GPIO_InitStructure.Pull = GPIO_NOPULL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 void DeInit_battery_charger_status_pins(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 GPIO_InitTypeDef GPIO_InitStructure;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 GPIO_InitStructure.Pull = GPIO_NOPULL;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 GPIO_InitStructure.Pin = CHARGE_IN_PIN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 HAL_GPIO_Init(CHARGE_IN_GPIO_PORT, &GPIO_InitStructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
108 void battery_charger_get_status_and_contral_battery_gas_gauge(uint8_t cycleTimeBase)
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 {
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
110 static uint8_t notifyChargeComplete = 0;
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
111
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
116 if(batteryChargerState == Charger_ColdStart) /* wait for the first valid voltage meassurement */
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
117 {
859
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
118 if(global.lifeData.battery_voltage != BATTERY_DEFAULT_VOLTAGE) /* wait for first valid voltage value */
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
119 {
859
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
120 if((global.lifeData.battery_voltage < BATTERY_CHARGER_CONNECTED_VOLTAGE)
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
121 && (global.lifeData.battery_voltage > BATTERY_ENDOF_CHARGE_VOLTAGE)) /* Voltage close to full state => maybe new battery inserted */
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
122 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
123 battery_gas_gauge_set_charge_full();
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
124 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
125 batteryChargerState = Charger_NotConnected;
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
126 }
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
127 }
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
128 else
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
129 { /* on disconnection or while disconnected */
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
130 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN))
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
131 {
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
132 switch(batteryChargerState)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
133 {
686
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
134 case Charger_WarmUp:
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
135 case Charger_Active: global.dataSendToMaster.chargeStatus = CHARGER_lostConnection;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
136 global.deviceDataSendToMaster.chargeStatus = CHARGER_lostConnection;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
137 batteryChargerState = Charger_LostConnection;
686
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
138 if(cycleTimeBase > CHARGER_DEBOUNCE_SECONDS) /* adapt connection lost detection to sleep mode */
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
139 {
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
140 battery_charger_counter = cycleTimeBase + 1;
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
141 }
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
142 else
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
143 {
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
144 battery_charger_counter = CHARGER_DEBOUNCE_SECONDS;
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
145 }
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
146 break;
859
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
147 case Charger_Finished: if((get_voltage() >= BATTERY_ENDOF_CHARGE_VOLTAGE) && (get_voltage() < BATTERY_CHARGER_CONNECTED_VOLTAGE)) /* stopping does not necessarily mean battery is full */
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
148 {
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
149 global.dataSendToMaster.chargeStatus = CHARGER_complete;
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
150 global.deviceDataSendToMaster.chargeStatus = CHARGER_complete;
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
151 notifyChargeComplete = 1;
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
152 }
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
153 battery_charger_counter = 10;
686
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
154 batteryChargerState = Charger_LostConnection;
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
155 /* no break */
668
079bb5b22c06 Rework charge cycle:
Ideenmodellierer
parents: 662
diff changeset
156 case Charger_LostConnection: /* the charger stops charging when charge current is 1/10 */
079bb5b22c06 Rework charge cycle:
Ideenmodellierer
parents: 662
diff changeset
157 /* Basically it is OK to rate a charging as complete if a defined voltage is reached */
686
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
158 if(((battery_gas_gauge_isChargeValueValid() == 0) || (global.lifeData.battery_charge < 90)) && (get_voltage() >= BATTERY_ENDOF_CHARGE_VOLTAGE) && (get_voltage() < BATTERY_CHARGER_CONNECTED_VOLTAGE))
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
159 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
160 notifyChargeComplete = 1;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
161 }
686
b1e24513b83e Bugfix Battery charge complete event:
Ideenmodellierer
parents: 668
diff changeset
162 if(battery_charger_counter >= cycleTimeBase)
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
163 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
164 battery_charger_counter -= cycleTimeBase;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
165 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
166 else
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
167 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
168 battery_charger_counter = 0;
859
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
169
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
170 global.dataSendToMaster.chargeStatus = CHARGER_off;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
171 global.deviceDataSendToMaster.chargeStatus = CHARGER_off;
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
172
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
173 if(notifyChargeComplete)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
174 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
175 battery_gas_gauge_set_charge_full();
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
176 scheduleUpdateDeviceDataChargerFull();
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
177 }
859
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
178 notifyChargeComplete = 0;
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
179 batteryChargerState = Charger_NotConnected;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
180 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
181 break;
859
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
182 default: batteryChargerState = Charger_NotConnected; /* unexpected state => reinitialize state machine */
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
183 break;
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
184 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
185 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
186 else
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
187 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
188 /* connected */
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
189 /* wait for disconnection to write and reset */
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
190 switch(batteryChargerState)
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
191 {
859
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
192 case Charger_NotConnected: battery_charger_counter = 0;
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
193 batteryChargerState = Charger_WarmUp;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
194 break;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
195 case Charger_LostConnection: batteryChargerState = Charger_Active;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
196 break;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
197 case Charger_WarmUp: battery_charger_counter += cycleTimeBase;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
198 if(battery_charger_counter >= CHARGER_DEBOUNCE_SECONDS )
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
199 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
200 scheduleUpdateDeviceDataChargerCharging();
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
201 batteryChargerState = Charger_Active;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
202 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
203 /* no break */
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
204 case Charger_Finished:
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
205 case Charger_Active: global.dataSendToMaster.chargeStatus = CHARGER_running;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
206 global.deviceDataSendToMaster.chargeStatus = CHARGER_running;
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
207
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
208 /* drive the output pin high to determine the state of the charger */
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
209 GPIO_InitTypeDef GPIO_InitStructure;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
210 GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
211 GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
212 GPIO_InitStructure.Pull = GPIO_NOPULL;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
213 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
214 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
215 HAL_GPIO_WritePin(CHARGE_OUT_GPIO_PORT, CHARGE_OUT_PIN,GPIO_PIN_SET);
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
216 HAL_Delay(1);
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
217
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
218 if(HAL_GPIO_ReadPin(CHARGE_IN_GPIO_PORT,CHARGE_IN_PIN)) /* high => charger stopped charging */
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
219 {
859
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
220 battery_charger_counter = 30;
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
221 batteryChargerState = Charger_Finished;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
222 }
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
223 else
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
224 {
668
079bb5b22c06 Rework charge cycle:
Ideenmodellierer
parents: 662
diff changeset
225 if(global.lifeData.battery_charge > 100.0) /* still charging but indicator is set to full => decrease to 99% to keep count increasing */
079bb5b22c06 Rework charge cycle:
Ideenmodellierer
parents: 662
diff changeset
226 {
079bb5b22c06 Rework charge cycle:
Ideenmodellierer
parents: 662
diff changeset
227 battery_gas_gauge_set(99.0);
079bb5b22c06 Rework charge cycle:
Ideenmodellierer
parents: 662
diff changeset
228 }
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
229 if(batteryChargerState == Charger_Finished) /* voltage dropped below the hysteresis again => charging restarted */
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
230 {
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
231 batteryChargerState = Charger_Active;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
232 notifyChargeComplete = 0;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
233 }
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
234 }
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
235
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
236 /* restore high impedance to be able to detect disconnection */
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
237 GPIO_InitStructure.Pin = CHARGE_OUT_PIN;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
238 GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
239 GPIO_InitStructure.Pull = GPIO_NOPULL;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
240 GPIO_InitStructure.Speed = GPIO_SPEED_LOW;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
241 HAL_GPIO_Init(CHARGE_OUT_GPIO_PORT, &GPIO_InitStructure);
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
242 HAL_Delay(1);
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
243 break;
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
244
859
d32901746950 Improvment battery charger visualization:
Ideenmodellierer
parents: 686
diff changeset
245 default: batteryChargerState = Charger_NotConnected; /* unexpected state => reinitialize state machine */
662
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
246 break;
1b995079c045 PSCR Mode
heinrichs weikamp
parents: 645
diff changeset
247 }
643
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
248 }
5149cd644fbc Reimplemented charger status generation (deactivated):
Ideenmodellierer
parents: 38
diff changeset
249 }
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
250 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
251
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
252 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/