Bladeren bron

Änderungen:
* Fehler können jetzt über i2c resetted werden
Refacotring:
* Dateinamen sind konsistent
* i2c interface neu gruppiert

Heinrich Blatt 8 maanden geleden
bovenliggende
commit
5bb9297a08

+ 2 - 2
main_target.c

@@ -3,8 +3,8 @@
 #include "ti_msp_dl_config.h"
 //#include "ti/driverlib/dl_i2c.h"
 #include <stdio.h>
-#include "src/i2c_comm/mcu_slave_interface.h"
-#include "src/interfaces/i2c_controller_interface.h"
+#include "src/interfaces/i2c_target.h"
+#include "src/interfaces/i2c_controller.h"
 
 
 #define DELAY_CYCLE  (50000000)

+ 1 - 1
src/battery_data/battery.c

@@ -3,7 +3,7 @@
 #include "ti_msp_dl_config.h"
 #include "src/peripherals/dac/dac.h"
 #include "src/peripherals/adc/adc.h"
-#include "src/interfaces/i2c_controller_interface.h"
+#include "src/interfaces/i2c_controller.h"
 // 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"

+ 0 - 81
src/i2c_comm/mcu_slave_interface.c

@@ -1,81 +0,0 @@
-/*
-References:
-https://stackoverflow.com/questions/246127/why-is-volatile-needed-in-c
-
-*/
-
-#include "mcu_slave_interface.h"
-#include "src/battery_data/battery.h"
-#include "ti/driverlib/dl_i2c.h"
-#include <stdio.h>
-#include <string.h>
-#include "src/peripherals/dac/dac.h"
-#include <inttypes.h>
-
-/*Function to Rx and Tx data from Target to Controller*/
-// The code has multiple i2c instances (multiple MCUs connected) from which we
-// need to select the right one, passing a pointer as an argument
-
-void mcu_i2c_handle(I2C_Regs *i2c) {
-  printf("MCU interrupt triggered\n");
-  uint8_t receivedCommand = DL_I2C_receiveTargetData(i2c);
-  printf("[SLAVE] Received Command: 0x%02X\n", receivedCommand);
-  uint8_t tx_buffer[8] = {0};
-  // changed to volatile variable, so that the compiler cannot optimize the
-  // variable out and is forced to do as told by the code
-  volatile uint8_t rx_buffer[8] = {0};
-  /*Handling GET commands with bitmasking*/
-  // GET command for ADC(Battery Measurement): Voltage, Current, Temperature
-  if ((receivedCommand & 0xF0) == 0x60) {
-    uint8_t slot = receivedCommand & 0x0F;
-    if (slot > NUM_SLOTS) {
-      DL_I2C_flushTargetTXFIFO(i2c);
-      return;
-    }
-
-    // Copying the memory block from battery_measure struct to tx_buffer:
-    // @todo check if this memcpy is even needed, probably 
-    // &battery_slots[slot].measurement can be directly used as buffer for DL_I2C_fillTargetTXFIFO
-    memcpy(tx_buffer, &battery_slots[slot].measurement, sizeof(BatteryMeasurement));
-    DL_I2C_fillTargetTXFIFO(i2c, tx_buffer, sizeof(BatteryMeasurement));
-    printf("Battery Measurement Sent to MCU. \n");
-    DL_I2C_flushTargetTXFIFO(i2c);
-
-  } else if (receivedCommand == CMD_SET_CURRENT) {
-    // Read incoming bytes from the Controller:
-    uint8_t rx_index = 0;
-    while (rx_index < 4) {
-      // TODO: Need to have a workaround, currently the code is getting stuck on
-      // the first trigger and provides result on the second trigger
-      if (!DL_I2C_isTargetRXFIFOEmpty(i2c)) {
-        rx_buffer[rx_index] = DL_I2C_receiveTargetData(i2c);
-        rx_index++;
-      }
-    }
-    // we expect 4 bytes:
-    // 1. command
-    // 2. slot_id
-    // 3 + 4. current (int16)
-    if (rx_index != 4) {
-      printf("ERROR: Incomplete I2C Rx: received %d%zu bytes\n", rx_index, 4);
-      DL_I2C_flushTargetRXFIFO(i2c);
-      rx_index = 0;
-      return;
-    }
-    uint8_t slot = rx_buffer[1]; // first byte is the slot id (0..3)
-    battery_slots[slot].set_current = *((rx_buffer)+2); // byte 3+4 is the current
-    printf("Slot id: %d, Current: %" SCNd16 "\n", slot, battery_slots[slot].set_current);
-    if (battery_slots[slot].set_current >= 0) {
-      DAC_SingleWrite(slot, battery_slots[slot].set_current);
-    } else if (battery_slots[slot].set_current < 0) {
-        
-        DL_TimerG_startCounter(PWM_0_INST);
-        DL_TimerG_setCaptureCompareValue(PWM_0_INST, -1*battery_slots[slot].set_current, DL_TIMER_CC_0_INDEX); // update ccr0 value
-
-    } else {
-      // do nothing, charge or discharge
-      printf("state is idle");
-    }
-
-  }
-}

+ 1 - 1
src/interfaces/i2c_hal.c → src/interfaces/i2c_controller.c

@@ -3,7 +3,7 @@
  * communication of MSPM0 SDK compatible with ADC (MCP3426/7/8) and DAC
  * (MCP34728)
  */
-#include "src/interfaces/i2c_controller_interface.h"
+#include "src/interfaces/i2c_controller.h"
 #include "ti/driverlib/dl_i2c.h"
 #include "ti_msp_dl_config.h"
 #include <stdio.h>

+ 1 - 1
src/interfaces/i2c_controller_interface.h → src/interfaces/i2c_controller.h

@@ -4,7 +4,7 @@
 * Our current functions in ADC and DAC are tightly coupled with I2C communication. 
 * Need a communication mechanism to isolate the flow of the ADC function from Asynchronous calls.
 * Decision:
-* i2c_controller_interface is a header file which defines what the I2C peripheral does:
+* i2c_controller is a header file which defines what the I2C peripheral does:
 * - i2c READ
 * - i2c WRITE
 * - i2c start controller transfer

+ 87 - 0
src/interfaces/i2c_target.c

@@ -0,0 +1,87 @@
+/*
+References:
+https://stackoverflow.com/questions/246127/why-is-volatile-needed-in-c
+
+*/
+
+#include "i2c_target.h"
+#include "src/battery_data/battery.h"
+#include "ti/driverlib/dl_i2c.h"
+#include <stdio.h>
+#include <string.h>
+#include "src/peripherals/dac/dac.h"
+#include <inttypes.h>
+
+/*Function to Rx and Tx data from Target to Controller*/
+// The code has multiple i2c instances (multiple MCUs connected) from which we
+// need to select the right one, passing a pointer as an argument
+
+void mcu_i2c_handle(I2C_Regs *i2c) {
+    printf("MCU interrupt triggered\n");
+    uint8_t receivedCommand = DL_I2C_receiveTargetData(i2c);
+    printf("[SLAVE] Received Command: 0x%02X\n", receivedCommand);
+    uint8_t tx_buffer[8] = {0};
+    // changed to volatile variable, so that the compiler cannot optimize the
+    // variable out and is forced to do as told by the code
+    volatile uint8_t rx_buffer[8] = {0};
+    /*Handling GET commands with bitmasking*/
+    // GET command for ADC(Battery Measurement): Voltage, Current, Temperature
+    if ((receivedCommand & 0x0F) == CMD_GET_MEASUREMENT) {
+        uint8_t slot = receivedCommand & 0xF0;
+        if (slot > NUM_SLOTS) {
+            DL_I2C_flushTargetTXFIFO(i2c);
+            return;
+        }
+
+        // Copying the memory block from battery_measure struct to tx_buffer:
+        // @todo check if this memcpy is even needed, probably 
+        // &battery_slots[slot].measurement can be directly used as buffer for DL_I2C_fillTargetTXFIFO
+        memcpy(tx_buffer, &battery_slots[slot].measurement, sizeof(BatteryMeasurement));
+        DL_I2C_fillTargetTXFIFO(i2c, tx_buffer, sizeof(BatteryMeasurement));
+        printf("Battery Measurement Sent to MCU. \n");
+        DL_I2C_flushTargetTXFIFO(i2c);
+  
+    } else if (receivedCommand == CMD_SET_CURRENT) {
+        // Read incoming bytes from the Controller:
+        uint8_t rx_index = 0;
+        while (rx_index < 4) {
+            // TODO: Need to have a workaround, currently the code is getting stuck on
+            // the first trigger and provides result on the second trigger
+            if (!DL_I2C_isTargetRXFIFOEmpty(i2c)) {
+              rx_buffer[rx_index] = DL_I2C_receiveTargetData(i2c);
+              rx_index++;
+            }
+        }
+        // we expect 4 bytes:
+        // 1. command
+        // 2. slot_id
+        // 3 + 4. current (int16)
+        if (rx_index != 4) {
+            printf("ERROR: Incomplete I2C Rx: received %d%zu bytes\n", rx_index, 4);
+            DL_I2C_flushTargetRXFIFO(i2c);
+            rx_index = 0;
+            return;
+        }
+        uint8_t slot = rx_buffer[1]; // first byte is the slot id (0..3)
+        battery_slots[slot].set_current = *((rx_buffer)+2); // byte 3+4 is the current
+        printf("Slot id: %d, Current: %" SCNd16 "\n", slot, battery_slots[slot].set_current);
+        if (battery_slots[slot].set_current >= 0) {
+            DAC_SingleWrite(slot, battery_slots[slot].set_current);
+        } else if (battery_slots[slot].set_current < 0) {
+            
+            DL_TimerG_startCounter(PWM_0_INST);
+            DL_TimerG_setCaptureCompareValue(PWM_0_INST, -1*battery_slots[slot].set_current, DL_TIMER_CC_0_INDEX); // update ccr0 value
+
+        } else {
+            // do nothing, charge or discharge
+            printf("state is idle");
+        }
+    } else if (receivedCommand == CMD_CELAR_ERR) {
+        uint8_t slot = receivedCommand & 0xF0;
+        if (slot > NUM_SLOTS) {
+            DL_I2C_flushTargetTXFIFO(i2c);
+            return;
+        }
+        *battery_slots[slot].state = SLOT_STATE_OK;
+    }
+}

+ 1 - 1
src/i2c_comm/mcu_slave_interface.h → src/interfaces/i2c_target.h

@@ -11,7 +11,7 @@ typedef enum{
     CMD_SET_CURRENT= 0x05,
     CMD_GET_MEASUREMENT= 0x06, 
     CMD_GET_BATTERY_STATE= 0x07, 
-    CMD_SET_HOV_CLEAR= 0x08
+    CMD_CELAR_ERR= 0x08
 }mcu_I2C_command;
 
 void mcu_i2c_handle(I2C_Regs *i2c);

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

@@ -3,7 +3,7 @@
 #include <stdint.h>
 #include <stdio.h>
 #include "src/peripherals/adc/adc_interface.h"
-#include "src/interfaces/i2c_controller_interface.h"
+#include "src/interfaces/i2c_controller.h"
 
 
 //static ADC_Params adc_params;

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

@@ -49,7 +49,7 @@ D15.
 */
 
 
-#include "src/interfaces/i2c_controller_interface.h"
+#include "src/interfaces/i2c_controller.h"
 #include "src/peripherals/adc/adc_interface.h"
 #include "ti_msp_dl_config.h"
 #include <stdio.h>

+ 1 - 1
src/peripherals/dac/dac.c

@@ -1,5 +1,5 @@
 #include "dac.h"
-#include "src/interfaces/i2c_controller_interface.h"
+#include "src/interfaces/i2c_controller.h"
 #include "ti/driverlib/dl_i2c.h"
 #include "ti_msp_dl_config.h"
 #include "src/battery_data/battery.h"