Mercurial > public > ostc4
annotate Small_CPU/Src/batteryGasGauge.c @ 480:3fe9cc747c5c Improve_Button_Sleep
Code cleanup pressure plausibility check:
The function was deactivated (always returning true) some time ago => removed useless code
author | ideenmodellierer |
---|---|
date | Wed, 13 May 2020 21:56:35 +0200 |
parents | aa286a4926c2 |
children | 1b995079c045 |
rev | line source |
---|---|
38 | 1 /** |
2 ****************************************************************************** | |
3 * @file batteryGasGauge.c | |
4 * @author heinrichs weikamp gmbh | |
5 * @version V0.0.1 | |
6 * @date 09-Dec-2014 | |
7 * @brief LTC2942 Battery Gas Gauge | |
8 * | |
9 @verbatim | |
10 ============================================================================== | |
11 ##### stm32f4xx_hal_i2c.c modification ##### | |
12 ============================================================================== | |
13 The LTC2942 requires an repeated start condition without stop condition | |
14 for data reception. | |
15 | |
16 @endverbatim | |
17 ****************************************************************************** | |
18 * @attention | |
19 * | |
20 * <h2><center>© COPYRIGHT(c) 2014 heinrichs weikamp</center></h2> | |
21 * | |
22 ****************************************************************************** | |
23 */ | |
24 /* Includes ------------------------------------------------------------------*/ | |
330 | 25 #include <string.h> /* memset */ |
38 | 26 #include "batteryGasGauge.h" |
27 #include "baseCPU2.h" | |
28 #include "stm32f4xx_hal.h" | |
29 #include "i2c.h" | |
30 | |
408
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
31 static float battery_f_voltage = 6.0; /* max assumed voltage */ |
228
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
32 static float battery_f_charge_percent = 0; |
38 | 33 |
34 #define BGG_BATTERY_OFFSET (26123) //; 65536-(3,35Ah/0,085mAh) | |
228
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
35 #define BGG_BATTERY_DIVIDER (394) //; 3,35Ah/0,085mAh/100 [%] |
38 | 36 |
37 float get_voltage(void) | |
38 { | |
39 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
40 return 3.0f; | |
41 #endif | |
42 | |
43 return battery_f_voltage; | |
44 } | |
45 | |
46 | |
47 float get_charge(void) | |
48 { | |
49 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
50 return 100.0f; | |
51 #endif | |
52 | |
53 return battery_f_charge_percent; | |
54 } | |
55 | |
56 | |
57 void init_battery_gas_gauge(void) | |
58 { | |
59 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
60 return; | |
61 #endif | |
62 | |
63 uint8_t buffer[2]; | |
64 buffer[0] = 0x01; | |
228
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
65 |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
66 // F8 = 11111000: |
433 | 67 // ADC auto mode (11) |
228
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
68 // Prescale M = 128 (111) |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
69 // AL/CC pin disable (0) |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
70 // Shutdown (0) |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
71 buffer[1] = 0xF8; |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
72 I2C_Master_Transmit(DEVICE_BATTERYGAUGE, buffer, 2); |
38 | 73 } |
74 | |
330 | 75 uint8_t battery_gas_gauge_CheckConfigOK(void) |
76 { | |
77 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
78 return; | |
79 #endif | |
80 | |
81 uint8_t retval = 0; | |
82 uint8_t bufferReceive[10]; | |
83 | |
84 memset(bufferReceive,0,sizeof(bufferReceive)); | |
85 | |
86 I2C_Master_Receive(DEVICE_BATTERYGAUGE, bufferReceive, 10); | |
87 if(bufferReceive[1] == 0xf8) | |
88 { | |
89 retval = 1; | |
90 } | |
91 return retval; | |
92 } | |
93 | |
242
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
94 static void disable_adc(void) |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
95 { |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
96 uint8_t buffer[2]; |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
97 buffer[0] = 0x01; |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
98 |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
99 // according to the datasheet of the LTC2942, the adc shall |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
100 // be disabled when writing to the gauge registers |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
101 |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
102 // 0xF9 = 11111001: |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
103 // see init_battery_gas_gauge() |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
104 // Shutdown (1) |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
105 buffer[1] = 0xF9; |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
106 I2C_Master_Transmit(DEVICE_BATTERYGAUGE, buffer, 2); |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
107 } |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
108 |
38 | 109 |
110 void battery_gas_gauge_get_data(void) | |
111 { | |
112 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
113 return; | |
114 #endif | |
115 | |
116 float battery_f_voltage_local; | |
117 float battery_f_charge_percent_local; | |
118 | |
119 uint8_t bufferReceive[10]; | |
408
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
120 |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
121 if(I2C_Master_Receive(DEVICE_BATTERYGAUGE, bufferReceive, 10) == HAL_OK) |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
122 { |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
123 battery_f_voltage_local = (float)(bufferReceive[8] * 256); |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
124 battery_f_voltage_local += (float)(bufferReceive[9]); |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
125 battery_f_voltage_local *= (float)6 / (float)0xFFFF; |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
126 |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
127 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
128 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256); |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
129 battery_f_charge_percent_local += (float)(bufferReceive[3]); |
433 | 130 battery_f_charge_percent_local -= BGG_BATTERY_OFFSET; /* Because of the prescalar 128 the counter assumes a max value of 5570mAh => normalize to 3350mAh*/ |
131 battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER; /* transform to percentage */ | |
38 | 132 |
408
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
133 if(battery_f_charge_percent_local < 0) |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
134 battery_f_charge_percent_local = 0; |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
135 |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
136 battery_f_voltage = battery_f_voltage_local; |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
137 battery_f_charge_percent = battery_f_charge_percent_local; |
2fc08a0d1ec3
Bugfix invalid voltage / temperatur after coldstart:
ideenmodellierer
parents:
330
diff
changeset
|
138 } |
38 | 139 } |
140 | |
141 | |
142 void battery_gas_gauge_set_charge_full(void) | |
143 { | |
242
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
144 disable_adc(); |
38 | 145 #ifdef OSTC_ON_DISCOVERY_HARDWARE |
146 return; | |
147 #endif | |
148 | |
149 uint8_t bufferSend[3]; | |
150 bufferSend[0] = 0x02; | |
151 bufferSend[1] = 0xFF; | |
152 bufferSend[2] = 0xFF; | |
153 I2C_Master_Transmit( DEVICE_BATTERYGAUGE, bufferSend, 3); | |
242
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
154 init_battery_gas_gauge(); |
38 | 155 } |
156 | |
157 | |
158 void battery_gas_gauge_set(float percentage) | |
159 { | |
242
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
160 |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
161 disable_adc(); |
38 | 162 #ifdef OSTC_ON_DISCOVERY_HARDWARE |
163 return; | |
164 #endif | |
165 | |
166 uint16_t mAhSend; | |
167 | |
168 if(percentage >= 100) | |
169 mAhSend = 0xFFFF; | |
228
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
170 else { |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
171 mAhSend = (percentage * BGG_BATTERY_DIVIDER) + BGG_BATTERY_OFFSET; |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
172 } |
38 | 173 |
174 uint8_t bufferSend[3]; | |
175 bufferSend[0] = 0x02; | |
176 bufferSend[1] = (uint8_t)(mAhSend / 256); | |
177 bufferSend[2] = (uint8_t)(mAhSend & 0xFF); | |
178 I2C_Master_Transmit( DEVICE_BATTERYGAUGE, bufferSend, 3); | |
242
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
179 init_battery_gas_gauge(); |
38 | 180 } |
181 | |
182 | |
183 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ |