comparison Small_CPU/Src/batteryGasGauge.c @ 228:f9ba924d188e div-fixes-4-1

Bugfix: set battery percentage correctly after RTE update When the RTE is rebooted, the battery percentage on the display is zeroed. However, when selecting SYS2-reboot-maintenance, the previously known battery percentage can be manually restored. Interestingly, when the restore reported a percentage A, choosing that resulted in a percentage B to be displayed again. With B much smaller than A. So, rebooting the RTE multiple times resulted in an seemingly empty battery, while it definitely is not. The reason for this is a relatively simple bug in the RTE code. This commit fixes the conversion between the internal LTC2941 registers and the percentage value to be displayed. Obviously, from and to the internal registers need to be symmetrical. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Tue, 02 Apr 2019 14:53:15 +0200
parents 5f11787b4f42
children b23de15e2861
comparison
equal deleted inserted replaced
227:d5891007fc4c 228:f9ba924d188e
25 #include "batteryGasGauge.h" 25 #include "batteryGasGauge.h"
26 #include "baseCPU2.h" 26 #include "baseCPU2.h"
27 #include "stm32f4xx_hal.h" 27 #include "stm32f4xx_hal.h"
28 #include "i2c.h" 28 #include "i2c.h"
29 29
30 float battery_f_voltage = 0; 30 static float battery_f_voltage = 0;
31 float battery_f_charge_percent = 0; 31 static float battery_f_charge_percent = 0;
32 32
33 #define BGG_BATTERY_OFFSET (26123) //; 65536-(3,35Ah/0,085mAh) 33 #define BGG_BATTERY_OFFSET (26123) //; 65536-(3,35Ah/0,085mAh)
34 #define BGG_BATTERY_DEVIDER (394) //; 3,35Ah/0,085mAh/100 [%] 34 #define BGG_BATTERY_DIVIDER (394) //; 3,35Ah/0,085mAh/100 [%]
35 35
36 float get_voltage(void) 36 float get_voltage(void)
37 { 37 {
38 #ifdef OSTC_ON_DISCOVERY_HARDWARE 38 #ifdef OSTC_ON_DISCOVERY_HARDWARE
39 return 3.0f; 39 return 3.0f;
59 return; 59 return;
60 #endif 60 #endif
61 61
62 uint8_t buffer[2]; 62 uint8_t buffer[2];
63 buffer[0] = 0x01; 63 buffer[0] = 0x01;
64 buffer[1] = 0xF8;// true: F8 = 11111000, wrong/old comment: 11101000 64
65 I2C_Master_Transmit( DEVICE_BATTERYGAUGE, buffer, 2); 65 // F8 = 11111000:
66 // Vbat 3.0V (11)
67 // Prescale M = 128 (111)
68 // AL/CC pin disable (0)
69 // Shutdown (0)
70 buffer[1] = 0xF8;
71 I2C_Master_Transmit(DEVICE_BATTERYGAUGE, buffer, 2);
66 } 72 }
67 73
68 74
69 void battery_gas_gauge_get_data(void) 75 void battery_gas_gauge_get_data(void)
70 { 76 {
84 90
85 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh 91 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh
86 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256); 92 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256);
87 battery_f_charge_percent_local += (float)(bufferReceive[3]); 93 battery_f_charge_percent_local += (float)(bufferReceive[3]);
88 battery_f_charge_percent_local -= BGG_BATTERY_OFFSET; 94 battery_f_charge_percent_local -= BGG_BATTERY_OFFSET;
89 battery_f_charge_percent_local /= BGG_BATTERY_DEVIDER; 95 battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER;
90 96
91 if(battery_f_charge_percent_local < 0) 97 if(battery_f_charge_percent_local < 0)
92 battery_f_charge_percent_local = 0; 98 battery_f_charge_percent_local = 0;
93 99
94 battery_f_voltage = battery_f_voltage_local; 100 battery_f_voltage = battery_f_voltage_local;
118 124
119 uint16_t mAhSend; 125 uint16_t mAhSend;
120 126
121 if(percentage >= 100) 127 if(percentage >= 100)
122 mAhSend = 0xFFFF; 128 mAhSend = 0xFFFF;
123 else 129 else {
124 mAhSend = (uint16_t)(percentage * 655.35f); 130 mAhSend = (percentage * BGG_BATTERY_DIVIDER) + BGG_BATTERY_OFFSET;
131 }
125 132
126 uint8_t bufferSend[3]; 133 uint8_t bufferSend[3];
127 bufferSend[0] = 0x02; 134 bufferSend[0] = 0x02;
128 bufferSend[1] = (uint8_t)(mAhSend / 256); 135 bufferSend[1] = (uint8_t)(mAhSend / 256);
129 bufferSend[2] = (uint8_t)(mAhSend & 0xFF); 136 bufferSend[2] = (uint8_t)(mAhSend & 0xFF);