浏览代码

multiplexer scan address added|adc reading channel 1 and 2 logic update

namrota ghosh 9 月之前
父节点
当前提交
b0acdaa3cd
共有 39 个文件被更改,包括 1409 次插入1540 次删除
  1. 二进制
      Debug/.clangd/.cache/clangd/index/adc.c.C783CB560A28832A.idx
  2. 二进制
      Debug/.clangd/.cache/clangd/index/adc.h.4254346EB7A93BD8.idx
  3. 二进制
      Debug/.clangd/.cache/clangd/index/battery.c.900D8F73FDE164C8.idx
  4. 二进制
      Debug/.clangd/.cache/clangd/index/battery.h.DD363DA997E60BB9.idx
  5. 二进制
      Debug/.clangd/.cache/clangd/index/cc_cv_charging.c.CE42AEEF5957AA2B.idx
  6. 二进制
      Debug/.clangd/.cache/clangd/index/dac.c.91713AFCB5D40D6E.idx
  7. 二进制
      Debug/.clangd/.cache/clangd/index/dac.h.D233D18DB68C8427.idx
  8. 二进制
      Debug/.clangd/.cache/clangd/index/i2c_controller.c.D084D0AF1C0F98F4.idx
  9. 二进制
      Debug/.clangd/.cache/clangd/index/i2c_target.c.81C6743683C30173.idx
  10. 二进制
      Debug/.clangd/.cache/clangd/index/i2c_target.h.4BFCCCED038EBB00.idx
  11. 二进制
      Debug/.clangd/.cache/clangd/index/multiplexer.c.3B7B273E7C0D0C2C.idx
  12. 二进制
      Debug/.clangd/.cache/clangd/index/multiplexer.h.0A1757C0295533D6.idx
  13. 二进制
      Debug/.clangd/.cache/clangd/index/ti_msp_dl_config.c.CD2F9AD1B4C264FA.idx
  14. 二进制
      Debug/.clangd/.cache/clangd/index/ti_msp_dl_config.h.6C46FDF13D1C9C02.idx
  15. 二进制
      Debug/adc.o
  16. 二进制
      Debug/battery.o
  17. 二进制
      Debug/cc_cv_charging.o
  18. 556 575
      Debug/charge_controller_v7.map
  19. 二进制
      Debug/charge_controller_v7.out
  20. 664 760
      Debug/charge_controller_v7_linkInfo.xml
  21. 二进制
      Debug/dac.o
  22. 二进制
      Debug/i2c_controller.o
  23. 二进制
      Debug/i2c_target.o
  24. 二进制
      Debug/multiplexer.o
  25. 10 2
      Debug/ti_msp_dl_config.c
  26. 6 0
      Debug/ti_msp_dl_config.h
  27. 二进制
      Debug/ti_msp_dl_config.o
  28. 63 25
      adc.c
  29. 2 1
      adc.h
  30. 0 5
      battery.c
  31. 21 41
      cc_cv_charging.c
  32. 2 1
      dac.c
  33. 1 0
      dac.h
  34. 18 91
      i2c_controller.c
  35. 6 0
      i2c_controller.syscfg
  36. 8 36
      i2c_target.c
  37. 0 1
      i2c_target.h
  38. 50 2
      multiplexer.c
  39. 2 0
      multiplexer.h

二进制
Debug/.clangd/.cache/clangd/index/adc.c.C783CB560A28832A.idx


二进制
Debug/.clangd/.cache/clangd/index/adc.h.4254346EB7A93BD8.idx


二进制
Debug/.clangd/.cache/clangd/index/battery.c.900D8F73FDE164C8.idx


二进制
Debug/.clangd/.cache/clangd/index/battery.h.DD363DA997E60BB9.idx


二进制
Debug/.clangd/.cache/clangd/index/cc_cv_charging.c.CE42AEEF5957AA2B.idx


二进制
Debug/.clangd/.cache/clangd/index/dac.c.91713AFCB5D40D6E.idx


二进制
Debug/.clangd/.cache/clangd/index/dac.h.D233D18DB68C8427.idx


二进制
Debug/.clangd/.cache/clangd/index/i2c_controller.c.D084D0AF1C0F98F4.idx


二进制
Debug/.clangd/.cache/clangd/index/i2c_target.c.81C6743683C30173.idx


二进制
Debug/.clangd/.cache/clangd/index/i2c_target.h.4BFCCCED038EBB00.idx


二进制
Debug/.clangd/.cache/clangd/index/multiplexer.c.3B7B273E7C0D0C2C.idx


二进制
Debug/.clangd/.cache/clangd/index/multiplexer.h.0A1757C0295533D6.idx


二进制
Debug/.clangd/.cache/clangd/index/ti_msp_dl_config.c.CD2F9AD1B4C264FA.idx


二进制
Debug/.clangd/.cache/clangd/index/ti_msp_dl_config.h.6C46FDF13D1C9C02.idx


二进制
Debug/adc.o


二进制
Debug/battery.o


二进制
Debug/cc_cv_charging.o


