Mercurial > public > ostc4
annotate Small_CPU/Src/batteryGasGauge.c @ 395:eb7696e0510f ImproveBluetooth
Added start of module configuration after eache modul power on
Added button evaluation to support request for bluetooth signal stragth
author | ideenmodellierer |
---|---|
date | Tue, 26 Nov 2019 21:17:57 +0100 |
parents | 2defc8cd93ce |
children | 2fc08a0d1ec3 |
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 | |
228
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
31 static float battery_f_voltage = 0; |
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: |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
67 // Vbat 3.0V (11) |
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]; | |
120 I2C_Master_Receive( DEVICE_BATTERYGAUGE, bufferReceive, 10); | |
121 | |
122 battery_f_voltage_local = (float)(bufferReceive[8] * 256); | |
123 battery_f_voltage_local += (float)(bufferReceive[9]); | |
124 battery_f_voltage_local *= (float)6 / (float)0xFFFF; | |
125 | |
126 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh | |
127 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256); | |
128 battery_f_charge_percent_local += (float)(bufferReceive[3]); | |
129 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
|
130 battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER; |
38 | 131 |
132 if(battery_f_charge_percent_local < 0) | |
133 battery_f_charge_percent_local = 0; | |
134 | |
135 battery_f_voltage = battery_f_voltage_local; | |
136 battery_f_charge_percent = battery_f_charge_percent_local; | |
137 } | |
138 | |
139 | |
140 void battery_gas_gauge_set_charge_full(void) | |
141 { | |
242
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
142 disable_adc(); |
38 | 143 #ifdef OSTC_ON_DISCOVERY_HARDWARE |
144 return; | |
145 #endif | |
146 | |
147 uint8_t bufferSend[3]; | |
148 bufferSend[0] = 0x02; | |
149 bufferSend[1] = 0xFF; | |
150 bufferSend[2] = 0xFF; | |
151 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
|
152 init_battery_gas_gauge(); |
38 | 153 } |
154 | |
155 | |
156 void battery_gas_gauge_set(float percentage) | |
157 { | |
242
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
158 |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
159 disable_adc(); |
38 | 160 #ifdef OSTC_ON_DISCOVERY_HARDWARE |
161 return; | |
162 #endif | |
163 | |
164 uint16_t mAhSend; | |
165 | |
166 if(percentage >= 100) | |
167 mAhSend = 0xFFFF; | |
228
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
168 else { |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
169 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
|
170 } |
38 | 171 |
172 uint8_t bufferSend[3]; | |
173 bufferSend[0] = 0x02; | |
174 bufferSend[1] = (uint8_t)(mAhSend / 256); | |
175 bufferSend[2] = (uint8_t)(mAhSend & 0xFF); | |
176 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
|
177 init_battery_gas_gauge(); |
38 | 178 } |
179 | |
180 | |
181 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ |