diff 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
line wrap: on
line diff
--- a/Small_CPU/Src/batteryGasGauge.c	Tue Apr 02 18:18:22 2019 +0200
+++ b/Small_CPU/Src/batteryGasGauge.c	Tue Apr 02 14:53:15 2019 +0200
@@ -27,11 +27,11 @@
 #include "stm32f4xx_hal.h"
 #include "i2c.h"
 
-float battery_f_voltage = 0;
-float battery_f_charge_percent = 0;
+static float battery_f_voltage = 0;
+static float battery_f_charge_percent = 0;
 
 #define BGG_BATTERY_OFFSET          (26123)  //; 65536-(3,35Ah/0,085mAh)
-#define BGG_BATTERY_DEVIDER         (394)    //; 3,35Ah/0,085mAh/100 [%]
+#define BGG_BATTERY_DIVIDER         (394)    //; 3,35Ah/0,085mAh/100 [%]
 
 float get_voltage(void)
 {
@@ -61,8 +61,14 @@
 	
 	uint8_t buffer[2];
 	buffer[0] = 0x01;
-	buffer[1] = 0xF8;// true: F8 = 11111000, wrong/old comment: 11101000
-	I2C_Master_Transmit(  DEVICE_BATTERYGAUGE, buffer, 2);
+
+	// F8 = 11111000:
+	// Vbat 3.0V (11)
+	// Prescale M = 128 (111)
+	// AL/CC pin disable (0)
+	// Shutdown (0)
+	buffer[1] = 0xF8;
+	I2C_Master_Transmit(DEVICE_BATTERYGAUGE, buffer, 2);
 }
 
 
@@ -86,7 +92,7 @@
 	battery_f_charge_percent_local =  (float)(bufferReceive[2] * 256);
 	battery_f_charge_percent_local += (float)(bufferReceive[3]);
 	battery_f_charge_percent_local -= BGG_BATTERY_OFFSET;
-	battery_f_charge_percent_local /= BGG_BATTERY_DEVIDER;
+	battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER;
 	
 	if(battery_f_charge_percent_local < 0)
 		battery_f_charge_percent_local = 0;
@@ -120,8 +126,9 @@
 	
 	if(percentage >= 100)
 		mAhSend = 0xFFFF;
-	else	
-		mAhSend = (uint16_t)(percentage * 655.35f);
+	else {
+		mAhSend = (percentage * BGG_BATTERY_DIVIDER) + BGG_BATTERY_OFFSET;
+	}
 	
 	uint8_t bufferSend[3];
 	bufferSend[0] = 0x02;