annotate Small_CPU/Src/batteryGasGauge.c @ 105:68181cd61f20 kittz

fix comments
author Dmitry Romanov <kitt@bk.ru>
date Mon, 24 Dec 2018 16:19:09 +0300
parents 5f11787b4f42
children f9ba924d188e
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
30 float battery_f_voltage = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31 float battery_f_charge_percent = 0;
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)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34 #define BGG_BATTERY_DEVIDER (394) //; 3,35Ah/0,085mAh/100 [%]
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;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 buffer[1] = 0xF8;// true: F8 = 11111000, wrong/old comment: 11101000
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 I2C_Master_Transmit( DEVICE_BATTERYGAUGE, buffer, 2);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 void battery_gas_gauge_get_data(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75 float battery_f_voltage_local;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 float battery_f_charge_percent_local;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78 uint8_t bufferReceive[10];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79 I2C_Master_Receive( DEVICE_BATTERYGAUGE, bufferReceive, 10);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 battery_f_voltage_local = (float)(bufferReceive[8] * 256);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82 battery_f_voltage_local += (float)(bufferReceive[9]);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 battery_f_voltage_local *= (float)6 / (float)0xFFFF;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87 battery_f_charge_percent_local += (float)(bufferReceive[3]);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88 battery_f_charge_percent_local -= BGG_BATTERY_OFFSET;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 battery_f_charge_percent_local /= BGG_BATTERY_DEVIDER;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 if(battery_f_charge_percent_local < 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92 battery_f_charge_percent_local = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 battery_f_voltage = battery_f_voltage_local;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 battery_f_charge_percent = battery_f_charge_percent_local;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 void battery_gas_gauge_set_charge_full(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 uint8_t bufferSend[3];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106 bufferSend[0] = 0x02;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 bufferSend[1] = 0xFF;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 bufferSend[2] = 0xFF;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 I2C_Master_Transmit( DEVICE_BATTERYGAUGE, bufferSend, 3);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 void battery_gas_gauge_set(float percentage)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 #ifdef OSTC_ON_DISCOVERY_HARDWARE
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 #endif
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 uint16_t mAhSend;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 if(percentage >= 100)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122 mAhSend = 0xFFFF;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124 mAhSend = (uint16_t)(percentage * 655.35f);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126 uint8_t bufferSend[3];
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 bufferSend[0] = 0x02;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 bufferSend[1] = (uint8_t)(mAhSend / 256);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 bufferSend[2] = (uint8_t)(mAhSend & 0xFF);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 I2C_Master_Transmit( DEVICE_BATTERYGAUGE, bufferSend, 3);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/