文件差异内容过多而无法显示
+ 556 - 575
Debug/charge_controller_v7.map


二进制
Debug/charge_controller_v7.out


文件差异内容过多而无法显示
+ 664 - 760
Debug/charge_controller_v7_linkInfo.xml


二进制
Debug/dac.o


二进制
Debug/i2c_controller.o


二进制
Debug/i2c_target.o


二进制
Debug/multiplexer.o


+ 10 - 2
Debug/ti_msp_dl_config.c

@@ -99,12 +99,20 @@ SYSCONFIG_WEAK void SYSCFG_DL_GPIO_init(void)
 
     DL_GPIO_initDigitalOutput(GPIO_Battery_Charging_PIN_PB0_IOMUX);
 
+    DL_GPIO_initDigitalOutput(GPIO_Battery_Charging_PIN_PB4_IOMUX);
+
     DL_GPIO_initDigitalOutput(GPIO_Battery_Discharging_PIN_PB8_IOMUX);
 
+    DL_GPIO_initDigitalOutput(GPIO_Battery_Discharging_PIN_PB12_IOMUX);
+
     DL_GPIO_clearPins(GPIOB, GPIO_Battery_Charging_PIN_PB0_PIN |
-		GPIO_Battery_Discharging_PIN_PB8_PIN);
+		GPIO_Battery_Charging_PIN_PB4_PIN |
+		GPIO_Battery_Discharging_PIN_PB8_PIN |
+		GPIO_Battery_Discharging_PIN_PB12_PIN);
     DL_GPIO_enableOutput(GPIOB, GPIO_Battery_Charging_PIN_PB0_PIN |
-		GPIO_Battery_Discharging_PIN_PB8_PIN);
+		GPIO_Battery_Charging_PIN_PB4_PIN |
+		GPIO_Battery_Discharging_PIN_PB8_PIN |
+		GPIO_Battery_Discharging_PIN_PB12_PIN);
 
 }
 

+ 6 - 0
Debug/ti_msp_dl_config.h

