Mercurial > public > ostc4
annotate Small_CPU/Src/batteryGasGauge.c @ 364:3014a698f46d MotionDetection
Update call of motion detection function
author | ideenmodellierer |
---|---|
date | Mon, 17 Jun 2019 19:47:07 +0200 |
parents | b23de15e2861 |
children | 2defc8cd93ce |
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 | |
242
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
74 static void disable_adc(void) |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
75 { |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
76 uint8_t buffer[2]; |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
77 buffer[0] = 0x01; |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
78 |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
79 // 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
|
80 // 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
|
81 |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
82 // 0xF9 = 11111001: |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
83 // see init_battery_gas_gauge() |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
84 // Shutdown (1) |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
85 buffer[1] = 0xF9; |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
86 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
|
87 } |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
88 |
38 | 89 |
90 void battery_gas_gauge_get_data(void) | |
91 { | |
92 #ifdef OSTC_ON_DISCOVERY_HARDWARE | |
93 return; | |
94 #endif | |
95 | |
96 float battery_f_voltage_local; | |
97 float battery_f_charge_percent_local; | |
98 | |
99 uint8_t bufferReceive[10]; | |
100 I2C_Master_Receive( DEVICE_BATTERYGAUGE, bufferReceive, 10); | |
101 | |
102 battery_f_voltage_local = (float)(bufferReceive[8] * 256); | |
103 battery_f_voltage_local += (float)(bufferReceive[9]); | |
104 battery_f_voltage_local *= (float)6 / (float)0xFFFF; | |
105 | |
106 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh | |
107 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256); | |
108 battery_f_charge_percent_local += (float)(bufferReceive[3]); | |
109 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
|
110 battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER; |
38 | 111 |
112 if(battery_f_charge_percent_local < 0) | |
113 battery_f_charge_percent_local = 0; | |
114 | |
115 battery_f_voltage = battery_f_voltage_local; | |
116 battery_f_charge_percent = battery_f_charge_percent_local; | |
117 } | |
118 | |
119 | |
120 void battery_gas_gauge_set_charge_full(void) | |
121 { | |
242
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
122 disable_adc(); |
38 | 123 #ifdef OSTC_ON_DISCOVERY_HARDWARE |
124 return; | |
125 #endif | |
126 | |
127 uint8_t bufferSend[3]; | |
128 bufferSend[0] = 0x02; | |
129 bufferSend[1] = 0xFF; | |
130 bufferSend[2] = 0xFF; | |
131 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
|
132 init_battery_gas_gauge(); |
38 | 133 } |
134 | |
135 | |
136 void battery_gas_gauge_set(float percentage) | |
137 { | |
242
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
138 |
b23de15e2861
Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents:
228
diff
changeset
|
139 disable_adc(); |
38 | 140 #ifdef OSTC_ON_DISCOVERY_HARDWARE |
141 return; | |
142 #endif | |
143 | |
144 uint16_t mAhSend; | |
145 | |
146 if(percentage >= 100) | |
147 mAhSend = 0xFFFF; | |
228
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
148 else { |
f9ba924d188e
Bugfix: set battery percentage correctly after RTE update
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
149 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
|
150 } |
38 | 151 |
152 uint8_t bufferSend[3]; | |
153 bufferSend[0] = 0x02; | |
154 bufferSend[1] = (uint8_t)(mAhSend / 256); | |
155 bufferSend[2] = (uint8_t)(mAhSend & 0xFF); | |
156 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
|
157 init_battery_gas_gauge(); |
38 | 158 } |
159 | |
160 | |
161 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ |