3 Commits 1c24f24635 ... 92cd570cc5

Author SHA1 Message Date
  Heinrich Blatt 92cd570cc5 Verbesserung: Die Response zum externen I2C kann auch zwischen den slots measurement/control funktionen getriggert werden 7 months ago
  Heinrich Blatt b825fec4ce Fix: Ignore-Condition für nicht fertig gestellte TX angefügt. 7 months ago
  Heinrich Blatt e59f26f85e Support für Temperatursensoren hinzugefügt 7 months ago

+ 9 - 7
main_target.c

@@ -105,18 +105,20 @@ int main(void)
     NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
 
     initialize_target_address();
-    
+
     while (1) {
 
-        if(mcu_CommandPending){
+        for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
+
+            if(mcu_CommandPending){
 #ifdef DEBUG_TARGET
-            printf("Calling MCU target action\n");
+                printf("Calling MCU target action\n");
 #endif
-            mcu_i2c_handle(I2C_target_INST);
-            mcu_CommandPending = false;
-        }
+                mcu_i2c_handle(I2C_target_INST);
+                mcu_CommandPending = false;
+            }
+        
         
-        for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
 
             // step 1: update the voltage readings
             battery_slotmgr.read_state(slot);

+ 23 - 2
src/battery_data/battery.c

@@ -8,6 +8,7 @@
 // we need the itnerface for the ADC_TARGET_BASE_ADDRESS constant
 // refactor this somewhen to a general constants file
 #include "src/peripherals/adc/adc_interface.h"
+#include "src/peripherals/temp/tmp1075.h"
 #include "src/config.h"
 
 BatterySlot battery_slots[NUM_SLOTS];
@@ -89,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);
@@ -102,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;
 
@@ -115,6 +134,8 @@ static void batteryslots_read_state(uint8_t slot) {
         printf("Slot %d voltage: %d mV and %d mA (pwm shunt) (hi side voltage: %d mV, hi side current: %d mA)\n", slot, battery_slots[slot].measurement.voltage, battery_slots[slot].measurement.current, hi_voltage, shunt_current);
 #endif
     }
+
+    battery_slots[slot].measurement.temperature = read_temperature(slot);
 }
 
 static void batteryslots_adjust_current(uint8_t slot) {

+ 8 - 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 
@@ -58,6 +58,9 @@
 // printf target i2c interrupts (where the mcu is the i2c target)
 #define DEBUG_TARGET 1
 
+// debug temperature sensor
+//#define DEBUG_TEMPERATURE
+
 //------------
 // Section for configuring error tresholds
 //------------
@@ -95,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;
 }
 

+ 1 - 0
src/peripherals/dac/dac.h

@@ -1,4 +1,5 @@
 #ifndef DAC_H_
+#define DAC_H_
 #include "ti/driverlib/dl_i2c.h"
 #include "ti_msp_dl_config.h"
 #include <stdbool.h>

+ 29 - 0
src/peripherals/temp/tmp1075.c

@@ -0,0 +1,29 @@
+
+#include "src/peripherals/temp/tmp1075.h"
+
+#include "src/interfaces/i2c_controller.h"
+
+#include <stdio.h>
+
+/**
+ * Read Temperature
+ * of a former initalized TMP1075 sensor
+ */
+uint16_t read_temperature(uint8_t slot) {
+    controllerRxPackage.len = 3;
+    controllerRxPackage.count = 0;
+    controllerRxPackage.complete = false;
+    i2c_hal.read(TMP1075_BASE_ADDRESS + slot);
+
+    while(!controllerRxPackage.complete);
+    uint16_t byte1 = controllerRxPackage.packet[0];
+    uint8_t byte2 = controllerRxPackage.packet[1];
+    
+    uint16_t temp = ((byte1 << 4) | (byte2 >> 4)) * 62.5;
+
+#ifdef DEBUG_TEMPERATURE
+    printf("Temperatur von slot %d: 0x%02X 0x%02X -> %d \n", slot, byte1, byte2, temp);
+#endif
+
+    return temp;
+}

+ 10 - 0
src/peripherals/temp/tmp1075.h

@@ -0,0 +1,10 @@
+#ifndef TMP1075_H_
+#define TMP1075_H_
+
+#include <stdint.h>
+
+#define TMP1075_BASE_ADDRESS 0x48
+
+uint16_t read_temperature(uint8_t slot);
+
+#endif