3 次代碼提交 92cd570cc5 ... a17001f7dc

作者 SHA1 備註 提交日期
  Heinrich Blatt a17001f7dc Bugfix: I2C Kommunikation failsafe gemacht 7 月之前
  Heinrich Blatt ae8e04a4bc Verbesserung: ADC Adressen für höhere slot ids gefixt 7 月之前
  Heinrich Blatt 0be7681dff Bugfix: PWMs auswählen angepasst 7 月之前
共有 5 個文件被更改,包括 71 次插入34 次删除
  1. 5 1
      src/battery_data/battery.c
  2. 2 1
      src/battery_data/battery.h
  3. 43 22
      src/interfaces/i2c_controller.c
  4. 3 3
      src/interfaces/i2c_target.c
  5. 18 7
      src/peripherals/adc/adc_hal.c

+ 5 - 1
src/battery_data/battery.c

@@ -19,7 +19,7 @@ static void set_dac(uint8_t slot, uint16_t value) {
 }
 static void set_pwm(uint8_t slot, uint16_t value) {
     battery_slots[slot].pwm_value = value;
-    DL_TimerG_setCaptureCompareValue(battery_slots[0].timer, value, DL_TIMER_CC_1_INDEX);
+    DL_TimerG_setCaptureCompareValue(battery_slots[slot].timer, value, DL_TIMER_CC_1_INDEX);
 }
 static void batteryslots_disable(uint8_t slot) {
     if (battery_slots[slot].dac_value != 0) {
@@ -35,10 +35,14 @@ static void batteryslots_init() {
 
     // initialize data structures
     battery_slots[0].timer = PWM_0_INST;
+    battery_slots[0].adc_addr = ADC_TARGET_BASE_ADDRESS;
 #if NUM_SLOTS == 4
     battery_slots[1].timer = PWM_1_INST;
+    battery_slots[1].adc_addr = ADC_TARGET_BASE_ADDRESS+4;
     battery_slots[2].timer = PWM_2_INST;
+    battery_slots[2].adc_addr = ADC_TARGET_BASE_ADDRESS+2;
     battery_slots[3].timer = PWM_3_INST;
+    battery_slots[3].adc_addr = ADC_TARGET_BASE_ADDRESS+6;
 #endif
 
     for(uint8_t i=0; i< NUM_SLOTS; i++){

+ 2 - 1
src/battery_data/battery.h

@@ -8,7 +8,7 @@
 #include "src/config.h"
 
 //Battery Discharge Safety Check
-typedef enum{
+typedef enum: uint8_t{
     SLOT_STATE_OK= 0x00,
     SLOT_STATE_SOV= 0x01,
     SLOT_ERR_HOV= (0x02 | 0x80),
@@ -48,6 +48,7 @@ typedef struct {
     GPTIMER_Regs *timer;
     SlotState *state;
     int16_t high_side_voltage;
+    uint8_t adc_addr;
 
 } BatterySlot;
 

+ 43 - 22
src/interfaces/i2c_controller.c

@@ -33,9 +33,15 @@ static bool msp_i2c_write(uint8_t const TARGET_ADDRESS) {
     }
     
     // **Wait for I2C Bus to be Free**
-    while (DL_I2C_getControllerStatus(I2C_controller_INST) &
-          DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
-      ;
+    uint32_t n_cycles = 0;
+    while ((DL_I2C_getControllerStatus(I2C_controller_INST) &
+            DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && n_cycles++ < MAX_I2C_WAIT_RX)
+        ;
+    if (n_cycles == MAX_I2C_WAIT_RX) {
+        printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
+        DL_I2C_resetControllerTransfer(I2C_controller_INST); 
+        return false;
+    }
 
     // **Start I2C Write Transaction**
     DL_I2C_startControllerTransfer(I2C_controller_INST, TARGET_ADDRESS,
@@ -49,10 +55,15 @@ static bool msp_i2c_write(uint8_t const TARGET_ADDRESS) {
     }
 #endif
 
-    // ** Wait for the I2C Bus to be FREE **
-    while (DL_I2C_getControllerStatus(I2C_controller_INST) &
-          DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
-      ;
+    n_cycles = 0;
+    while ((DL_I2C_getControllerStatus(I2C_controller_INST) &
+            DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && n_cycles++ < MAX_I2C_WAIT_RX)
+        ;
+    if (n_cycles == MAX_I2C_WAIT_RX) {
+        printf("Error in reading from I2C Bus: Bus is not getting ready after transmit data\n");
+        DL_I2C_resetControllerTransfer(I2C_controller_INST); 
+        return false;
+    }
     
     // **Check if the target address is incorrect
     
@@ -77,23 +88,33 @@ static bool msp_i2c_write(uint8_t const TARGET_ADDRESS) {
 static bool msp_i2c_read(uint8_t const TARGET_ADDRESS) {
 
   
- // Flush any stale data in TX FIFO:
-  DL_I2C_flushControllerRXFIFO(I2C_controller_INST);
-  while (DL_I2C_getControllerStatus(I2C_controller_INST) &
-         DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
-    ;
-  DL_I2C_startControllerTransfer(I2C_controller_INST, TARGET_ADDRESS,
-                                 DL_I2C_CONTROLLER_DIRECTION_RX, controllerRxPackage.len);
+    // Flush any stale data in TX FIFO:
+    DL_I2C_flushControllerRXFIFO(I2C_controller_INST);
+    
+    uint32_t n_cycles = 0;
+    while ((DL_I2C_getControllerStatus(I2C_controller_INST) &
+            DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && n_cycles++ < MAX_I2C_WAIT_RX)
+        ;
+    if (n_cycles == MAX_I2C_WAIT_RX) {
+        printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
+    }
+
+    DL_I2C_startControllerTransfer(I2C_controller_INST, TARGET_ADDRESS,
+                                    DL_I2C_CONTROLLER_DIRECTION_RX, controllerRxPackage.len);
 
-  while (DL_I2C_getControllerStatus(I2C_controller_INST) &
-         DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
-    ;
+    n_cycles = 0;
+    while ((DL_I2C_getControllerStatus(I2C_controller_INST) &
+            DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && n_cycles++ < MAX_I2C_WAIT_RX)
+        ;
+    if (n_cycles == MAX_I2C_WAIT_RX) {
+        printf("Error in reading from I2C Bus: Bus is not getting ready after transmit start\n");
+    }
+        
+    DL_I2C_enableInterrupt(I2C_controller_INST,
+                            DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
     
-  DL_I2C_enableInterrupt(I2C_controller_INST,
-                         DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
-  
-  
-  return true;
+    
+    return true;
 }
 
 /**

+ 3 - 3
src/interfaces/i2c_target.c

@@ -35,7 +35,7 @@ void initialize_target_address() {
 void mcu_i2c_handle(I2C_Regs *i2c) {
     uint8_t receivedByte = DL_I2C_receiveTargetData(i2c);
     uint8_t receivedCommand = (receivedByte & 0x0F);
-    uint8_t slot = (receivedByte & 0xF0);
+    uint8_t slot = ((receivedByte & 0xF0) >> 4);
 
 #ifdef DEBUG_TARGET
     printf("[SLAVE] Received Byte: 0x%02X\n", receivedByte);
@@ -52,13 +52,13 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
             return;
         }
 
-        DL_I2C_fillTargetTXFIFO(i2c, &battery_slots[slot].measurement, sizeof(BatteryMeasurement));
+        DL_I2C_fillTargetTXFIFO(i2c, (uint8_t *)&battery_slots[slot].measurement, sizeof(BatteryMeasurement));
 
 #ifdef DEBUG_TARGET
         printf("Battery Measurement Sent to MCU. \n");
 #endif
         printf("");
-        DL_I2C_flushTargetTXFIFO(i2c);
+        //DL_I2C_flushTargetTXFIFO(i2c);
   
     } else if (receivedCommand == CMD_SET_CURRENT) {
         // Read incoming bytes from the Controller:

+ 18 - 7
src/peripherals/adc/adc_hal.c

@@ -124,9 +124,13 @@ static bool adc_configure(uint8_t slot_id, ADC_Params *params) {
     printf("Config Byte: 0x%02X\n", controllerTxPackage.packet[0]);
 #endif
     // Wait for I2C Bus to be Free**
-    while (DL_I2C_getControllerStatus(I2C_controller_INST) &
-          DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
+    uint32_t n_cycles = 0;
+    while ((DL_I2C_getControllerStatus(I2C_controller_INST) &
+            DL_I2C_CONTROLLER_STATUS_BUSY_BUS) && n_cycles++ < MAX_I2C_WAIT_RX)
         ;
+    if (n_cycles == MAX_I2C_WAIT_RX) {
+        printf("Error in reading from I2C Bus: Bus is not getting ready on config byte\n");
+    }
     if(controllerTxPackage.packet[0] == 0xFF){
         // this clause can only happen if the internal memory management is messed up?!
         // the config function should take care that this is never the case
@@ -140,9 +144,9 @@ static bool adc_configure(uint8_t slot_id, ADC_Params *params) {
     controllerTxPackage.len = 1;
     controllerTxPackage.count = 0;
     controllerTxPackage.complete = false;
-    i2c_hal.write(ADC_TARGET_BASE_ADDRESS);
+    i2c_hal.write(battery_slots[slot_id].adc_addr);
     
-    uint32_t n_cycles = 0;
+    n_cycles = 0;
     while(!controllerTxPackage.complete && n_cycles++ < MAX_I2C_WAIT_RX);
     if (n_cycles == MAX_I2C_WAIT_RX) {
 #ifdef DEBUG_ADC
@@ -166,7 +170,7 @@ Conversion mode, writing this bit to “1” initiates a new conversion.
 */
 
 static bool adc_is_ready(uint8_t slot_id, ADC_Params *params) {
-    uint8_t adc_address = ADC_TARGET_BASE_ADDRESS + slot_id;
+    uint8_t adc_address = battery_slots[slot_id].adc_addr;
     controllerRxPackage.len = 3;
     controllerRxPackage.count = 0;
     controllerRxPackage.complete = false;
@@ -194,8 +198,15 @@ static int16_t read_adc_raw_data(uint8_t slot_id, ADC_Params *params) {
     controllerRxPackage.count = 0;
     controllerRxPackage.complete = false;
     
-    i2c_hal.read(ADC_TARGET_BASE_ADDRESS + slot_id);
-    while(!controllerRxPackage.complete);
+    i2c_hal.read(battery_slots[slot_id].adc_addr);
+    uint32_t n_cycles = 0;
+    while(!controllerRxPackage.complete && n_cycles++ < MAX_I2C_WAIT_RX);
+    if (n_cycles == MAX_I2C_WAIT_RX) {
+#ifdef DEBUG_ADC
+        printf("[ADC] No Response to the ADC!\n");
+#endif
+        return 0xffff;
+    }
     uint8_t msb = controllerRxPackage.packet[0];
     uint8_t lsb = controllerRxPackage.packet[1];
     uint8_t config_adc_byte = controllerRxPackage.packet[2];