@@ -115,12 +115,18 @@ extern "C" {
 /* Defines for PIN_PB0: GPIOB.0 with pinCMx 12 on package pin 47 */
 #define GPIO_Battery_Charging_PIN_PB0_PIN                        (DL_GPIO_PIN_0)
 #define GPIO_Battery_Charging_PIN_PB0_IOMUX                      (IOMUX_PINCM12)
+/* Defines for PIN_PB4: GPIOB.4 with pinCMx 17 on package pin 52 */
+#define GPIO_Battery_Charging_PIN_PB4_PIN                        (DL_GPIO_PIN_4)
+#define GPIO_Battery_Charging_PIN_PB4_IOMUX                      (IOMUX_PINCM17)
 /* Port definition for Pin Group GPIO_Battery_Discharging */
 #define GPIO_Battery_Discharging_PORT                                    (GPIOB)
 
 /* Defines for PIN_PB8: GPIOB.8 with pinCMx 25 on package pin 60 */
 #define GPIO_Battery_Discharging_PIN_PB8_PIN                     (DL_GPIO_PIN_8)
 #define GPIO_Battery_Discharging_PIN_PB8_IOMUX                   (IOMUX_PINCM25)
+/* Defines for PIN_PB12: GPIOB.12 with pinCMx 29 on package pin 64 */
+#define GPIO_Battery_Discharging_PIN_PB12_PIN                   (DL_GPIO_PIN_12)
+#define GPIO_Battery_Discharging_PIN_PB12_IOMUX                  (IOMUX_PINCM29)
 
 
 /* Defines for CRC */

二进制
Debug/ti_msp_dl_config.o


+ 63 - 25
adc.c

@@ -14,15 +14,13 @@ uint8_t gRxPacket[I2C_RX_MAX_PACKET_SIZE];
 uint32_t gTxADClen, gTxADCcount;
 uint32_t gRxADClen, gRxADCcount;
 
-
 uint8_t ADC_ConstructConfigBytes(ADC_PARAMS params) {
 
     uint8_t config = 0;
 
-    
     config |= ((params.channel - 1) << 5);  // Channel Selection (Bits 6-5)
 
-    if (params.continuous) config |= (1 << 4);  // Continuous Mode (Bit 4)
+    config |= (1 << 4);  // One-Shot Mode
 
     switch (params.resolution) {
         case 12: config |= (0b00 << 2); break;
@@ -47,8 +45,6 @@ void ADC_SetConfigurationBytes(ADC_PARAMS params) {
     // **Construct Configuration Byte**
     uint8_t config_byte = ADC_ConstructConfigBytes(params);
 
-    //printf("Writing Config: 0x%02X to ADC (0x%X)...\n", config_byte, ADC_TARGET_BASE_ADDRESS);
-
     // Wait for I2C Bus to be Free**
     while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
 
@@ -57,15 +53,22 @@ void ADC_SetConfigurationBytes(ADC_PARAMS params) {
     gTxPacket[0]= config_byte;
     gTxADClen= 1;
     gTxADCcount= 0;
-    gTxComplete= true;
+    gTxComplete= false;
+
     DL_I2C_startControllerTransfer(I2C_controller_INST, ADC_TARGET_BASE_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, gTxADClen);
     DL_I2C_enableInterrupt(I2C_controller_INST, DL_I2C_INTERRUPT_CONTROLLER_TXFIFO_TRIGGER);
-    while(!gTxComplete);
+    //while(!gTxComplete);
     // **Ensure STOP Condition is Sent**
     while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
     printf("Configuration Sent Successfully for 0x%X!\n", config_byte);
 }
 
+// **Interrupt handler for configuration completion**
+void I2C_ConfigInterruptHandler(void) {
+    if (DL_I2C_getPendingInterrupt(I2C_controller_INST) == DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER) {
+        gTxComplete = true;
+    }
+}
 /*
 READY BIT:
     This bit is the data ready flag. In read mode, this bit indicates if the output register has been updated
@@ -81,7 +84,7 @@ bool ADC_CheckReadyBit(uint8_t slot_id, ADC_PARAMS params){
     uint8_t config_byte = ADC_ConstructConfigBytes(params);
     DL_I2C_startControllerTransfer(I2C_controller_INST, ADC_TARGET_BASE_ADDRESS+slot_id, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
     config_byte= DL_I2C_receiveControllerData(I2C_controller_INST);
-    bool ready= (config_byte & 0x80)==0;
+    bool ready= (config_byte & 0x80)==0; //Ready bit is bit 7
     printf("Slot: %d | Config Byte: 0x%02X | READY Bit: %d\n", slot_id, config_byte, ready);
     return ready;
 }
@@ -117,22 +120,27 @@ Function to read ADC DATA: This function simply retrieves the ADC raw digital ou
 * Output code is in binary and is proportional to the Input Voltage and PGA settings.
 * From the datasheet: https://ww1.microchip.com/downloads/aemDocuments/documents/OTH/ProductDocuments/DataSheets/22226a.pdf
     - Equation 4.4 is being used to convert the output codes to input voltage
+
+* The ACK bits after conversion is issued by the Master, when the device receives a READ command, it outputs two data bytes
+    followed by a configuration register in 16 bit conversion mode the MSB(=sign bit) of the first data type is D15.
+    
 */
 
 int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params)
 {
+    //Request ADC Conversion
+    //ADC_SetConfigurationBytes(params);
+
+    //Wait until data is ready
     while(!ADC_CheckReadyBit(slot_id, params)){
         delay_cycles(50000);
     }
-    uint8_t adc_data[3] = {0};  // Buffer for ADC data (MSB, LSB, Config Byte)
+    // Buffer for ADC data (MSB, LSB, Config Byte)
+    uint8_t adc_data[3] = {0};  
     int16_t raw_adc = 0;
-    /*The ACK bits after conversion is issued by the Master, when the device receives a READ command, it outputs two data bytes
-    followed by a configuration register
-    in 16 bit conversion mode the MSB(=sign bit) of the first data type is D15.
-    */
     uint8_t bytes_to_read= 3;
     uint8_t adc_address= ADC_TARGET_BASE_ADDRESS + slot_id;
-    //printf("Reading ADC Data from MCP3428 for channel: %u\n", params.channel);
+    printf("Reading ADC Data from MCP3428 for channel: %u\n", params.channel);
     
     if(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS){
         printf("Error: I2C bus stuck! Resetting..\n");
@@ -146,11 +154,7 @@ int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params)
     DL_I2C_startControllerTransfer(I2C_controller_INST, adc_address, DL_I2C_CONTROLLER_DIRECTION_RX, gRxADClen);
     DL_I2C_enableInterrupt(I2C_controller_INST, DL_I2C_INTERRUPT_CONTROLLER_RXFIFO_TRIGGER);
     while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
-    delay_cycles(100000);
     while(!gRxComplete);
-        
-    
-    while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
     
     uint8_t msb= gRxPacket[0];
     uint8_t lsb= gRxPacket[1];
@@ -179,6 +183,13 @@ int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params)
    
 }
 
+// **Interrupt handler for ADC Read Completion**
+void I2C_ADC_IRQHandler(void) {
+    if (DL_I2C_getPendingInterrupt(I2C_controller_INST) == DL_I2C_IIDX_CONTROLLER_RXFIFO_TRIGGER) {
+        gRxComplete = true;
+    }
+}
+
 
 /* Function to Convert ADC Reading to Voltage */
 uint16_t ADC_ConvertToVoltage(int16_t adc_value, ADC_PARAMS params) {
@@ -201,12 +212,6 @@ uint16_t ADC_ConvertToVoltage(int16_t adc_value, ADC_PARAMS params) {
             return 0;
         }
     measured_voltage= (((uint32_t)adc_value * ADC_VREF_MV) * 3)/max_adc_value;
-
-    
-    /*Formula for ADC to Voltage Conversion*/
-    //voltage = (adc_value / max_adc_value)* VREF/gain;
-   
-    //printf("Converted Voltage: %d mV\n", measured_voltage);
     return (uint16_t)measured_voltage;
 }
 
@@ -216,8 +221,9 @@ uint16_t ADC_ConvertToCurrent(int16_t adc_value, ADC_PARAMS params) {
     //uint8_t r_shunt= 0.1;
     //Convert ADC value to voltage across shunt resistor:
     uint16_t voltage_mV= ADC_ConvertToVoltage(adc_value, params);
+    uint8_t gain_multiplier= (1 <<(params.gain - 1));
     //Convert voltage drop across shunt resistor to current
-    current_mA= (voltage_mV)/ 0.1;
+    current_mA= (voltage_mV)/ (0.1 * gain_multiplier);
     //printf("Converted Current: %d mA.\n", current_mA);
     return (int16_t)current_mA;
 }
@@ -225,6 +231,7 @@ uint16_t ADC_ConvertToCurrent(int16_t adc_value, ADC_PARAMS params) {
 void Battery_UpdateVoltage(ADC_PARAMS params){
     for(uint8_t slot=0; slot< NUM_SLOTS; slot++ ){
         //CH1: Voltage Setup
+        params.channel= 1;
         int16_t raw_adc_voltage= ADC_ReadData(slot, params);
         batteries[slot].voltage= ADC_ConvertToVoltage(raw_adc_voltage, params);   
         printf("Battery Voltage for slot %d is %u mV.\n", slot, batteries[slot].voltage);
@@ -235,9 +242,40 @@ void Battery_UpdateVoltage(ADC_PARAMS params){
 void Battery_UpdateCurrent(ADC_PARAMS params){
     for(uint8_t slot=0; slot< NUM_SLOTS; slot++ ){
         //CH2: Charge Current
+        params.channel= 2;
         int16_t raw_adc_current= ADC_ReadData(slot, params);
         batteries[slot].current= ADC_ConvertToCurrent(raw_adc_current, params);
         printf("Battery current for slot %d is %u mA.\n", slot, batteries[slot].current);
         }
 
 }
+
+void Battery_UpdateADCReading(){
+    ADC_PARAMS adc_params;
+
+    // For Channel 1: Voltage
+    adc_params.channel= 1;
+    adc_params.resolution= 12;
+    adc_params.continuous= 0;
+    adc_params.gain= 1;
+    ADC_SetConfigurationBytes(adc_params);
+    delay_cycles(50000);
+    for(uint8_t slot=0; slot< NUM_SLOTS; slot++ ){
+        int16_t raw_adc_voltage= ADC_ReadData(slot, adc_params);
+        batteries[slot].voltage= ADC_ConvertToVoltage(raw_adc_voltage, adc_params);   
+        printf("Battery Voltage for slot %d is %u mV.\n", slot, batteries[slot].voltage);
+        }
+
+    //For Channel 2: Current
+    adc_params.channel= 2;
+    ADC_SetConfigurationBytes(adc_params);
+    delay_cycles(50000);
+
+    for(uint8_t slot=0; slot< NUM_SLOTS; slot++ ){
+        int16_t raw_adc_current= ADC_ReadData(slot, adc_params);
+        batteries[slot].current= ADC_ConvertToCurrent(raw_adc_current, adc_params);
+        printf("Battery current for slot %d is %u mA.\n", slot, batteries[slot].current);
+        }
+
+}
+    

+ 2 - 1
adc.h

@@ -5,7 +5,7 @@
 
 #define ADC_TARGET_BASE_ADDRESS (0x68)
 #define ADC_VREF_MV (2048)
-#define I2C_TIMEOUT 100000 //define timeout limit
+#define DELAY (100000) //define timeout limit
 
 //Maximum packet sizes
 #define I2C_TX_MAX_PACKET_SIZE (16)
@@ -34,5 +34,6 @@ bool ADC_CheckReadyBit(uint8_t slot_id, ADC_PARAMS params);
 int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params);
 void Battery_UpdateVoltage(ADC_PARAMS params);
 void Battery_UpdateCurrent(ADC_PARAMS params);
+void Battery_UpdateADCReading();
 
 #endif

+ 0 - 5
battery.c

@@ -9,11 +9,6 @@ Battery batteries[NUM_SLOTS];
 
 uint16_t charge_change_threshold;
 
-// for testing:
-Battery batteries[NUM_SLOTS] = {
-    {0, STATE_BATTERY_DETECTED, 3700, 500, 25, 3000, 4200, 2000, 10000, 80}
-};
-
 /*Initialize battery array and default parameters*/
 void Battery_Init(){
     for(uint8_t i=0; i< NUM_SLOTS; i++){

+ 21 - 41
cc_cv_charging.c

@@ -11,12 +11,14 @@
 #define MIN_VOLTAGE_MV (3000)
 #define CC_CURRENT_LIMIT_MA (500)
 #define CUTOFF_CURRENT_MA (50)
-#define MAX_CYCLES (500)
+#define MAX_CYCLES (2)
 
+
+//********** Not used **************************
 // if the battery is deeply discharged then 50mA of trickle charge is given for a set timer
-#define TRICKLE_CHARGE_CURRENT_MA (50)
-#define TRICKLE_CHARGE_VOLTAGE_MV (2100)
-#define TRICKLE_CHARGE_TIMEOUT_MS (5000) //5 mseconds
+//#define TRICKLE_CHARGE_CURRENT_MA (50)
+//#define TRICKLE_CHARGE_VOLTAGE_MV (2100)
+//#define TRICKLE_CHARGE_TIMEOUT_MS (5000) //5 mseconds
 
 //Pre Charge
 #define BATTERY_CAPACITY_MAH (2000)
@@ -31,20 +33,8 @@ static uint32_t pre_charge_start_time= 0;
 void CC_CV_UpdateChargingState(uint8_t slot_id){
     uint16_t batt_voltage= batteries[slot_id].voltage;
     int16_t batt_current= batteries[slot_id].current;
-    static uint16_t trickle_timer= 0;
-
-    if(batt_voltage < TRICKLE_CHARGE_VOLTAGE_MV){
-        charging_state= STATE_TRICKLE_CHARGE;
-        trickle_timer= 0;
-    }
-    else if(charging_state== STATE_TRICKLE_CHARGE){
-        trickle_timer ++;
 
-        if(trickle_timer >= TRICKLE_CHARGE_TIMEOUT_MS){
-            charging_state = STATE_ERROR;
-        }
-    }
-    else if(batt_voltage < MIN_VOLTAGE_MV){
+    if(batt_voltage < MIN_VOLTAGE_MV){
         charging_state= STATE_PRE_CHARGE;
     }
 
@@ -87,57 +77,47 @@ Constant Voltage: CV volatge starts once the maximum voltage threshold is obtain
 */
 
 void CC_CV_ControlCharging(uint8_t slot_id){
-    
+    //Calling function to get all the conditional states
     CC_CV_UpdateChargingState(slot_id);
     uint16_t batt_voltage= batteries[slot_id].voltage;
     int16_t batt_current= batteries[slot_id].current;
     
     switch(charging_state){
-        case STATE_TRICKLE_CHARGE:
-            DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB0_PIN);
-            //GPIO_setConfig(GPIO_Battery_Charging_PIN_PB0_PIN , GPIO_CFG_OUT_STD| GPIO_CFG_OUT_HIGH | GPIO_Battery_Charging_PIN_PB0_IOMUX);
-            DAC_fastWrite(TRICKLE_CHARGE_CURRENT_MA);
-            printf("[CC-CV] Trickle Charging: Slot %d at %d mA.\n", slot_id, TRICKLE_CHARGE_CURRENT_MA);
-            break;
         
         case STATE_PRE_CHARGE:
-            DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB0_PIN);
-            //GPIO_setConfig(GPIO_Battery_Charging_PIN_PB0_PIN , GPIO_CFG_OUT_STD| GPIO_CFG_OUT_HIGH | GPIO_Battery_Charging_PIN_PB0_IOMUX);
-            DAC_fastWrite(PRE_CHARGE_CURRENT_MA);
+            DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
+            DAC_fastWrite(CHANNEL_A_VALUE);
             printf("[CC-CV] Pre Charging: Slot %d at %d mA.\n", slot_id, PRE_CHARGE_CURRENT_MA);
             break;
         
         case STATE_CC_CHARGING:
-            DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB0_PIN);
-            //GPIO_setConfig(GPIO_Battery_Charging_PIN_PB0_PIN , GPIO_CFG_OUT_STD | GPIO_CFG_OUT_HIGH | GPIO_Battery_Charging_PIN_PB0_IOMUX);
-            DAC_fastWrite(CC_CURRENT_LIMIT_MA);
+            DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
+            DAC_fastWrite(CHANNEL_A_VALUE);
             printf("[CC-CV] CC Charging: Slot %d at %d mA.\n", slot_id, CC_CURRENT_LIMIT_MA);
             break;
         
         case STATE_CV_CHARGING:
-            DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB0_PIN);
-            //GPIO_setConfig(GPIO_Battery_Charging_PIN_PB0_PIN , GPIO_CFG_OUT_STD | GPIO_CFG_OUT_HIGH | GPIO_Battery_Charging_PIN_PB0_IOMUX);
-            DAC_fastWrite(CUTOFF_CURRENT_MA);
+            DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
+            DAC_fastWrite(CHANNEL_A_VALUE);
             printf("[CC-CV] CV Charging: Slot %d at %d mA.\n", slot_id, CUTOFF_CURRENT_MA);
             break;
         
         case STATE_DISCHARGING:
-            DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB0_PIN);
-            DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Discharging_PIN_PB8_PIN);
-            //GPIO_setConfig(GPIO_Battery_Charging_PIN_PB0_PIN , GPIO_CFG_OUT_STD | GPIO_CFG_OUT_HIGH | GPIO_Battery_Discharging_PIN_PB8_IOMUX);
-            DAC_fastWrite(batt_voltage);
+            DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
+            DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB12_PIN);
+            DAC_fastWrite(CHANNEL_A_VALUE);
             printf("[CC-CV] Discharging: Slot %d at %d mA.\n", slot_id, batt_voltage);
             break;
 
         case STATE_FINAL_DISCHARGE:
-            DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB0_PIN);
-            DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB8_PIN);
+            DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
+            DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB12_PIN);
             printf("[CC-CV] Final Discharge: Slot %d..\n", slot_id);
             DAC_fastWrite(0);
         
         case STATE_ERROR:
-            DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB0_PIN);
-            DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB8_PIN);
+            DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB4_PIN);
+            DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB12_PIN);
             printf("[CC-CV] Error: Slot %d.\n", slot_id);
 
         default:

