changeset 433:aa286a4926c2 Improve_BatteryGasGauge

Detect startup after power off: Introduced a variable in memory area which is not initialized during startup. In case of a start from power off the memory cell will be initialized (persistant as long as RTE is powered) and charge counter is set to 0 signaling the user to do a complete charge to calibrate the counter.
author ideenmodellierer
date Sun, 23 Feb 2020 21:09:56 +0100
parents 2b4440f75434
children f68f2c4d71c7
files Small_CPU/CPU2-RTE.ld Small_CPU/Src/baseCPU2.c Small_CPU/Src/batteryGasGauge.c
diffstat 3 files changed, 22 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/Small_CPU/CPU2-RTE.ld	Tue Feb 18 12:41:45 2020 +0100
+++ b/Small_CPU/CPU2-RTE.ld	Sun Feb 23 21:09:56 2020 +0100
@@ -181,7 +181,15 @@
 		*(.heap*)
 		__HeapLimit = .;
 	} > RAM
-
+	
+	/* .noinit section contains data which will not be change during startup */
+   .noinit  :
+   {
+     . = ALIGN(4);
+     *(.noinit*)
+     _end = . ;
+   }  > RAM
+  
 	/* .stack_dummy section doesn't contains any symbols. It is only
 	 * used for linker to calculate size of stack sections, and assign
 	 * values to stack symbols later */
--- a/Small_CPU/Src/baseCPU2.c	Tue Feb 18 12:41:45 2020 +0100
+++ b/Small_CPU/Src/baseCPU2.c	Sun Feb 23 21:09:56 2020 +0100
@@ -151,6 +151,8 @@
 #include "stm32f4xx_hal.h"
 #include <stdio.h>
 
+uint8_t coldstart __attribute__((section (".noinit")));
+
 uint8_t hasExternalClock(void) {
 	if ((TM_OTP_Read(0, 0) > 0) && (TM_OTP_Read(0, 0) < 0xFF))
 		return 1;
@@ -181,7 +183,9 @@
 		/* for safety reasons and coming functions */
 		.magic[0] = FIRMWARE_MAGIC_FIRST, .magic[1] = FIRMWARE_MAGIC_SECOND,
 		.magic[2] = FIRMWARE_MAGIC_CPU2_RTE, /* the magic byte for RTE */
-		.magic[3] = FIRMWARE_MAGIC_END };
+		.magic[3] = FIRMWARE_MAGIC_END
+};
+
 
 uint8_t firmwareVersionHigh(void) {
 	return cpu2_FirmwareData.versionFirst;
@@ -339,7 +343,11 @@
 	init_battery_gas_gauge();
 	HAL_Delay(10);
 	battery_gas_gauge_get_data();
-//	battery_gas_gauge_set(0);
+	if(coldstart != 0xA5)
+	{
+		coldstart = 0xA5;
+		battery_gas_gauge_set(0);
+	}
 
 	global.lifeData.battery_voltage = get_voltage();
 	global.lifeData.battery_charge = get_charge();
--- a/Small_CPU/Src/batteryGasGauge.c	Tue Feb 18 12:41:45 2020 +0100
+++ b/Small_CPU/Src/batteryGasGauge.c	Sun Feb 23 21:09:56 2020 +0100
@@ -64,7 +64,7 @@
 	buffer[0] = 0x01;
 
 	// F8 = 11111000:
-	// Vbat 3.0V (11)
+	// ADC auto mode (11)
 	// Prescale M = 128 (111)
 	// AL/CC pin disable (0)
 	// Shutdown (0)
@@ -127,8 +127,8 @@
 		// max/full: 0.085 mAh * 1 * 65535 = 5570 mAh
 		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_DIVIDER;
+		battery_f_charge_percent_local -= BGG_BATTERY_OFFSET;		/* Because of the prescalar 128 the counter assumes a max value of 5570mAh => normalize to 3350mAh*/
+		battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER;		/* transform to percentage */
 
 		if(battery_f_charge_percent_local < 0)
 			battery_f_charge_percent_local = 0;