Browse Source

Refactoring

Heinrich Blatt 6 months ago
parent
commit
e141c8a9d1

+ 12 - 5
main_target.c

@@ -1,5 +1,6 @@
 
 
 #include "src/battery_data/battery.h"
 #include "src/battery_data/battery.h"
+#include "ti/driverlib/dl_i2c.h"
 #include "ti_msp_dl_config.h"
 #include "ti_msp_dl_config.h"
 //#include "ti/driverlib/dl_i2c.h"
 //#include "ti/driverlib/dl_i2c.h"
 #include <stdio.h>
 #include <stdio.h>
@@ -84,6 +85,10 @@ void I2C_target_INST_IRQHandler(void) {
     break;
     break;
   case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
   case DL_I2C_IIDX_TARGET_ARBITRATION_LOST:
     break;
     break;
+  case DL_I2C_IIDX_TIMEOUT_A:
+  case DL_I2C_IIDX_TIMEOUT_B:
+    DL_I2C_flushTargetRXFIFO(I2C_target_INST);
+    DL_I2C_flushTargetTXFIFO(I2C_target_INST);
   default:
   default:
     break;
     break;
   }
   }
@@ -102,6 +107,9 @@ int main(void)
     while (1) {
     while (1) {
 
 
         for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
         for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
+            if (slot != 0) {
+                continue;
+            }
         
         
             // step 1: update the voltage readings
             // step 1: update the voltage readings
             battery_slotmgr.read_state(slot);
             battery_slotmgr.read_state(slot);
@@ -109,12 +117,11 @@ int main(void)
             // step 2: control loop to adjust the dac / adc values,
             // step 2: control loop to adjust the dac / adc values,
             // but only if no error happens
             // but only if no error happens
             // (0x80 is the error flag of the state)
             // (0x80 is the error flag of the state)
-            if ((*battery_slots[slot].state & 0x80) == 0) {
+            //if ((*battery_slots[slot].state & 0x80) == 0) {
                 battery_slotmgr.adjust_current(slot);
                 battery_slotmgr.adjust_current(slot);
-            } else {
-                battery_slotmgr.disable(slot);
-            }
-            break;
+            //} else {
+            //    battery_slotmgr.disable(slot);
+            //}
         }
         }
         
         
         delay_cycles(MAINLOOP_DELAY);
         delay_cycles(MAINLOOP_DELAY);

+ 0 - 42
src/battery_data/battery.c

