Mercurial > public > ostc4
annotate Small_CPU/Src/batteryGasGauge.c @ 238:a9d798e8c11f div-fixes-5
cleanup, bugfix: do not mask I2C_SystemStatus with local variable
This commit is partly cleanup, and partly possible bugfix. Masking
the global I2C_SystemStatus with a local variable is (very) bad practice,
but more importantly, dangerous, as other code uses this I2C_SystemStatus
to base decisions on. So, this is definitely non-trivial, as it can
possibly change the flow of control. This said, its tested and seems to
have no negative effects (but also no positive, as I sort of hoped for),
so that is why I mark it cleanup as well. Constructs like this shall
be heavily documented in the code, when there is a reason to do things
like this.
Further, remove a 2nd rather useless construct. There is no reason
to & 0x03 the output of I2C_SystemStatus. This is the only location
in the entire code base where this is done, so, its not only useless
but also inconsistent and confusing the true intentions here.
Finally, littered to code with todo's that I will take care of in
next commits.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author | Jan Mulder <jlmulder@xs4all.nl> |
---|---|
date | Mon, 08 Apr 2019 10:16:17 +0200 |
parents | f9ba924d188e |
children | b23de15e2861 |
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 ------------------------------------------------------------------*/ | |
25 #include "batteryGasGauge.h" | |
26 #include "baseCPU2.h" | |
27 #include "stm32f4xx_hal.h" | |
28 #include "i2c.h" | |
29 | |
228
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
30 static float battery_f_voltage = 0; |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
31 static float battery_f_charge_percent = 0; |
38 | 32 |
33 #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
|
34 #define BGG_BATTERY_DIVIDER (394) //; 3,35Ah/0,085mAh/100 [%] |
38 | 35 |
36 float get_voltage(void) | |
37 { | |
38 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
39 return 3.0f; | |
40 #endif | |
41 | |
42 return battery_f_voltage; | |
43 } | |
44 | |
45 | |
46 float get_charge(void) | |
47 { | |
48 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
49 return 100.0f; | |
50 #endif | |
51 | |
52 return battery_f_charge_percent; | |
53 } | |
54 | |
55 | |
56 void init_battery_gas_gauge(void) | |
57 { | |
58 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
59 return; | |
60 #endif | |
61 | |
62 uint8_t buffer[2]; | |
63 buffer[0] = 0x01; | |
228
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
64 |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
65 // F8 = 11111000: |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
66 // Vbat 3.0V (11) |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
67 // Prescale M = 128 (111) |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
68 // AL/CC pin disable (0) |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
69 // Shutdown (0) |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
70 buffer[1] = 0xF8; |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
71 I2C_Master_Transmit(DEVICE_BATTERYGAUGE, buffer, 2); |
38 | 72 } |
73 | |
74 | |
75 void battery_gas_gauge_get_data(void) | |
76 { | |
77 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
78 return; | |
79 #endif | |
80 | |
81 float battery_f_voltage_local; | |
82 float battery_f_charge_percent_local; | |
83 | |
84 uint8_t bufferReceive[10]; | |
85 I2C_Master_Receive( DEVICE_BATTERYGAUGE, bufferReceive, 10); | |
86 | |
87 battery_f_voltage_local = (float)(bufferReceive[8] * 256); | |
88 battery_f_voltage_local += (float)(bufferReceive[9]); | |
89 battery_f_voltage_local *= (float)6 / (float)0xFFFF; | |
90 | |
91 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh | |
92 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256); | |
93 battery_f_charge_percent_local += (float)(bufferReceive[3]); | |
94 battery_f_charge_percent_local -= BGG_BATTERY_OFFSET; | |
228
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
95 battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER; |
38 | 96 |
97 if(battery_f_charge_percent_local < 0) | |
98 battery_f_charge_percent_local = 0; | |
99 | |
100 battery_f_voltage = battery_f_voltage_local; | |
101 battery_f_charge_percent = battery_f_charge_percent_local; | |
102 } | |
103 | |
104 | |
105 void battery_gas_gauge_set_charge_full(void) | |
106 { | |
107 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
108 return; | |
109 #endif | |
110 | |
111 uint8_t bufferSend[3]; | |
112 bufferSend[0] = 0x02; | |
113 bufferSend[1] = 0xFF; | |
114 bufferSend[2] = 0xFF; | |
115 I2C_Master_Transmit( DEVICE_BATTERYGAUGE, bufferSend, 3); | |
116 } | |
117 | |
118 | |
119 void battery_gas_gauge_set(float percentage) | |
120 { | |
121 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
122 return; | |
123 #endif | |
124 | |
125 uint16_t mAhSend; | |
126 | |
127 if(percentage >= 100) | |
128 mAhSend = 0xFFFF; | |
228
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
129 else { |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
130 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
|
131 } |
38 | 132 |
133 uint8_t bufferSend[3]; | |
134 bufferSend[0] = 0x02; | |
135 bufferSend[1] = (uint8_t)(mAhSend / 256); | |
136 bufferSend[2] = (uint8_t)(mAhSend & 0xFF); | |
137 I2C_Master_Transmit( DEVICE_BATTERYGAUGE, bufferSend, 3); | |
138 } | |
139 | |
140 | |
141 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ |