annotate Small_CPU/Src/batteryGasGauge.c @ 271:1303747b5ba2 write-from-sim

cleanup: also write gas and setpoint changes in simulator mode This is related to the previous commit. There is all kinds of logic around that distinguishes real diving mode and simulator mode. This is valid in certain cases. For example, we do not want a simulator session to act as a repetitive dive for subsequent real dives. What is simply wrong, is skipping relevant logic, like gas switches to act differently in the simulator and in a real dive. The main advantage of this commit is code simplicity. Normal users will never see the difference, as simulator dives are not logged (unless compiled in by a developer). Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Sat, 27 Apr 2019 09:32:16 +0200
parents b23de15e2861
children 2defc8cd93ce
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 batteryGasGauge.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 * @version V0.0.1
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
6 * @date 09-Dec-2014
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
7 * @brief LTC2942 Battery Gas Gauge
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
8 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
9 @verbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
10 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
11 ##### stm32f4xx_hal_i2c.c modification #####
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
12 ==============================================================================
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
13 The LTC2942 requires an repeated start condition without stop condition
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
14 for data reception.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
15
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
16 @endverbatim
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
17 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
18 * @attention
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
19 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
20 * <h2><center>&copy; COPYRIGHT(c) 2014 heinrichs weikamp</center></h2>
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
21 *
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
22 ******************************************************************************
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
23 */
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
24 /* Includes ------------------------------------------------------------------*/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
25 #include "batteryGasGauge.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26 #include "baseCPU2.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 #include "stm32f4xx_hal.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28 #include "i2c.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36 float get_voltage(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39 return 3.0f;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 return battery_f_voltage;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 float get_charge(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 return 100.0f;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52 return battery_f_charge_percent;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 void init_battery_gas_gauge(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 uint8_t buffer[2];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 void battery_gas_gauge_get_data(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 float battery_f_voltage_local;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 float battery_f_charge_percent_local;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 uint8_t bufferReceive[10];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100 I2C_Master_Receive( DEVICE_BATTERYGAUGE, bufferReceive, 10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 battery_f_voltage_local = (float)(bufferReceive[8] * 256);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 battery_f_voltage_local += (float)(bufferReceive[9]);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 battery_f_voltage_local *= (float)6 / (float)0xFFFF;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 battery_f_charge_percent_local += (float)(bufferReceive[3]);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 if(battery_f_charge_percent_local < 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 battery_f_charge_percent_local = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 battery_f_voltage = battery_f_voltage_local;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 battery_f_charge_percent = battery_f_charge_percent_local;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 void battery_gas_gauge_set_charge_full(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 {
242
b23de15e2861 Comply to datasheet when resetting battery gauge registers
Jan Mulder <jlmulder@xs4all.nl>
parents: 228
diff changeset
122 disable_adc();
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 uint8_t bufferSend[3];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 bufferSend[0] = 0x02;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 bufferSend[1] = 0xFF;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 bufferSend[2] = 0xFF;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
135
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136 void battery_gas_gauge_set(float percentage)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
142 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
143
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144 uint16_t mAhSend;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
146 if(percentage >= 100)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 uint8_t bufferSend[3];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153 bufferSend[0] = 0x02;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154 bufferSend[1] = (uint8_t)(mAhSend / 256);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155 bufferSend[2] = (uint8_t)(mAhSend & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
160
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
161 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/