@@ -36,14 +36,12 @@ static void batteryslots_init() {
     // initialize data structures
     // initialize data structures
     battery_slots[0].timer = PWM_0_INST;
     battery_slots[0].timer = PWM_0_INST;
     battery_slots[0].adc_addr = ADC_TARGET_BASE_ADDRESS;
     battery_slots[0].adc_addr = ADC_TARGET_BASE_ADDRESS;
-#if NUM_SLOTS == 4
     battery_slots[1].timer = PWM_1_INST;
     battery_slots[1].timer = PWM_1_INST;
     battery_slots[1].adc_addr = ADC_TARGET_BASE_ADDRESS+4;
     battery_slots[1].adc_addr = ADC_TARGET_BASE_ADDRESS+4;
     battery_slots[2].timer = PWM_2_INST;
     battery_slots[2].timer = PWM_2_INST;
     battery_slots[2].adc_addr = ADC_TARGET_BASE_ADDRESS+2;
     battery_slots[2].adc_addr = ADC_TARGET_BASE_ADDRESS+2;
     battery_slots[3].timer = PWM_3_INST;
     battery_slots[3].timer = PWM_3_INST;
     battery_slots[3].adc_addr = ADC_TARGET_BASE_ADDRESS+6;
     battery_slots[3].adc_addr = ADC_TARGET_BASE_ADDRESS+6;
-#endif
 
 
     for(uint8_t i=0; i< NUM_SLOTS; i++){
     for(uint8_t i=0; i< NUM_SLOTS; i++){
 
 
@@ -144,22 +142,12 @@ static void batteryslots_read_state(uint8_t slot) {
 
 
 static void batteryslots_adjust_current(uint8_t slot) {
 static void batteryslots_adjust_current(uint8_t slot) {
 
 
-#ifdef DEBUG_TRACE_CTRL
-    printf("Ctrl: Checking to adjust battery current. Set current: %d mA, measured current: %d mA\n",
-        battery_slots[slot].set_current,
-        battery_slots[slot].measurement.current
-        );
-#endif
-
     if (battery_slots[slot].set_current > 0) {
     if (battery_slots[slot].set_current > 0) {
         // positive current -> charge (with DAC)
         // positive current -> charge (with DAC)
 
 
         if (battery_slots[slot].pwm_value != 0) {
         if (battery_slots[slot].pwm_value != 0) {
             // seems like we switched from a charging before
             // seems like we switched from a charging before
             // -> disable DAC before getting active
             // -> disable DAC before getting active
-#ifdef DEBUG_CTRL
-            printf("Detected active pwm. Value: %d. Disabling.", battery_slots[slot].pwm_value);
-#endif
             set_pwm(slot, 0);
             set_pwm(slot, 0);
         }
         }
 
 
@@ -168,16 +156,10 @@ static void batteryslots_adjust_current(uint8_t slot) {
             // exceeded to the upper limit
             // exceeded to the upper limit
             // -> update dac value, decrease the voltage
             // -> update dac value, decrease the voltage
             if (battery_slots[slot].dac_value-1 >= 0) {
             if (battery_slots[slot].dac_value-1 >= 0) {
-#ifdef DEBUG_TRACE_CTRL
-                printf("Ctrl: Decreasing DAC to %d\n", battery_slots[slot].dac_value-1);
-#endif
                 set_dac(slot, --battery_slots[slot].dac_value);
                 set_dac(slot, --battery_slots[slot].dac_value);
             }
             }
             else {
             else {
                 // we want to give more current, but we can't ?!
                 // we want to give more current, but we can't ?!
-#ifdef DEBUG_CTRL
-                printf("Ctrl: Unable to decrease DAC to %d\n", battery_slots[slot].dac_value-1);
-#endif
                 *battery_slots[slot].state = SLOT_WARN_LOWER_DAC_NOT_POSSIBLE;
                 *battery_slots[slot].state = SLOT_WARN_LOWER_DAC_NOT_POSSIBLE;
             }
             }
         } else if (battery_slots[slot].set_current - BATTERY_CURRENT_THRESHOLD > battery_slots[slot].measurement.current) {
         } else if (battery_slots[slot].set_current - BATTERY_CURRENT_THRESHOLD > battery_slots[slot].measurement.current) {
@@ -185,16 +167,10 @@ static void batteryslots_adjust_current(uint8_t slot) {
             // exceeded to the upplowerer limit
             // exceeded to the upplowerer limit
             // -> update dac value, increase the voltage
             // -> update dac value, increase the voltage
             if (battery_slots[slot].dac_value+1 <= MAX_DAC_VALUE) {
             if (battery_slots[slot].dac_value+1 <= MAX_DAC_VALUE) {
-#ifdef DEBUG_TRACE_CTRL
-                printf("Ctrl: Increasing DAC to %d\n", battery_slots[slot].dac_value+1);
-#endif
                 set_dac(slot, ++battery_slots[slot].dac_value);
                 set_dac(slot, ++battery_slots[slot].dac_value);
             }
             }
             else {
             else {
                 // we want to give more current, but we can't ?!
                 // we want to give more current, but we can't ?!
-#ifdef DEBUG_CTRL
-                printf("Ctrl: Unable to increase DAC to %d\n", battery_slots[slot].dac_value+1);
-#endif
                 *battery_slots[slot].state = SLOT_WARN_HIGHER_DAC_NOT_POSSIBLE;
                 *battery_slots[slot].state = SLOT_WARN_HIGHER_DAC_NOT_POSSIBLE;
             }
             }
         }
         }
@@ -204,9 +180,6 @@ static void batteryslots_adjust_current(uint8_t slot) {
         if (battery_slots[slot].dac_value != 0) {
         if (battery_slots[slot].dac_value != 0) {
             // seems like we switched from a charging before
             // seems like we switched from a charging before
             // -> disable DAC before getting active
             // -> disable DAC before getting active
-#ifdef DEBUG_CTRL
-            printf("Detected active dac. Value: %d. Disabling.", battery_slots[slot].dac_value);
-#endif
             set_dac(slot, 0);
             set_dac(slot, 0);
         }
         }
 
 
@@ -217,16 +190,10 @@ static void batteryslots_adjust_current(uint8_t slot) {
 
 
             if (battery_slots[slot].pwm_value+1 <= MAX_PWM_CYCLE) {
             if (battery_slots[slot].pwm_value+1 <= MAX_PWM_CYCLE) {
                 // pwm is inverse to the DAC since dragging more current means more negative
                 // pwm is inverse to the DAC since dragging more current means more negative
-#ifdef DEBUG_TRACE_CTRL
-                printf("Ctrl: Increasing PWM to %d\n", battery_slots[slot].pwm_value+1);
-#endif
                 set_pwm(slot, ++battery_slots[slot].pwm_value);
                 set_pwm(slot, ++battery_slots[slot].pwm_value);
             }
             }
             else {
             else {
                 // we want to give more current, but we can't ?!
                 // we want to give more current, but we can't ?!
-#ifdef DEBUG_CTRL
-                printf("Ctrl: Unable to increase PWM to %d\n", battery_slots[slot].pwm_value+1);
-#endif
                 *battery_slots[slot].state = SLOT_WARN_HIGHER_PWM_NOT_POSSIBLE;
                 *battery_slots[slot].state = SLOT_WARN_HIGHER_PWM_NOT_POSSIBLE;
             }
             }
         } else if (battery_slots[slot].set_current - BATTERY_CURRENT_THRESHOLD > battery_slots[slot].measurement.current) {
         } else if (battery_slots[slot].set_current - BATTERY_CURRENT_THRESHOLD > battery_slots[slot].measurement.current) {
@@ -234,24 +201,15 @@ static void batteryslots_adjust_current(uint8_t slot) {
             // exceeded to the upplowerer limit
             // exceeded to the upplowerer limit
             // -> update pwm value, increase the voltage
             // -> update pwm value, increase the voltage
             if (battery_slots[slot].pwm_value-1 >= 0) {
             if (battery_slots[slot].pwm_value-1 >= 0) {
-#ifdef DEBUG_TRACE_CTRL
-                printf("Ctrl: Decreasing PWM to %d\n", battery_slots[slot].pwm_value-1);
-#endif
                 set_pwm(slot, --battery_slots[slot].pwm_value);
                 set_pwm(slot, --battery_slots[slot].pwm_value);
             }
             }
             else {
             else {
                 // we want to give more current, but we can't ?!
                 // we want to give more current, but we can't ?!
-#ifdef DEBUG_CTRL
-                printf("Ctrl: Unable to decrease PWM to %d\n", battery_slots[slot].pwm_value-1);
-#endif
                 *battery_slots[slot].state = SLOT_WARN_LOWER_PWM_NOT_POSSIBLE;
                 *battery_slots[slot].state = SLOT_WARN_LOWER_PWM_NOT_POSSIBLE;
             }
             }
         }
         }
     } else {
     } else {
         // we have 0 -> stop charging and discharging
         // we have 0 -> stop charging and discharging
-#ifdef DEBUG_CTRL
-        printf("0 current -> Disabling slot.\n");
-#endif
         batteryslots_disable(slot);
         batteryslots_disable(slot);
     }
     }
 }
 }

+ 2 - 2
src/config.h

@@ -81,8 +81,8 @@
 #define DAC_TARGET_ADDRESS 0x60
 #define DAC_TARGET_ADDRESS 0x60
 
 
 // ADC base address
 // ADC base address
-// it is assumed the slots are ADC ADC_TARGET_BASE_ADDRESS slot 0, 
-// ADC_TARGET_BASE_ADDRESS+1 slot 1, etc.
+// it is assumed the slots are ADC ADC_TARGET_BASE_ADDRESS slot 0,
+// other addresses follow the address table of the MCP3428
 #define ADC_TARGET_BASE_ADDRESS 0x68
 #define ADC_TARGET_BASE_ADDRESS 0x68
 
 
 // ADC Measurement mode:
 // ADC Measurement mode:

+ 5 - 5
src/interfaces/i2c_controller.c

@@ -11,14 +11,14 @@
 /*
 /*
 static function is for implementing Data Hiding, access to the static function
 static function is for implementing Data Hiding, access to the static function
 is restricted to the file where they are declared const keyword for
 is restricted to the file where they are declared const keyword for
-'TARGET_ADDRESS' and 'Data_length' makes the variable immutable. const uint8_t *
+'i2c_addr' and 'Data_length' makes the variable immutable. const uint8_t *
 const Data: means the pointer to the variable and the value of Data is immutable
 const Data: means the pointer to the variable and the value of Data is immutable
 */
 */
 
 
 I2CRxPackage controllerRxPackage;
 I2CRxPackage controllerRxPackage;
 I2CTxPackage controllerTxPackage;
 I2CTxPackage controllerTxPackage;
 
 
-static bool msp_i2c_write(uint8_t const TARGET_ADDRESS) {
+static bool msp_i2c_write(uint8_t const i2c_addr) {
     // Flush any stale data in TX FIFO:
     // Flush any stale data in TX FIFO:
     DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
     DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
 
 
@@ -44,7 +44,7 @@ static bool msp_i2c_write(uint8_t const TARGET_ADDRESS) {
     }
     }
 
 
     // **Start I2C Write Transaction**
     // **Start I2C Write Transaction**
-    DL_I2C_startControllerTransfer(I2C_controller_INST, TARGET_ADDRESS,
+    DL_I2C_startControllerTransfer(I2C_controller_INST, i2c_addr,
                                   DL_I2C_CONTROLLER_DIRECTION_TX, controllerTxPackage.len);
                                   DL_I2C_CONTROLLER_DIRECTION_TX, controllerTxPackage.len);
 
 
     // **Load Configuration Byte into TX FIFO**
     // **Load Configuration Byte into TX FIFO**
@@ -85,7 +85,7 @@ static bool msp_i2c_write(uint8_t const TARGET_ADDRESS) {
     return true;
     return true;
 }
 }
 
 
-static bool msp_i2c_read(uint8_t const TARGET_ADDRESS) {
+static bool msp_i2c_read(uint8_t const i2c_addr) {
 
 
   
   
     // Flush any stale data in TX FIFO:
     // Flush any stale data in TX FIFO:
@@ -99,7 +99,7 @@ static bool msp_i2c_read(uint8_t const TARGET_ADDRESS) {
         printf("Error in reading from I2C Bus: Bus is not getting ready before transmit start\n");
         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_startControllerTransfer(I2C_controller_INST, i2c_addr,
                                     DL_I2C_CONTROLLER_DIRECTION_RX, controllerRxPackage.len);
                                     DL_I2C_CONTROLLER_DIRECTION_RX, controllerRxPackage.len);
 
 
     n_cycles = 0;
     n_cycles = 0;

+ 5 - 6
src/interfaces/i2c_target.c

@@ -44,7 +44,9 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
 
 
     if (receivedCommand == CMD_GET_MEASUREMENT) {
     if (receivedCommand == CMD_GET_MEASUREMENT) {
         DL_I2C_flushTargetTXFIFO(i2c);
         DL_I2C_flushTargetTXFIFO(i2c);
-        DL_I2C_fillTargetTXFIFO(i2c, (uint8_t *)&battery_slots[slot].measurement, sizeof(BatteryMeasurement));
+        DL_I2C_fillTargetTXFIFO(i2c, (uint8_t *)&battery_slots[slot].measurement, 8);
+        //wait until all bytes are written and sent to the Controller
+        while(!(DL_I2C_isTargetTXFIFOEmpty(i2c)));
     } else if (receivedCommand == CMD_SET_CURRENT) {
     } else if (receivedCommand == CMD_SET_CURRENT) {
         // Read incoming bytes from the Controller:
         // Read incoming bytes from the Controller:
         uint8_t rx_index = 0;
         uint8_t rx_index = 0;
@@ -57,9 +59,6 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
                 rx_index++;
                 rx_index++;
             }
             }
         }
         }
-        // we expect 3 bytes:
-        // 1. <slot_id><command>
-        // 2 + 3. current (int16)
         if (rx_index != 2) {
         if (rx_index != 2) {
 #ifdef DEBUG_TARGET
 #ifdef DEBUG_TARGET
             printf("ERROR: Incomplete I2C Rx: received %d bytes\n", rx_index);
             printf("ERROR: Incomplete I2C Rx: received %d bytes\n", rx_index);
@@ -68,10 +67,10 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
             rx_index = 0;
             rx_index = 0;
             return;
             return;
         }
         }
-        battery_slots[slot].set_current = *((int16_t*)(&rx_buffer[0])); // byte 2+3 is the current
+        battery_slots[slot].set_current = *((int16_t*)(&rx_buffer[0]));
         
         
 #ifdef DEBUG_TARGET
 #ifdef DEBUG_TARGET
-        printf("Slot id: %d, Current: %" SCNd16 " (Bytes 0x%02X 0x%02X)\n", slot, battery_slots[slot].set_current, rx_buffer[0], rx_buffer[1]);
+        printf("Slot id: %d, Current: %d mA (Bytes 0x%02X 0x%02X)\n", slot, battery_slots[slot].set_current, rx_buffer[0], rx_buffer[1]);
 #endif
 #endif
         /*
         /*
         // This code is for debugging:
         // This code is for debugging:

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

@@ -60,9 +60,6 @@ uint16_t read_adc_channel(uint8_t slot, uint8_t channel) {
 
 
         case ADC_STATE_READ:
         case ADC_STATE_READ:
             adc_voltage = adc_hal.read_voltage(slot, &adc_params);
             adc_voltage = adc_hal.read_voltage(slot, &adc_params);
-#ifdef DEBUG_ADC
-            printf("[ADC] ADC reading completed. Slot %d Channel %d is %d \n", slot, channel, adc_voltage);
-#endif
             adc_state = ADC_STATE_DONE;
             adc_state = ADC_STATE_DONE;
             break;
             break;
         default:
         default:

+ 0 - 15
src/peripherals/adc/adc_hal.c

@@ -120,9 +120,6 @@ static uint8_t construct_config_byte(ADC_Params *params) {
 static bool adc_configure(uint8_t slot_id, ADC_Params *params) {
 static bool adc_configure(uint8_t slot_id, ADC_Params *params) {
     controllerTxPackage.packet[0] = construct_config_byte(params);
     controllerTxPackage.packet[0] = construct_config_byte(params);
 
 
-#ifdef DEBUG_ADC
-    printf("Config Byte: 0x%02X\n", controllerTxPackage.packet[0]);
-#endif
     // Wait for I2C Bus to be Free**
     // Wait for I2C Bus to be Free**
     uint32_t n_cycles = 0;
     uint32_t n_cycles = 0;
     while ((DL_I2C_getControllerStatus(I2C_controller_INST) &
     while ((DL_I2C_getControllerStatus(I2C_controller_INST) &
@@ -176,15 +173,10 @@ static bool adc_is_ready(uint8_t slot_id, ADC_Params *params) {
     controllerRxPackage.complete = false;
     controllerRxPackage.complete = false;
 
 
     i2c_hal.read(adc_address);
     i2c_hal.read(adc_address);
-    //i2c_hal.read(ADC_TARGET_BASE_ADDRESS + slot_id, 3);
     // Ready bit is bit 7
     // Ready bit is bit 7
     while(!controllerRxPackage.complete);
     while(!controllerRxPackage.complete);
     uint8_t config_adc_byte = controllerRxPackage.packet[2];
     uint8_t config_adc_byte = controllerRxPackage.packet[2];
     bool ready = (config_adc_byte & 0x80) == 1;
     bool ready = (config_adc_byte & 0x80) == 1;
-#ifdef DEBUG_ADC
-    printf("Bytes: 0x%02X 0x%02X 0x%02X (%d %d %d)\n", controllerRxPackage.packet[0], controllerRxPackage.packet[1],  controllerRxPackage.packet[2], controllerRxPackage.packet[0], controllerRxPackage.packet[1],  controllerRxPackage.packet[2]);
-    printf("ADC Ready:: gRxADClen: %d, gRxADCcount: %d ready? %d\n", controllerRxPackage.len, controllerRxPackage.count, ready);
-#endif
     return ready;
     return ready;
 }
 }
 
 
@@ -202,9 +194,6 @@ static int16_t read_adc_raw_data(uint8_t slot_id, ADC_Params *params) {
     uint32_t n_cycles = 0;
     uint32_t n_cycles = 0;
     while(!controllerRxPackage.complete && n_cycles++ < MAX_I2C_WAIT_RX);
     while(!controllerRxPackage.complete && n_cycles++ < MAX_I2C_WAIT_RX);
     if (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;
         return 0xffff;
     }
     }
     uint8_t msb = controllerRxPackage.packet[0];
     uint8_t msb = controllerRxPackage.packet[0];
@@ -230,10 +219,6 @@ static int16_t read_adc_raw_data(uint8_t slot_id, ADC_Params *params) {
             //printf("Error: Unknown ADC Resolution!\n");
             //printf("Error: Unknown ADC Resolution!\n");
             break;
             break;
     }
     }
-
-#ifdef DEBUG_ADC
-    printf("Raw ADC: %d, MSB: 0x%02X (%d) LSB: 0x%02X (%d) Config: 0x%02X\n", raw_adc, msb, msb, lsb, lsb, config_adc_byte);
-#endif
     return raw_adc * params->factor * 2048 / (max_adc_val/2) / params->gain;
     return raw_adc * params->factor * 2048 / (max_adc_val/2) / params->gain;
 }
 }
 
 

+ 0 - 8
src/peripherals/dac/dac.c

@@ -20,14 +20,6 @@ bool DAC_SingleWrite(uint8_t slot, uint16_t channel_value) {
     controllerTxPackage.packet[1] = (0x10) | ((channel_value >> 8) & 0x0F);
     controllerTxPackage.packet[1] = (0x10) | ((channel_value >> 8) & 0x0F);
     controllerTxPackage.packet[2] = (channel_value & 0xFF);
     controllerTxPackage.packet[2] = (channel_value & 0xFF);
 
 
-#ifdef DEBUG_DAC
-    // Log data being sent
-    printf("Sending to DAC: 0x%02X 0x%02X 0x%02X\n",
-            controllerTxPackage.packet[0],
-            controllerTxPackage.packet[1],
-            controllerTxPackage.packet[2]);
-#endif
-
     // Write data to DAC
     // Write data to DAC
     if (!i2c_hal.write(DAC_TARGET_ADDRESS)) {
     if (!i2c_hal.write(DAC_TARGET_ADDRESS)) {
 #ifdef DEBUG_DAC
 #ifdef DEBUG_DAC