+ 2 - 1
dac.c

@@ -26,12 +26,12 @@ bool DAC_fastWrite(uint16_t channel_a_value){
     /*DAC has a 12 bit resolution that ranges from 0 to 4095.
     0x00: sets the Power Mode to NORMAL for Channel A
     (channel_a_value >> 8): shifts the value to 8 places right and gives upper 4 bits
+    VoutA channel, rest channels are ll powered down
     */
 
     uint8_t output_buffer[8];
 
     output_buffer[0]= (0x00)|(channel_a_value >> 8)&0x0F;
-    //output_buffer[0] = channel_a_value >> 8;
     output_buffer[1]= channel_a_value & 0xFF;
 
     output_buffer[2]= 0x40; // Power down for the other channels
@@ -45,6 +45,7 @@ bool DAC_fastWrite(uint16_t channel_a_value){
 
     if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR) {
         printf("I2C Write Error: Failed to write to DAC channels for FAST write mode!\n");
+        DL_I2C_resetControllerTransfer(I2C_controller_INST); // Reset bus if stuck
         return false;  // Return failure if there was an error
     }
 

+ 1 - 0
dac.h

@@ -4,6 +4,7 @@
 #include <stdbool.h>
 
 #define DAC_TARGET_BASE_ADDRESS (0x60)
+#define CHANNEL_A_VALUE (800) // in mAmps
 
 bool DAC_fastWrite(uint16_t channel_a_value);
 void DAC_UpdateOutput();

+ 18 - 91
i2c_controller.c

@@ -1,28 +1,6 @@
 /*
-
- 20.02.2025:
- Flowchart of the code:
- 1. MCU requests ADC data over I2C
- 2. ADC is connected to the MULTIPLEXER.
- 3. Interrupt triggers when data is available.
- 4. Interrupt moves RECEIVED bytes into RXFIFO Buffer
- 5. Interrupt marks reception complete RX_DONE
- 6. Read ADC_Data and retrieve the final ADc value
- 7. Convert ADC value to Voltage
-
- 24.02.2025: 
- Working Code for Reading Analog Signals from ADC to MCU through Multiplexer
-    - configured the registers with Channel, Resolution, Mode(Continuous or Single-mode) and Gain: Construct_Config_Byte()
-    - SET configuration registers to ADC: SetConfiguration()
-    - Read the Analog Output: Read_ADC_Data()
-    - Convert Analog output to voltage in Volts: Convert_ADC_To_Voltage()
-    - Commented out interrupts for the time being.
-    - Adding code for Pi to READ ADC voltages in mV.
-
- 27.02.2025: 
- DAC function to write to Channel A in Fast Mode and return the Analog output
- */
-
+Main Source File
+*/
 #include "i2c_target.h"
 #include "ti/devices/msp/peripherals/hw_dac12.h"
 #include "ti/driverlib/dl_adc12.h"
@@ -46,33 +24,7 @@
 I2C_Instance gI2C;
 I2C_ResponseInfo gResponse;
 BatteryData battery_data;
-/*
-Scans all the addresses of the peripherals:
-*/
-void I2C_scanBus() {
-    printf("Scanning I2C Bus...\n");
-
-    // **Step 1: Reset I2C Controller if Busy**
-    if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) {
-        printf("I2C Bus Busy! Resetting I2C Controller...\n");
-        DL_I2C_disableController(I2C_controller_INST);  // Disable I2C
-        delay_cycles(20000);
-        DL_I2C_enableController(I2C_controller_INST);   // Re-enable I2C
-        delay_cycles(20000);
-    }
 
-    // **Step 2: Scan I2C Bus**
-    for (uint8_t addr = 0x08; addr < 0x78; addr++) {  // Valid I2C Address Range
-        DL_I2C_startControllerTransfer(I2C_controller_INST, addr, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
-        delay_cycles(5000);
-        
-        if (!(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR)) {
-            printf("Device found at: 0x%02X\n", addr);
-        }
-    }
-
-    printf("I2C Scan Complete!\n");
-}
 
 /*Interrupt for MCU -> ADC
 * CASE: DL_I2C_IIDX_CONTROLLER_RX_DONE: ADC Reception Complete
@@ -349,23 +301,6 @@ void I2C_target_INST_IRQHandler(void) {
     }
 }
 
-
-void Reset_I2C_Bus() {
-    printf("I2C Bus is stuck! Resetting...\n");
-    
-    // Disable I2C Controller
-    DL_I2C_disableController(I2C_controller_INST);
-    delay_cycles(50000);
-
-    // Re-enable I2C Controller
-    DL_I2C_enableController(I2C_controller_INST);
-    delay_cycles(50000);
-
-    // Check if bus is free now
-    uint32_t status = DL_I2C_getControllerStatus(I2C_controller_INST);
-    printf("I2C Bus Status After Reset: 0x%08X\n", status);
-}
-
 /********MAIN function*************/
 
 int main(void)
@@ -384,11 +319,11 @@ int main(void)
     
     //Multiplexer
     Multiplexer_SelectChannel(I2C_CHANNEL);
-    //I2C_scanBus();
+    I2C_scanBus();
     I2C_init(&gI2C);
 
     // *Configure ADC for voltage: Channel 1 and Channel 2*
-    ADC_PARAMS adc_voltage_params={
+    /*ADC_PARAMS adc_voltage_params={
         .channel= 1,
         .resolution= 12,
         .continuous= 1,
@@ -400,36 +335,28 @@ int main(void)
         .resolution= 12,
         .continuous= 1,
         .gain= 1
-    };
+    };*/
     
-    uint16_t channel_a_value= 3300; // in mVolts
-    ADC_SetConfigurationBytes(adc_voltage_params); 
-    delay_cycles(50000);
-    ADC_SetConfigurationBytes(adc_current_params);
-    delay_cycles(50000);
-
-    //GPIO_setConfig(GPIO_Battery_Charging_PIN_PB0_PIN , GPIO_CFG_OUT_STD| GPIO_CFG_OUT_HIGH| GPIO_Battery_Charging_PIN_PB0_IOMUX);
-    //GPIO_setConfig(GPIO_Battery_Discharging_PIN_PB8_PIN, GPIO_CFG_OUT_STD| GPIO_CFG_OUT_HIGH| GPIO_Battery_Discharging_PIN_PB8_IOMUX);
+    
+    //ADC_SetConfigurationBytes(adc_voltage_params); 
+    //delay_cycles(50000);
+    //ADC_SetConfigurationBytes(adc_current_params);
+    //delay_cycles(50000);
 
     while (1)
     {   //Looping through the ADC Channels
-        delay_cycles(50000);
-
-        Battery_UpdateVoltage(adc_voltage_params);
+        Battery_UpdateADCReading();
         delay_cycles(100000);
           
-        Battery_UpdateCurrent(adc_current_params);
-        delay_cycles(50000);
-   
-        //DAC_fastWrite(channel_a_value);
+        //Battery_UpdateCurrent(adc_current_params);
         //delay_cycles(50000);
 
-        //CC-CV Cycle
-        for(uint8_t slot_id= 0; slot_id < NUM_SLOTS; slot_id++){
-            CC_CV_ControlCharging(slot_id);
-        }
-        
-        
+        //CC-CV Cycle: maximum cycles is not yet implemented
+        //for(uint8_t slot_id= 0; slot_id < NUM_SLOTS; slot_id++){
+        //    CC_CV_ControlCharging(slot_id);
+        //}  
+
+        //DAC_fastWrite(CHANNEL_A_VALUE);   
 
     }   
 

+ 6 - 0
i2c_controller.syscfg

@@ -27,13 +27,19 @@ CRC.seed       = 0xFFFF;
 
 GPIO1.$name                         = "GPIO_Battery_Charging";
 GPIO1.port                          = "PORTB";
+GPIO1.associatedPins.create(2);
 GPIO1.associatedPins[0].$name       = "PIN_PB0";
 GPIO1.associatedPins[0].pin.$assign = "PB0";
+GPIO1.associatedPins[1].$name       = "PIN_PB4";
+GPIO1.associatedPins[1].pin.$assign = "PB4";
 
 GPIO2.$name                         = "GPIO_Battery_Discharging";
 GPIO2.port                          = "PORTB";
+GPIO2.associatedPins.create(2);
 GPIO2.associatedPins[0].$name       = "PIN_PB8";
 GPIO2.associatedPins[0].pin.$assign = "PB8";
+GPIO2.associatedPins[1].$name       = "PIN_PB12";
+GPIO2.associatedPins[1].pin.$assign = "PB12";
 
 I2C1.basicEnableController             = true;
 I2C1.advControllerTXFIFOTRIG           = "BYTES_1";

+ 8 - 36
i2c_target.c

@@ -29,6 +29,13 @@ uint32_t piRxLen, piRxCount;
 
 */
 
+
+// for testing:
+//Battery batteries[NUM_SLOTS] = {
+//    {0, STATE_BATTERY_DETECTED, 3700, 500, 25, 3000, 4200, 2000, 10000, 80}
+//};
+
+
 void Battery_ReadState(uint8_t slot_id){
     
     uint16_t voltage_mv= batteries[slot_id].voltage;
@@ -55,39 +62,4 @@ void Battery_ReadState(uint8_t slot_id){
     }    
 }
 
-
-
-/*
-     {
-                if(receivedCommand == CMD_SET_BATTERY_LIMIT){
-                    BatteryLimitMsg battery_limits;
-                    //requestedSlot= DL_I2C_receiveTargetData(I2C_target_INST);
-                    for(uint8_t slot_id=0; slot_id< NUM_SLOTS; slot++){
-                        
-
-                    }
-                    
-                    if(requestedSlot < NUM_SLOTS){
-                        batteries[requestedSlot].min_voltage= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
-                        batteries[requestedSlot].max_voltage= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
-                        batteries[requestedSlot].cut_off_current= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
-                        batteries[requestedSlot].capacitance= (DL_I2C_receiveTargetData(I2C_target_INST)<< 8)|DL_I2C_receiveTargetData(I2C_target_INST);
-                    }
-
-
-                }
-            }
-            break;
-
-
-
-                Battery_StateUpdate();
-                uint8_t battery_slots_status[NUM_SLOTS];
-                for(uint8_t slot= 0; slot< NUM_SLOTS; slot++){
-                    battery_slots_status[slot]= batteries[slot].state;
-                }
-                while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY); 
-                DL_I2C_fillTargetTXFIFO(I2C_target_INST, battery_slots_status, NUM_SLOTS);
-                while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY); 
-*/
-        
+ 

+ 0 - 1
i2c_target.h

@@ -43,6 +43,5 @@ typedef struct{
 } BatteryLimitMsg;
 
 //void I2C_Init();
-//void ProcessI2Ccommand();
 void Battery_ReadState(uint8_t slot_id);
 #endif

+ 50 - 2
multiplexer.c

@@ -25,6 +25,38 @@ The TCA9548A is example of a single-register device, which is controlled via I2C
 there is only 1 register needed, and the controller merely writes the register data after the target address, skipping the register number.
 */
 
+/*
+Scans all the addresses of the peripherals:
+*/
+void I2C_scanBus() {
+    printf("Scanning I2C Bus...\n");
+
+    // **Step 1: Reset I2C Controller if Busy**
+    if (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS) {
+        printf("I2C Bus Busy! Resetting I2C Controller...\n");
+        DL_I2C_disableController(I2C_controller_INST);  // Disable I2C
+        delay_cycles(20000);
+        DL_I2C_enableController(I2C_controller_INST);   // Re-enable I2C
+        delay_cycles(20000);
+    }
+
+    // **Step 2: Scan I2C Bus**
+    for (uint8_t addr = 0x08; addr < 0x78; addr++) {  // Valid I2C Address Range
+        DL_I2C_startControllerTransfer(I2C_controller_INST, addr, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
+        delay_cycles(5000);
+        
+        if (!(DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_ERROR)) {
+            printf("Device found at: 0x%02X\n", addr);
+        }else {
+            // Clear the error by resetting the I2C controller
+            DL_I2C_disableController(I2C_controller_INST);
+            DL_I2C_enableController(I2C_controller_INST);
+        }
+    }
+
+    printf("I2C Scan Complete!\n");
+}
+
 /*Function for Multiplexer*/
 
 void Multiplexer_SelectChannel(uint8_t channel)
@@ -34,7 +66,7 @@ void Multiplexer_SelectChannel(uint8_t channel)
         return;
     }
     uint8_t data = (channel);
-    //printf("Selecting Multiplexer Channel: 0x%02X\n", data);
+    printf("Selecting Multiplexer I2C Channel: 0x%02X\n", data);
 
     // Ensure bus is idle before starting communication
     while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
@@ -81,4 +113,20 @@ void Multiplexer_SelectChannel(uint8_t channel)
     //    printf("Multiplexer Active Channel: 0x%X\n", data);
     //    return;
     //}
-}
+}
+
+void Reset_I2C_Bus() {
+    printf("I2C Bus is stuck! Resetting...\n");
+    
+    // Disable I2C Controller
+    DL_I2C_disableController(I2C_controller_INST);
+    delay_cycles(50000);
+
+    // Re-enable I2C Controller
+    DL_I2C_enableController(I2C_controller_INST);
+    delay_cycles(50000);
+
+    // Check if bus is free now
+    uint32_t status = DL_I2C_getControllerStatus(I2C_controller_INST);
+    printf("I2C Bus Status After Reset: 0x%08X\n", status);
+}

+ 2 - 0
multiplexer.h

@@ -4,7 +4,9 @@
 #include <stdint.h>
 
 #define MULTIPLEXER_I2C_ADDR           (0x70)
+//For Channel 0 Hex Value is 0x01
 #define I2C_CHANNEL                    (0x01)
 
 void Multiplexer_SelectChannel(uint8_t channel);
+void I2C_scanBus();
 #endif

部分文件因为文件数量过多而无法显示