Ver código fonte

Fix: Ignore-Condition für nicht fertig gestellte TX angefügt.

Heinrich Blatt 7 meses atrás
pai
commit
b825fec4ce

+ 20 - 2
src/battery_data/battery.c

@@ -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;
 

+ 5 - 2
src/config.h

@@ -5,7 +5,7 @@
 
 // How many slots do we currently support?
 // use 1 for debugging, 4 for production
-#define NUM_SLOTS 1
+#define NUM_SLOTS 4
 
 //Battery Tolerance
 // how much tolerance do we allow if we charge / discharge before we 
@@ -98,4 +98,7 @@
 // controller mode only
 // (target for the other MCU is treated differently)
 #define I2C_TX_MAX_PACKET_SIZE 5
-#define I2C_RX_MAX_PACKET_SIZE 5
+#define I2C_RX_MAX_PACKET_SIZE 5
+
+// how many cycles do we wait until a tx message is completed?
+#define MAX_I2C_WAIT_RX 32000*10

+ 3 - 1
src/peripherals/adc/adc.c

@@ -35,7 +35,9 @@ uint16_t read_adc_channel(uint8_t slot, uint8_t channel) {
                 adc_params.factor = 1000; // get microvolts
             }
             //printf("Config: Memory address of batteries: %p\n", &batteries[0]);
-            adc_hal.configure(slot, &adc_params);
+            if (!adc_hal.configure(slot, &adc_params)) {
+                return 0xffff;
+            }
             if (adc_params.continuous != 1) {
                 // in one shot mode we wait first to get the result
                 adc_state = ADC_STATE_WAIT;

+ 8 - 1
src/peripherals/adc/adc_hal.c

@@ -142,7 +142,14 @@ static bool adc_configure(uint8_t slot_id, ADC_Params *params) {
     controllerTxPackage.complete = false;
     i2c_hal.write(ADC_TARGET_BASE_ADDRESS);
     
-    while(!controllerTxPackage.complete);
+    uint32_t n_cycles = 0;
+    while(!controllerTxPackage.complete && n_cycles++ < MAX_I2C_WAIT_RX);
+    if (n_cycles == MAX_I2C_WAIT_RX) {
+#ifdef DEBUG_ADC
+        printf("[ADC] No Response to the config byte!\n");
+#endif
+        return false;
+    }
     return true;
 }