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