|
|
@@ -90,12 +90,20 @@ static void batteryslots_read_state(uint8_t slot) {
|
|
|
|
|
|
// step 1: read channel 0 (voltage reading of the cell)
|
|
|
uint16_t bare_voltage = read_adc_channel(slot, 0);
|
|
|
+ if (bare_voltage == 0xffff) {
|
|
|
+ // the voltage reading is invalid -> ignore this cycle.
|
|
|
+ return;
|
|
|
+ }
|
|
|
battery_slots[slot].measurement.voltage = bare_voltage*(56+100)/56; // We have that voltage divider
|
|
|
|
|
|
// DAC branch: we can calculate the current based on the shunt
|
|
|
if (battery_slots[slot].set_current >= 0) {
|
|
|
// read channel 1 (current reading on charge)
|
|
|
bare_voltage = read_adc_channel(slot, 1);
|
|
|
+ if (bare_voltage == 0xffff) {
|
|
|
+ // the voltage reading is invalid -> ignore this cycle.
|
|
|
+ return;
|
|
|
+ }
|
|
|
battery_slots[slot].measurement.current = bare_voltage*10/1000; // current comes in microvolts
|
|
|
#ifdef DEBUG_CTRL
|
|
|
printf("Slot %d voltage: %d mV and %d mA (dac shunt)\n", slot, battery_slots[slot].measurement.voltage, battery_slots[slot].measurement.current);
|
|
|
@@ -103,10 +111,20 @@ static void batteryslots_read_state(uint8_t slot) {
|
|
|
} else {
|
|
|
// we are in PWM mode, the shunt is on the high side
|
|
|
// read channel 2 (voltage reading on 5V side)
|
|
|
- uint16_t shunt_current = 10*read_adc_channel(slot, 2)/1000;
|
|
|
+ bare_voltage = read_adc_channel(slot, 2);
|
|
|
+ if (bare_voltage == 0xffff) {
|
|
|
+ // the voltage reading is invalid -> ignore this cycle.
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ uint16_t shunt_current = 10*bare_voltage/1000;
|
|
|
|
|
|
// read channel 3 (current reading after step conversion, 5V side)
|
|
|
- uint16_t hi_voltage = read_adc_channel(slot, 3)*(56+100)/56;
|
|
|
+ bare_voltage = read_adc_channel(slot, 3);
|
|
|
+ if (bare_voltage == 0xffff) {
|
|
|
+ // the voltage reading is invalid -> ignore this cycle.
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ uint16_t hi_voltage = bare_voltage*(56+100)/56;
|
|
|
|
|
|
uint32_t hi_power = shunt_current*hi_voltage;
|
|
|
|