Explorar el Código

Debugging parameter optimiert:
In der globalen Config sind jetzt verschiedene Defines, die verschiedene Debugging-Outputs erlauben.
Alle printfs soweit gesehen in ifdefs verschoben. Sind alle deaktiviert, dann optimiert der Linker die libc mit weg. (-> Memory-Verbesserung)

Heinrich Blatt hace 8 meses
padre
commit
b2a5b2537a

+ 3 - 2
main_target.c

@@ -10,7 +10,6 @@
 volatile bool mcu_CommandPending= false;
 
 void I2C_controller_INST_IRQHandler(void) {
-  // printf("I2C Interrupt Triggered to ADC!\n");
   switch (DL_I2C_getPendingInterrupt(I2C_controller_INST)) { 
     
     case DL_I2C_IIDX_CONTROLLER_START:
@@ -109,7 +108,9 @@ int main(void)
     while (1) {
 
         if(mcu_CommandPending){
-            printf("Step 1: Calling MCU target interrupt\n");
+#ifdef DEBUG_TARGET
+            printf("Calling MCU target action\n");
+#endif
             mcu_i2c_handle(I2C_target_INST);
             mcu_CommandPending= false;
         }

+ 45 - 6
src/battery_data/battery.c

@@ -7,12 +7,8 @@
 // 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/config.h"
 
-// Permissible charge temperature for LiIon battery is 0.0 degree Celsius to 45.0 degree Celsius
-// Correct temp_threshold yet to be analyzed
-#define TEMP_THRESHOLD  (460)
-//#define VOLTAGE_THRESHOLD ()
-// for extern -> variable definition
 BatterySlot battery_slots[NUM_SLOTS];
 
 static void set_dac(uint8_t slot, uint16_t value) {
@@ -90,12 +86,23 @@ static void batteryslots_read_state(uint8_t slot) {
 }
 
 static void batteryslots_adjust_current(uint8_t slot) {
+
+#ifdef DEBUG_TRACE_CTRL
+    printf("Ctrl: Adjusting 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) {
         // positive current -> charge (with DAC)
 
         if (battery_slots[slot].pwm_value != 0) {
             // seems like we switched from a charging before
             // -> 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);
         }
 
@@ -104,10 +111,16 @@ static void batteryslots_adjust_current(uint8_t slot) {
             // exceeded to the upper limit
             // -> update dac value, decrease the voltage
             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);
             }
             else {
                 // 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;
             }
         } else if (battery_slots[slot].set_current - BATTERY_CURRENT_THRESHOLD < battery_slots[slot].measurement.current) {
@@ -115,10 +128,16 @@ static void batteryslots_adjust_current(uint8_t slot) {
             // exceeded to the upplowerer limit
             // -> update dac value, increase the voltage
             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);
             }
             else {
                 // 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;
             }
         }
@@ -128,6 +147,9 @@ static void batteryslots_adjust_current(uint8_t slot) {
         if (battery_slots[slot].dac_value != 0) {
             // seems like we switched from a charging before
             // -> 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);
         }
 
@@ -137,14 +159,22 @@ static void batteryslots_adjust_current(uint8_t slot) {
             // -> update pwm value, decrease the voltage
             
             // @todo debugging & validation: ensure that this directive works
+#ifdef DEBUG_CTRL
             printf("timer count: %d\n", DL_Timer_getTimerCount(battery_slots[0].timer));
+#endif
 
             if (battery_slots[slot].pwm_value+1 <= DL_Timer_getTimerCount(battery_slots[0].timer)) {
                 // 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);
             }
             else {
                 // 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;
             }
         } else if (battery_slots[slot].set_current - BATTERY_CURRENT_THRESHOLD < battery_slots[slot].measurement.current) {
@@ -152,15 +182,24 @@ static void batteryslots_adjust_current(uint8_t slot) {
             // exceeded to the upplowerer limit
             // -> update pwm value, increase the voltage
             if (battery_slots[slot].pwm_value-1 >= 0) {
-                set_dac(slot, --battery_slots[slot].pwm_value);
+#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);
             }
             else {
                 // 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;
             }
         }
     } else {
         // we have 0 -> stop charging and discharging
+#ifdef DEBUG_CTRL
+        printf("0 current -> Disabling slot.\n");
+#endif
         batteryslots_disable(slot);
     }
 }

+ 25 - 1
src/config.h

@@ -21,6 +21,31 @@
 // small for production
 #define MAINLOOP_DELAY (32000000*5)
 
+//------------
+// Section for configuring debugging outputs
+//------------
+
+// printf ADC outputs
+#define DEBUG_ADC 1
+
+// printf DAC outputs
+#define DEBUG_DAC 1
+
+// printf control loop outputs
+#define DEBUG_CTRL 1
+
+// printf trace: put also the transition messages
+#define DEBUG_TRACE_CTRL 1
+
+// printf i2c errors
+#define DEBUG_I2C_ERR 1
+
+// printf i2c traffic (tx)
+#define DEBUG_I2C_TX 1
+
+// printf target i2c interrupts (where the mcu is the i2c target)
+#define DEBUG_TARGET 1
+
 //------------
 // Section for configuring error tresholds
 //------------
@@ -45,7 +70,6 @@
 // ADC_TARGET_BASE_ADDRESS+1 slot 1, etc.
 #define ADC_TARGET_BASE_ADDRESS 0x68
 
-
 // Packet buffer sizes for RX and TX for the
 // controller mode only
 // (target for the other MCU is treated differently)

+ 61 - 55
src/interfaces/i2c_controller.c

@@ -19,55 +19,59 @@ I2CRxPackage controllerRxPackage;
 I2CTxPackage controllerTxPackage;
 
 static bool msp_i2c_write(uint8_t const TARGET_ADDRESS) {
-  // Flush any stale data in TX FIFO:
-  DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
+    // Flush any stale data in TX FIFO:
+    DL_I2C_flushControllerTXFIFO(I2C_controller_INST);
 
-  // **Check if the I2C bus is stuck before WRITE
-  if (DL_I2C_getControllerStatus(I2C_controller_INST) &
-      DL_I2C_CONTROLLER_STATUS_ERROR) {
-    printf("I2C Communication: Bus is stuck!\n");
-    DL_I2C_resetControllerTransfer(I2C_controller_INST); 
-    return false; 
-  }
-  
-  // **Wait for I2C Bus to be Free**
-  while (DL_I2C_getControllerStatus(I2C_controller_INST) &
-         DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
-    ;
-
-  // **Start I2C Write Transaction**
-  DL_I2C_startControllerTransfer(I2C_controller_INST, TARGET_ADDRESS,
-                                 DL_I2C_CONTROLLER_DIRECTION_TX, controllerTxPackage.len);
+    // **Check if the I2C bus is stuck before WRITE
+    if (DL_I2C_getControllerStatus(I2C_controller_INST) &
+        DL_I2C_CONTROLLER_STATUS_ERROR) {
+#ifdef DEBUG_I2C_ERR
+        printf("I2C Communication: Bus is stuck!\n");
+#endif
+        DL_I2C_resetControllerTransfer(I2C_controller_INST); 
+        return false; 
+    }
+    
+    // **Wait for I2C Bus to be Free**
+    while (DL_I2C_getControllerStatus(I2C_controller_INST) &
+          DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
+      ;
 
-  // **Load Configuration Byte into TX FIFO**
-  DL_I2C_fillControllerTXFIFO(I2C_controller_INST, controllerTxPackage.packet, controllerTxPackage.len);
-  for (uint8_t i = 0; i < controllerTxPackage.len; i++) {
-      printf("Sending 0x%02X\n", controllerTxPackage.packet[i]);
-  }
+    // **Start I2C Write Transaction**
+    DL_I2C_startControllerTransfer(I2C_controller_INST, TARGET_ADDRESS,
+                                  DL_I2C_CONTROLLER_DIRECTION_TX, controllerTxPackage.len);
 
-  // ** Wait for the I2C Bus to be FREE **
-  while (DL_I2C_getControllerStatus(I2C_controller_INST) &
-         DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
-    ;
-  
-  // **Check if the target address is incorrect
-  
-  if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ADDR_ACK) {
-    printf("I2C Write Error: Target Address not acknowledged!\n");
-    return false;
-  }
-  
-  // Debug for I2C WRITE:
-  //printf("HAL Write: Address=0x%02X, Data Length=%d\n", TARGET_ADDRESS, Data_length);
+    // **Load Configuration Byte into TX FIFO**
+    DL_I2C_fillControllerTXFIFO(I2C_controller_INST, controllerTxPackage.packet, controllerTxPackage.len);
+#ifdef DEBUG_I2C_TX
+    for (uint8_t i = 0; i < controllerTxPackage.len; i++) {
+        printf("Sending 0x%02X\n", controllerTxPackage.packet[i]);
+    }
+#endif
 
+    // ** Wait for the I2C Bus to be FREE **
+    while (DL_I2C_getControllerStatus(I2C_controller_INST) &
+          DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
+      ;
+    
+    // **Check if the target address is incorrect
+    
+    if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ADDR_ACK) {
+#ifdef DEBUG_I2C_ERR
+        printf("I2C Write Error: Target Address not acknowledged!\n");
+#endif
+      return false;
+    }
 
-  // **Check for any WRITE error
-  if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR) {
-    printf("I2C Write Error: Bus error after sending data.\n");
-    return false;
-  }
+    // **Check for any WRITE error
+    if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR) {
+#ifdef DEBUG_I2C_ERR
+        printf("I2C Write Error: Bus error after sending data.\n");
+#endif
+        return false;
+    }
 
-  return true;
+    return true;
 }
 
 static bool msp_i2c_read(uint8_t const TARGET_ADDRESS) {
@@ -97,18 +101,20 @@ static bool msp_i2c_read(uint8_t const TARGET_ADDRESS) {
  * to ensure all relevant bus participants are present
  */
  bool i2c_discover(uint8_t i2c_addr) {
-  if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) {
-      DL_I2C_disableController(I2C_controller_INST);  // Disable I2C
-      DL_I2C_enableController(I2C_controller_INST);   // Re-enable I2C
-  }
-  printf("Scanning 0x%02X\n", i2c_addr);
-  DL_I2C_startControllerTransfer(I2C_controller_INST, i2c_addr, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
-  delay_cycles(5000);
-  uint32_t i2c_status = DL_I2C_getControllerStatus(I2C_controller_INST);
-  bool found = !(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR);
-  DL_I2C_disableController(I2C_controller_INST);
-  DL_I2C_enableController(I2C_controller_INST);
-  return found;
+    if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) {
+        DL_I2C_disableController(I2C_controller_INST);  // Disable I2C
+        DL_I2C_enableController(I2C_controller_INST);   // Re-enable I2C
+    }
+#ifdef DEBUG_I2C_TX
+    printf("Scanning 0x%02X\n", i2c_addr);
+#endif
+    DL_I2C_startControllerTransfer(I2C_controller_INST, i2c_addr, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
+    delay_cycles(5000);
+    uint32_t i2c_status = DL_I2C_getControllerStatus(I2C_controller_INST);
+    bool found = !(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR);
+    DL_I2C_disableController(I2C_controller_INST);
+    DL_I2C_enableController(I2C_controller_INST);
+    return found;
 }
 
 I2C_Interface i2c_hal = {

+ 12 - 1
src/interfaces/i2c_target.c

@@ -17,9 +17,10 @@ https://stackoverflow.com/questions/246127/why-is-volatile-needed-in-c
 // 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);
+#ifdef DEBUG_TARGET
     printf("[SLAVE] Received Command: 0x%02X\n", receivedCommand);
+#endif
     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
@@ -38,7 +39,9 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
         // &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));
+#ifdef DEBUG_TARGET
         printf("Battery Measurement Sent to MCU. \n");
+#endif
         DL_I2C_flushTargetTXFIFO(i2c);
   
     } else if (receivedCommand == CMD_SET_CURRENT) {
@@ -57,14 +60,18 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
         // 2. slot_id
         // 3 + 4. current (int16)
         if (rx_index != 4) {
+#ifdef DEBUG_TARGET
             printf("ERROR: Incomplete I2C Rx: received %d%zu bytes\n", rx_index, 4);
+#endif
             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
+#ifdef DEBUG_TARGET
         printf("Slot id: %d, Current: %" SCNd16 "\n", slot, battery_slots[slot].set_current);
+#endif
         if (battery_slots[slot].set_current >= 0) {
             DAC_SingleWrite(slot, battery_slots[slot].set_current);
         } else if (battery_slots[slot].set_current < 0) {
@@ -74,7 +81,11 @@ void mcu_i2c_handle(I2C_Regs *i2c) {
 
         } else {
             // do nothing, charge or discharge
+#ifdef DEBUG_TARGET
             printf("state is idle");
+#else
+            ;
+#endif
         }
     } else if (receivedCommand == CMD_CELAR_ERR) {
         uint8_t slot = receivedCommand & 0xF0;

+ 8 - 4
src/peripherals/adc/adc.c

@@ -4,6 +4,7 @@
 #include <stdio.h>
 #include "src/peripherals/adc/adc_interface.h"
 #include "src/interfaces/i2c_controller.h"
+#include "src/config.h"
 
 
 //static ADC_Params adc_params;
@@ -38,10 +39,11 @@ void updateADCReading(uint8_t slot, uint8_t channel) {
 
                 int16_t raw_adc_voltage = adc_hal.read_raw(slot, &adc_params);
                 battery_slots[slot].measurement.voltage =
-                    adc_hal.convert_voltage(raw_adc_voltage, &adc_params); 
+                    adc_hal.convert_voltage(raw_adc_voltage, &adc_params);
                 adc_state = ADC_STATE_DONE;
+#ifdef DEBUG_ADC
                 printf("[ADC] Battery Voltage in slot %d is %d mV.\n", slot, battery_slots[slot].measurement.voltage);
-                //printf("voltage: Memory address of batteries: %p\n", &batteries[0].voltage);
+#endif
 
             } else if (channel == 1) {
 
@@ -49,16 +51,18 @@ void updateADCReading(uint8_t slot, uint8_t channel) {
                 battery_slots[slot].measurement.current =
                     adc_hal.convert_current(raw_adc_current, &adc_params);
                 adc_state = ADC_STATE_DONE;
+#ifdef DEBUG_ADC
                 printf("[ADC] Battery Current in slot %d is %d mA.\n", slot, battery_slots[slot].measurement.current);
-                //printf("current: Memory address of batteries: %p\n", &batteries[0]);
+#endif
             } else if (channel == 2) {
                 // @fixme: this is the third adc channel, needed for current meausrement on disharge mode
                 int16_t raw_adc_voltage = adc_hal.read_raw(slot, &adc_params);
                 battery_slots[slot].measurement.voltage =
                     adc_hal.convert_voltage(raw_adc_voltage, &adc_params); 
                 adc_state = ADC_STATE_DONE;
+#ifdef DEBUG_ADC
                 printf("[ADC] Ch3 Voltage in slot %d is %d mV.\n", slot, battery_slots[slot].measurement.voltage);
-                //printf("voltage: Memory address of batteries: %p\n", &batteries[0].voltage);
+#endif
 
             }
             break;

+ 98 - 86
src/peripherals/adc/adc_hal.c

@@ -54,6 +54,7 @@ D15.
 #include "ti_msp_dl_config.h"
 #include <stdio.h>
 #include "src/battery_data/battery.h"
+#include "src/config.h"
 
 /*
 * Creating Configuartion Register as mentioned in the datasheet: https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/22226a.pdf
@@ -110,27 +111,32 @@ static uint8_t construct_config_byte(ADC_Params *params) {
  * I2C*/
 
 static bool adc_configure(uint8_t slot_id, ADC_Params *params) {
-  controllerTxPackage.packet[0] = construct_config_byte(params);
-  printf("Config Byte: 0x%02X\n", controllerTxPackage.packet[0]);
-  // Wait for I2C Bus to be Free**
-  while (DL_I2C_getControllerStatus(I2C_controller_INST) &
-         DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
-    ;
-  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
-    printf("[ADC] Unable to send config bytes\n");
-    *battery_slots[slot_id].state = SLOT_ERR_CONFIGBYTE;
-    return false;
-  } 
-  // Prepare TX Buffer
-  controllerTxPackage.len = 1;
-  controllerTxPackage.count = 0;
-  controllerTxPackage.complete = false;
-  i2c_hal.write(ADC_TARGET_BASE_ADDRESS);
-  
-  while(!controllerTxPackage.complete);
-  return true;
+    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**
+    while (DL_I2C_getControllerStatus(I2C_controller_INST) &
+          DL_I2C_CONTROLLER_STATUS_BUSY_BUS)
+        ;
+    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
+#ifdef DEBUG_ADC
+        printf("[ADC] Unable to send config bytes\n");
+#endif
+        *battery_slots[slot_id].state = SLOT_ERR_CONFIGBYTE;
+        return false;
+    } 
+    // Prepare TX Buffer
+    controllerTxPackage.len = 1;
+    controllerTxPackage.count = 0;
+    controllerTxPackage.complete = false;
+    i2c_hal.write(ADC_TARGET_BASE_ADDRESS);
+    
+    while(!controllerTxPackage.complete);
+    return true;
 }
 
 
@@ -146,86 +152,92 @@ 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;
-  controllerRxPackage.len = 3;
-  controllerRxPackage.count = 0;
-  controllerRxPackage.complete = false;
-
-  i2c_hal.read(adc_address);
-  //i2c_hal.read(ADC_TARGET_BASE_ADDRESS + slot_id, 3);
-  // Ready bit is bit 7
-  while(!controllerRxPackage.complete);
-  uint8_t config_adc_byte = controllerRxPackage.packet[2];
-  bool ready = (config_adc_byte & 0x80) == 1;
-  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);
-  return ready;
+    uint8_t adc_address = ADC_TARGET_BASE_ADDRESS + slot_id;
+    controllerRxPackage.len = 3;
+    controllerRxPackage.count = 0;
+    controllerRxPackage.complete = false;
+
+    i2c_hal.read(adc_address);
+    //i2c_hal.read(ADC_TARGET_BASE_ADDRESS + slot_id, 3);
+    // Ready bit is bit 7
+    while(!controllerRxPackage.complete);
+    uint8_t config_adc_byte = controllerRxPackage.packet[2];
+    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;
 }
 
 
 static int16_t read_adc_raw_data(uint8_t slot_id, ADC_Params *params) {
 
-  // Buffer for ADC data (MSB, LSB, Config Byte)
-  int16_t raw_adc = 0;
+    // Buffer for ADC data (MSB, LSB, Config Byte)
+    int16_t raw_adc = 0;
 
-  controllerRxPackage.len = 3;
-  controllerRxPackage.count = 0;
-  controllerRxPackage.complete = false;
-  
-  i2c_hal.read(ADC_TARGET_BASE_ADDRESS + slot_id);
-  while(!controllerRxPackage.complete);
-  printf("ADC Read:: gRxADClen: %d, gRxADCcount: %d\n", controllerRxPackage.len, controllerRxPackage.count);
-  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]);
-  uint8_t msb = controllerRxPackage.packet[0];
-  uint8_t lsb = controllerRxPackage.packet[1];
-  uint8_t config_adc_byte = controllerRxPackage.packet[2];
-
-  if (params->resolution == 12) {
-    raw_adc = ((msb & 0b00001111) << 8) | lsb;
-    if (raw_adc > 2047)
-      raw_adc -= 4096;
-  }
-  printf("MSB: 0x%02X (%d)\n", msb, msb);
-  printf("LSB: 0x%02X (%d)\n", lsb, lsb);
-  printf("Config Byte response: 0x%02X \n", config_adc_byte);
-  return raw_adc;
+    controllerRxPackage.len = 3;
+    controllerRxPackage.count = 0;
+    controllerRxPackage.complete = false;
+    
+    i2c_hal.read(ADC_TARGET_BASE_ADDRESS + slot_id);
+    while(!controllerRxPackage.complete);
+#ifdef DEBUG_ADC
+    printf("ADC Read:: gRxADClen: %d, gRxADCcount: %d\n", controllerRxPackage.len, controllerRxPackage.count);
+    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]);
+#endif
+    uint8_t msb = controllerRxPackage.packet[0];
+    uint8_t lsb = controllerRxPackage.packet[1];
+    uint8_t config_adc_byte = controllerRxPackage.packet[2];
+
+    if (params->resolution == 12) {
+      raw_adc = ((msb & 0b00001111) << 8) | lsb;
+      if (raw_adc > 2047)
+        raw_adc -= 4096;
+    }
+#ifdef DEBUG_ADC
+    printf("MSB: 0x%02X (%d)\n", msb, msb);
+    printf("LSB: 0x%02X (%d)\n", lsb, lsb);
+    printf("Config Byte response: 0x%02X \n", config_adc_byte);
+#endif
+    return raw_adc;
 }
 
 /* Function to Convert ADC Reading to Voltage */
 static uint16_t adc_voltage(int16_t adc_value, ADC_Params *params) {
-  uint16_t measured_voltage = 0;
-  uint16_t LSB = 0;
-  uint32_t max_adc_value = 1;
+    uint16_t measured_voltage = 0;
+    uint16_t LSB = 0;
+    uint32_t max_adc_value = 1;
 
-  switch (params->resolution) {
-  case 12: // 12-bit
-    max_adc_value = 4095;
-    break;
-  case 14: // 14-bit
-    max_adc_value = 16383;
-    break;
-  case 16: // 16-bit
-    max_adc_value = 65535;
-    break;
-  default:
-    //printf("Error: Unknown ADC Resolution!\n");
-    return 0;
-  }
-  measured_voltage = (((uint32_t)adc_value) * 2.7);
-  //printf("Measured ADC voltage: %d\n", measured_voltage);
-  return (uint16_t)measured_voltage;
+    switch (params->resolution) {
+    case 12: // 12-bit
+        max_adc_value = 4095;
+        break;
+    case 14: // 14-bit
+        max_adc_value = 16383;
+        break;
+    case 16: // 16-bit
+        max_adc_value = 65535;
+        break;
+    default:
+        //printf("Error: Unknown ADC Resolution!\n");
+        return 0;
+    }
+    measured_voltage = (((uint32_t)adc_value) * 2.7);
+    //printf("Measured ADC voltage: %d\n", measured_voltage);
+    return (uint16_t)measured_voltage;
 }
 
 /* Function to Convert ADC Reading to Voltage */
 uint16_t adc_current(int16_t adc_value, ADC_Params *params) {
-  int16_t current_mA = 0;
-  // Convert ADC value to voltage across shunt resistor:
-  uint16_t voltage_mV = adc_voltage(adc_value, params);
-  uint8_t gain_multiplier = (1 << (params->gain - 1));
-  // Convert voltage drop across shunt resistor to current
-  current_mA = (adc_value) * (10 * gain_multiplier);
-  //printf("[ADC] Battery current is %u mA.\n", current_mA);
-  return (int16_t)current_mA;
+    int16_t current_mA = 0;
+    // Convert ADC value to voltage across shunt resistor:
+    uint16_t voltage_mV = adc_voltage(adc_value, params);
+    uint8_t gain_multiplier = (1 << (params->gain - 1));
+    // Convert voltage drop across shunt resistor to current
+    current_mA = (adc_value) * (10 * gain_multiplier);
+    //printf("[ADC] Battery current is %u mA.\n", current_mA);
+    return (int16_t)current_mA;
 }
 
 

+ 26 - 23
src/peripherals/dac/dac.c

@@ -25,37 +25,40 @@ void DAC_UpdateOutput() {
     ;
 
   DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &general_call_command, 1);
-
-  //printf("DAC Outputs Updated via General Call Software Update!\n");
 }
 
 
 bool DAC_SingleWrite(uint8_t slot, uint16_t channel_value) {
-  if(channel_value > MAX_DAC_VALUE){
-    printf("DAC Error: channel_value out of range. Must be between 0 and %d\n", MAX_DAC_VALUE);
-    *battery_slots[slot].state = SLOT_WARN_DAC_INVALID_VALUE;
-    return false;
-  }
-  controllerTxPackage.len = 3;
-  controllerTxPackage.packet[0] = 0x58; //0x58 for Channel 0; 0x5A for Channel 1
-  controllerTxPackage.packet[1] = (0x10) | ((channel_value >> 8) & 0x0F);
-  controllerTxPackage.packet[2] = (channel_value & 0xFF);
-
-
-  // Log data being sent
-  printf("Sending to DAC: 0x%02X 0x%02X 0x%02X\n",
-           controllerTxPackage.packet[0],
-           controllerTxPackage.packet[1],
-           controllerTxPackage.packet[2]);
-
-  // Write data to DAC
-  if (!i2c_hal.write(DAC_TARGET_ADDRESS)) {
+    if(channel_value > MAX_DAC_VALUE) {
+#ifdef DEBUG_DAC
+        printf("DAC Error: channel_value out of range. Must be between 0 and %d\n", MAX_DAC_VALUE);
+#endif
+        *battery_slots[slot].state = SLOT_WARN_DAC_INVALID_VALUE;
+        return false;
+    }
+    controllerTxPackage.len = 3;
+    controllerTxPackage.packet[0] = 0x58; //0x58 for Channel 0; 0x5A for Channel 1
+    controllerTxPackage.packet[1] = (0x10) | ((channel_value >> 8) & 0x0F);
+    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
+    if (!i2c_hal.write(DAC_TARGET_ADDRESS)) {
+#ifdef DEBUG_DAC
         printf("I2C DAC Write Error: Failed to write to DAC.\n");
+#endif
         *battery_slots[slot].state = SLOT_ERR_DAC_WRITE_FAILED;
         return false;
     }
 
-  DAC_UpdateOutput();
+    DAC_UpdateOutput();
 
-  return true;
+    return true;
 }