comparison Small_CPU/Src/batteryGasGauge.c @ 408:2fc08a0d1ec3 Improment_NVM

Bugfix invalid voltage / temperatur after coldstart: In case of a cold start (complete power on / off) the RTE could send devie data before values like voltage or temperature have been measured for the first time. As result the device history data was set to it limits causing a minimal voltage of 2V to be displayed in history memory. Workaround for voltage is to initialize it to 6V at startup but for temperature a default value having no impact to the history memory is not possible
author ideenmodellierer
date Sun, 12 Jan 2020 19:50:14 +0100
parents 2defc8cd93ce
children aa286a4926c2
comparison
equal deleted inserted replaced
407:b11e50415982 408:2fc08a0d1ec3
26 #include "batteryGasGauge.h" 26 #include "batteryGasGauge.h"
27 #include "baseCPU2.h" 27 #include "baseCPU2.h"
28 #include "stm32f4xx_hal.h" 28 #include "stm32f4xx_hal.h"
29 #include "i2c.h" 29 #include "i2c.h"
30 30
31 static float battery_f_voltage = 0; 31 static float battery_f_voltage = 6.0; /* max assumed voltage */
32 static float battery_f_charge_percent = 0; 32 static float battery_f_charge_percent = 0;
33 33
34 #define BGG_BATTERY_OFFSET (26123) //; 65536-(3,35Ah/0,085mAh) 34 #define BGG_BATTERY_OFFSET (26123) //; 65536-(3,35Ah/0,085mAh)
35 #define BGG_BATTERY_DIVIDER (394) //; 3,35Ah/0,085mAh/100 [%] 35 #define BGG_BATTERY_DIVIDER (394) //; 3,35Ah/0,085mAh/100 [%]
36 36
115 115
116 float battery_f_voltage_local; 116 float battery_f_voltage_local;
117 float battery_f_charge_percent_local; 117 float battery_f_charge_percent_local;
118 118
119 uint8_t bufferReceive[10]; 119 uint8_t bufferReceive[10];
120 I2C_Master_Receive( DEVICE_BATTERYGAUGE, bufferReceive, 10); 120
121 if(I2C_Master_Receive(DEVICE_BATTERYGAUGE, bufferReceive, 10) == HAL_OK)
122 {
123 battery_f_voltage_local = (float)(bufferReceive[8] * 256);
124 battery_f_voltage_local += (float)(bufferReceive[9]);
125 battery_f_voltage_local *= (float)6 / (float)0xFFFF;
126
127 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh
128 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256);
129 battery_f_charge_percent_local += (float)(bufferReceive[3]);
130 battery_f_charge_percent_local -= BGG_BATTERY_OFFSET;
131 battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER;
121 132
122 battery_f_voltage_local = (float)(bufferReceive[8] * 256); 133 if(battery_f_charge_percent_local < 0)
123 battery_f_voltage_local += (float)(bufferReceive[9]); 134 battery_f_charge_percent_local = 0;
124 battery_f_voltage_local *= (float)6 / (float)0xFFFF;
125 135
126 // max/full: 0.085 mAh * 1 * 65535 = 5570 mAh 136 battery_f_voltage = battery_f_voltage_local;
127 battery_f_charge_percent_local = (float)(bufferReceive[2] * 256); 137 battery_f_charge_percent = battery_f_charge_percent_local;
128 battery_f_charge_percent_local += (float)(bufferReceive[3]); 138 }
129 battery_f_charge_percent_local -= BGG_BATTERY_OFFSET;
130 battery_f_charge_percent_local /= BGG_BATTERY_DIVIDER;
131
132 if(battery_f_charge_percent_local < 0)
133 battery_f_charge_percent_local = 0;
134
135 battery_f_voltage = battery_f_voltage_local;
136 battery_f_charge_percent = battery_f_charge_percent_local;
137 } 139 }
138 140
139 141
140 void battery_gas_gauge_set_charge_full(void) 142 void battery_gas_gauge_set_charge_full(void)
141 { 143 {