Преглед изворни кода

Added Realy Pins and changed CC_CV logic

namrota ghosh пре 9 месеци
родитељ
комит
59e070c39e
41 измењених фајлова са 1826 додато и 1184 уклоњено
  1. BIN
      Debug/.clangd/.cache/clangd/index/adc.c.C783CB560A28832A.idx
  2. BIN
      Debug/.clangd/.cache/clangd/index/adc.h.4254346EB7A93BD8.idx
  3. BIN
      Debug/.clangd/.cache/clangd/index/battery.c.900D8F73FDE164C8.idx
  4. BIN
      Debug/.clangd/.cache/clangd/index/battery.h.DD363DA997E60BB9.idx
  5. BIN
      Debug/.clangd/.cache/clangd/index/cc_cv_charging.c.CE42AEEF5957AA2B.idx
  6. BIN
      Debug/.clangd/.cache/clangd/index/cc_cv_charging.h.3E17593078666ADB.idx
  7. BIN
      Debug/.clangd/.cache/clangd/index/dac.c.91713AFCB5D40D6E.idx
  8. BIN
      Debug/.clangd/.cache/clangd/index/i2c_controller.c.D084D0AF1C0F98F4.idx
  9. BIN
      Debug/.clangd/.cache/clangd/index/i2c_target.c.81C6743683C30173.idx
  10. BIN
      Debug/.clangd/.cache/clangd/index/i2c_target.h.4BFCCCED038EBB00.idx
  11. BIN
      Debug/.clangd/.cache/clangd/index/multiplexer.c.3B7B273E7C0D0C2C.idx
  12. BIN
      Debug/.clangd/.cache/clangd/index/ti_msp_dl_config.c.CD2F9AD1B4C264FA.idx
  13. BIN
      Debug/.clangd/.cache/clangd/index/ti_msp_dl_config.h.6C46FDF13D1C9C02.idx
  14. 30 0
      Debug/Event.dot
  15. BIN
      Debug/adc.o
  16. BIN
      Debug/battery.o
  17. BIN
      Debug/cc_cv_charging.o
  18. 582 496
      Debug/charge_controller_v7.map
  19. BIN
      Debug/charge_controller_v7.out
  20. 978 564
      Debug/charge_controller_v7_linkInfo.xml
  21. BIN
      Debug/dac.o
  22. 5 1
      Debug/i2c_controller.d
  23. BIN
      Debug/i2c_controller.o
  24. BIN
      Debug/i2c_target.o
  25. BIN
      Debug/multiplexer.o
  26. 1 0
      Debug/subdir_rules.mk
  27. 4 2
      Debug/subdir_vars.mk
  28. 9 0
      Debug/ti_msp_dl_config.c
  29. 16 0
      Debug/ti_msp_dl_config.h
  30. BIN
      Debug/ti_msp_dl_config.o
  31. 48 20
      adc.c
  32. 5 2
      adc.h
  33. 7 0
      battery.c
  34. 1 1
      battery.h
  35. 52 2
      cc_cv_charging.c
  36. 0 3
      dac.c
  37. 73 82
      i2c_controller.c
  38. 13 0
      i2c_controller.syscfg
  39. 0 9
      i2c_target.c
  40. 1 1
      i2c_target.h
  41. 1 1
      multiplexer.c

BIN
Debug/.clangd/.cache/clangd/index/adc.c.C783CB560A28832A.idx


BIN
Debug/.clangd/.cache/clangd/index/adc.h.4254346EB7A93BD8.idx


BIN
Debug/.clangd/.cache/clangd/index/battery.c.900D8F73FDE164C8.idx


BIN
Debug/.clangd/.cache/clangd/index/battery.h.DD363DA997E60BB9.idx


BIN
Debug/.clangd/.cache/clangd/index/cc_cv_charging.c.CE42AEEF5957AA2B.idx


BIN
Debug/.clangd/.cache/clangd/index/cc_cv_charging.h.3E17593078666ADB.idx


BIN
Debug/.clangd/.cache/clangd/index/dac.c.91713AFCB5D40D6E.idx


BIN
Debug/.clangd/.cache/clangd/index/i2c_controller.c.D084D0AF1C0F98F4.idx


BIN
Debug/.clangd/.cache/clangd/index/i2c_target.c.81C6743683C30173.idx


BIN
Debug/.clangd/.cache/clangd/index/i2c_target.h.4BFCCCED038EBB00.idx


BIN
Debug/.clangd/.cache/clangd/index/multiplexer.c.3B7B273E7C0D0C2C.idx


BIN
Debug/.clangd/.cache/clangd/index/ti_msp_dl_config.c.CD2F9AD1B4C264FA.idx


BIN
Debug/.clangd/.cache/clangd/index/ti_msp_dl_config.h.6C46FDF13D1C9C02.idx


+ 30 - 0
Debug/Event.dot

@@ -0,0 +1,30 @@
+
+
+digraph H {
+
+  parent [
+   shape=plaintext
+   label=<
+     <table border='1' cellborder='1'>
+       <tr>
+            <td bgcolor="grey" port='chan_1'>Channel 1</td>
+            <td bgcolor="grey" port='chan_2'>Channel 2</td>
+            <td bgcolor="grey" port='chan_3'>Channel 3</td>
+            <td bgcolor="grey" port='chan_4'>Channel 4</td>
+            <td bgcolor="grey" port='chan_5'>Channel 5</td>
+            <td bgcolor="grey" port='chan_6'>Channel 6</td>
+            <td bgcolor="grey" port='chan_7'>Channel 7</td>
+            <td bgcolor="grey" port='chan_8'>Channel 8</td>
+            <td bgcolor="grey" port='chan_9'>Channel 9</td>
+            <td bgcolor="grey" port='chan_10'>Channel 10</td>
+            <td bgcolor="grey" port='chan_11'>Channel 11</td>
+            <td bgcolor="black" port='chan_12'><font color="grey">Channel 12</font></td>
+            <td bgcolor="black" port='chan_13'><font color="grey">Channel 13</font></td>
+            <td bgcolor="black" port='chan_14'><font color="grey">Channel 14</font></td>
+            <td bgcolor="black" port='chan_15'><font color="grey">Channel 15</font></td>
+         </tr>
+     </table>
+  >];
+
+
+}



BIN
Debug/cc_cv_charging.o


Разлика између датотеке није приказан због своје велике величине
+ 582 - 496
Debug/charge_controller_v7.map


BIN
Debug/charge_controller_v7.out


Разлика између датотеке није приказан због своје велике величине
+ 978 - 564
Debug/charge_controller_v7_linkInfo.xml



+ 5 - 1
Debug/i2c_controller.d

@@ -76,7 +76,9 @@ i2c_controller.o: ../i2c_controller.c ../i2c_target.h ../battery.h \
  /home/ngosh/ti/mspm0_sdk_2_03_00_07/source/ti/driverlib/m0p/dl_interrupt.h \
  /home/ngosh/ti/mspm0_sdk_2_03_00_07/source/ti/driverlib/m0p/dl_systick.h \
  ../ti/comm_modules/i2c/controller/i2c_comm_controller.h \
- ../multiplexer.h ../adc.h ../dac.h ../cc_cv_charging.h
+ ../multiplexer.h ../adc.h ../dac.h ../cc_cv_charging.h \
+ /home/ngosh/ti/mspm0_sdk_2_03_00_07/source/ti/drivers/GPIO.h \
+ /home/ngosh/ti/mspm0_sdk_2_03_00_07/source/ti/drivers/gpio/GPIOMSPM0.h
 ../i2c_target.h:
 ../battery.h:
 /home/ngosh/ti/mspm0_sdk_2_03_00_07/source/ti/devices/msp/peripherals/hw_dac12.h:
@@ -158,3 +160,5 @@ ti_msp_dl_config.h:
 ../adc.h:
 ../dac.h:
 ../cc_cv_charging.h:
+/home/ngosh/ti/mspm0_sdk_2_03_00_07/source/ti/drivers/GPIO.h:
+/home/ngosh/ti/mspm0_sdk_2_03_00_07/source/ti/drivers/gpio/GPIOMSPM0.h:

BIN
Debug/i2c_controller.o


BIN
Debug/i2c_target.o


BIN
Debug/multiplexer.o


+ 1 - 0
Debug/subdir_rules.mk

@@ -22,6 +22,7 @@ device.opt: build-1016228703
 device.cmd.genlibs: build-1016228703
 ti_msp_dl_config.c: build-1016228703
 ti_msp_dl_config.h: build-1016228703
+Event.dot: build-1016228703
 
 %.o: ./%.c $(GEN_OPTS) | $(GEN_FILES) $(GEN_MISC_FILES)
 	@echo 'Building file: "$<"'

+ 4 - 2
Debug/subdir_vars.mk

@@ -52,7 +52,8 @@ OBJS += \
 
 GEN_MISC_FILES += \
 ./device.cmd.genlibs \
-./ti_msp_dl_config.h 
+./ti_msp_dl_config.h \
+./Event.dot 
 
 OBJS__QUOTED += \
 "adc.o" \
@@ -67,7 +68,8 @@ OBJS__QUOTED += \
 
 GEN_MISC_FILES__QUOTED += \
 "device.cmd.genlibs" \
-"ti_msp_dl_config.h" 
+"ti_msp_dl_config.h" \
+"Event.dot" 
 
 C_DEPS__QUOTED += \
 "adc.d" \

+ 9 - 0
Debug/ti_msp_dl_config.c

@@ -97,6 +97,15 @@ SYSCONFIG_WEAK void SYSCFG_DL_GPIO_init(void)
     DL_GPIO_enableHiZ(GPIO_I2C_target_IOMUX_SDA);
     DL_GPIO_enableHiZ(GPIO_I2C_target_IOMUX_SCL);
 
+    DL_GPIO_initDigitalOutput(GPIO_Battery_Charging_PIN_PB0_IOMUX);
+
+    DL_GPIO_initDigitalOutput(GPIO_Battery_Discharging_PIN_PB8_IOMUX);
+
+    DL_GPIO_clearPins(GPIOB, GPIO_Battery_Charging_PIN_PB0_PIN |
+		GPIO_Battery_Discharging_PIN_PB8_PIN);
+    DL_GPIO_enableOutput(GPIOB, GPIO_Battery_Charging_PIN_PB0_PIN |
+		GPIO_Battery_Discharging_PIN_PB8_PIN);
+
 }
 
 

+ 16 - 0
Debug/ti_msp_dl_config.h

@@ -108,6 +108,21 @@ extern "C" {
 #define GPIO_I2C_target_IOMUX_SCL_FUNC                  IOMUX_PINCM2_PF_I2C0_SCL
 
 
+
+/* Port definition for Pin Group GPIO_Battery_Charging */
+#define GPIO_Battery_Charging_PORT                                       (GPIOB)
+
+/* 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)
+/* 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 CRC */
 #define CRC_SEED                                                        (0xFFFF)
 
@@ -119,6 +134,7 @@ void SYSCFG_DL_GPIO_init(void);
 void SYSCFG_DL_SYSCTL_init(void);
 void SYSCFG_DL_I2C_controller_init(void);
 void SYSCFG_DL_I2C_target_init(void);
+
 void SYSCFG_DL_CRC_init(void);
 
 

BIN
Debug/ti_msp_dl_config.o


+ 48 - 20
adc.c

@@ -14,6 +14,7 @@ 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;
@@ -46,9 +47,7 @@ void ADC_SetConfigurationBytes(ADC_PARAMS params) {
     // **Construct Configuration Byte**
     uint8_t config_byte = ADC_ConstructConfigBytes(params);
 
-    //uint8_t adc_address= ADC_TARGET_BASE_ADDRESS + slot_id;
-
-    printf("Writing Config: 0x%02X to ADC (0x%X)...\n", config_byte, ADC_TARGET_BASE_ADDRESS);
+    //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);
@@ -67,6 +66,27 @@ void ADC_SetConfigurationBytes(ADC_PARAMS params) {
     printf("Configuration Sent Successfully for 0x%X!\n", config_byte);
 }
 
+/*
+READY BIT:
+    This bit is the data ready flag. In read mode, this bit indicates if the output register has been updated
+    with a latest conversion result. In One-Shot Conversion mode, writing this bit to “1” initiates a new
+    conversion.
+
+    1= Output Register has not been updated
+    0= Output Register has been updated
+
+*/
+
+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;
+    printf("Slot: %d | Config Byte: 0x%02X | READY Bit: %d\n", slot_id, config_byte, ready);
+    return ready;
+}
+
+
 /*
 Function to read ADC DATA: This function simply retrieves the ADC raw digital output as 16-bit signed integer. 
 * The value returned is not yet converted to VOLTAGE.
@@ -101,6 +121,9 @@ Function to read ADC DATA: This function simply retrieves the ADC raw digital ou
 
 int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params)
 {
+    while(!ADC_CheckReadyBit(slot_id, params)){
+        delay_cycles(50000);
+    }
     uint8_t adc_data[3] = {0};  // Buffer for ADC data (MSB, LSB, Config Byte)
     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
@@ -109,28 +132,29 @@ int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params)
     */
     uint8_t bytes_to_read= 3;
     uint8_t adc_address= ADC_TARGET_BASE_ADDRESS + slot_id;
-    printf("Reading ADC Data from MCP3428...\n");
-    delay_cycles(100000);
-    while (DL_I2C_getControllerStatus(I2C_controller_INST) & DL_I2C_CONTROLLER_STATUS_BUSY_BUS);
+    //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");
+        DL_I2C_resetControllerTransfer(I2C_controller_INST);
+    }
 
-    // Determine bytes to read
-    //printf("Resolution for reading ADC is %d\n", resolution);
-    // Request 3 Bytes from ADC**
-    //DL_I2C_startControllerTransfer(I2C_controller_INST, DEF_TARGET_ADDR_ADC, DL_I2C_CONTROLLER_DIRECTION_RX, bytes_to_read);
     gRxADClen= bytes_to_read;
     gRxADCcount= 0;
     gRxComplete= false;
-
+   
     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 config_adc_byte= gRxPacket[2];
+    
     uint8_t msb= gRxPacket[0];
     uint8_t lsb= gRxPacket[1];
+    uint8_t config_adc_byte= gRxPacket[2];
 
     uint8_t gain_setting = (config_adc_byte & 0x03);
     uint8_t gain_multiplier = (1 << gain_setting); // Gain values: 1, 2, 4, 8
@@ -158,7 +182,7 @@ int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params)
 
 /* Function to Convert ADC Reading to Voltage */
 uint16_t ADC_ConvertToVoltage(int16_t adc_value, ADC_PARAMS params) {
-    uint32_t measured_voltage= 0;
+    uint16_t measured_voltage= 0;
     uint16_t LSB= 0;
     uint32_t max_adc_value= 1;
 
@@ -188,27 +212,31 @@ uint16_t ADC_ConvertToVoltage(int16_t adc_value, ADC_PARAMS params) {
 
 /* Function to Convert ADC Reading to Voltage */
 uint16_t ADC_ConvertToCurrent(int16_t adc_value, ADC_PARAMS params) {
-    uint16_t current_mA= 0;
+    int16_t current_mA= 0;
     //uint8_t r_shunt= 0.1;
     //Convert ADC value to voltage across shunt resistor:
     uint16_t voltage_mV= ADC_ConvertToVoltage(adc_value, params);
     //Convert voltage drop across shunt resistor to current
     current_mA= (voltage_mV)/ 0.1;
-    printf("Converted Current: %d mA.\n", current_mA);
-    return current_mA;
+    //printf("Converted Current: %d mA.\n", current_mA);
+    return (int16_t)current_mA;
 }
 
-void Battery_UpdateCurrentVoltage(ADC_PARAMS params){
+void Battery_UpdateVoltage(ADC_PARAMS params){
     for(uint8_t slot=0; slot< NUM_SLOTS; slot++ ){
-        //Set Configuration
-        //ADC_SetConfigurationBytes(slot, params);
         //CH1: Voltage Setup
         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);
+        }
+
+}
+
+void Battery_UpdateCurrent(ADC_PARAMS params){
+    for(uint8_t slot=0; slot< NUM_SLOTS; slot++ ){
         //CH2: Charge Current
         int16_t raw_adc_current= ADC_ReadData(slot, params);
-        batteries[slot].current= raw_adc_current;
+        batteries[slot].current= ADC_ConvertToCurrent(raw_adc_current, params);
         printf("Battery current for slot %d is %u mA.\n", slot, batteries[slot].current);
         }
 

+ 5 - 2
adc.h

@@ -1,9 +1,11 @@
+
 #ifndef ADC_H_
 #include "ti/driverlib/dl_i2c.h"
 #include "ti_msp_dl_config.h"
 
 #define ADC_TARGET_BASE_ADDRESS (0x68)
 #define ADC_VREF_MV (2048)
+#define I2C_TIMEOUT 100000 //define timeout limit
 
 //Maximum packet sizes
 #define I2C_TX_MAX_PACKET_SIZE (16)
@@ -28,8 +30,9 @@ typedef struct{
 
 uint8_t ADC_ConstructConfigBytes(ADC_PARAMS params);
 void ADC_SetConfigurationBytes(ADC_PARAMS params);
+bool ADC_CheckReadyBit(uint8_t slot_id, ADC_PARAMS params);
 int16_t ADC_ReadData(uint8_t slot_id, ADC_PARAMS params);
-uint16_t ADC_ConvertToVoltage(int16_t adc_value, ADC_PARAMS params);
-void Battery_UpdateCurrentVoltage(ADC_PARAMS params);
+void Battery_UpdateVoltage(ADC_PARAMS params);
+void Battery_UpdateCurrent(ADC_PARAMS params);
 
 #endif

+ 7 - 0
battery.c

@@ -9,6 +9,11 @@ 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++){
@@ -35,3 +40,5 @@ void Battery_SafetyCheck(uint8_t slot){
         //How to exit? Disable the DAC channel?
     }
 }
+
+

+ 1 - 1
battery.h

@@ -3,7 +3,7 @@
 #include <stdint.h>
 
 //define macro to be used by multiple files in the program witout the variable being overwritten
-
+//for testing
 #define NUM_SLOTS (1)
 #define BATTERY_THRESHOLD (50)
 

+ 52 - 2
cc_cv_charging.c

@@ -2,17 +2,22 @@
 #include "battery.h"
 #include "dac.h"
 #include "adc.h"
+#include "ti/devices/msp/m0p/mspm0g350x.h"
+#include "ti/driverlib/dl_gpio.h"
 #include <stdio.h>
 
+
 #define MAX_VOLTAGE_MV (4200)
 #define MIN_VOLTAGE_MV (3000)
 #define CC_CURRENT_LIMIT_MA (500)
 #define CUTOFF_CURRENT_MA (50)
 #define MAX_CYCLES (500)
+
 // 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
+
 //Pre Charge
 #define BATTERY_CAPACITY_MAH (2000)
 #define PRE_CHARGE_CURRENT_MA (BATTERY_CAPACITY_MAH/ 10)
@@ -20,6 +25,8 @@
 static ChargingState charging_state= STATE_PRE_CHARGE;
 static uint16_t cycle_count= 0;
 static uint32_t pre_charge_start_time= 0;
+//batt_voltage and current as stored from ADC
+
 
 void CC_CV_UpdateChargingState(uint8_t slot_id){
     uint16_t batt_voltage= batteries[slot_id].voltage;
@@ -69,31 +76,70 @@ void CC_CV_UpdateChargingState(uint8_t slot_id){
     }
 }
 
+/*
+Function for Battery Charging and Discharging:
+Trickle Charge: Only used when the battery voltage < ~2.1 V. 
+    - In this state, the batteries internal protection may have been previously disconnected.
+Pre Charge: Once the battery is in discharged state, pre-charging begins.
+    - Starts charging safely with a typically low current C/10
+Constant Current: Continues until the battery voltage has reached the "full" or floating voltage level
+Constant Voltage: CV volatge starts once the maximum voltage threshold is obtained
+*/
 
 void CC_CV_ControlCharging(uint8_t slot_id){
     
     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);
             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);
             printf("[CC-CV] CC Charging: Slot %d at %d mA.\n", slot_id, CC_CURRENT_LIMIT_MA);
             break;
         
         case STATE_CV_CHARGING:
-            DAC_fastWrite(MAX_VOLTAGE_MV);
-            printf("[CC-CV] CV Charging: Slot %d at %d mA.\n", slot_id, PRE_CHARGE_CURRENT_MA);
+            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);
+            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);
+            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);
+            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);
+            printf("[CC-CV] Error: Slot %d.\n", slot_id);
+
         default:
             break;
         }
@@ -101,3 +147,7 @@ void CC_CV_ControlCharging(uint8_t slot_id){
 }
 
 
+
+
+
+

+ 0 - 3
dac.c

@@ -51,9 +51,6 @@ bool DAC_fastWrite(uint16_t channel_a_value){
     // **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, DAC_TARGET_BASE_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_TX, 8);
 

+ 73 - 82
i2c_controller.c

@@ -40,26 +40,12 @@
 #include "battery.h"
 #include "i2c_target.h"
 #include "cc_cv_charging.h"
+#include <ti/drivers/GPIO.h>
+
 
 I2C_Instance gI2C;
 I2C_ResponseInfo gResponse;
 BatteryData battery_data;
-
-/*****************/
-/*Register for the MCU*/
-#define REGISTER_SIZE 256
-uint8_t registers[REGISTER_SIZE]= {1, 2, 3, 4, 5};
-
-
-/*Counters for TX length and bytes sent*/
-uint32_t gTxLen, gTxCount;
-/* Counters for RX length and bytes sent*/
-uint32_t gRxLen, gRxCount;
-
-/* Boolean to know when a stop command was issued */
-bool gStopReceived = false;
-uint8_t gTxData[MAX_DATA_SIZE] = {0} ;
-
 /*
 Scans all the addresses of the peripherals:
 */
@@ -88,35 +74,6 @@ void I2C_scanBus() {
     printf("I2C Scan Complete!\n");
 }
 
-/*Function to get the RDY bit from ADC*/
-
-bool Check_ADC_Ready()
-{
-    uint8_t config_byte = 0;
-
-    /*
-    READY BIT:
-    This bit is the data ready flag. In read mode, this bit indicates if the output register has been updated
-    with a latest conversion result. In One-Shot Conversion mode, writing this bit to “1” initiates a new
-    conversion.
-
-    1= Output Register has not been updated
-    0= Output Register has been updated
-
-    */
-
-    DL_I2C_startControllerTransfer(I2C_controller_INST, ADC_TARGET_BASE_ADDRESS, DL_I2C_CONTROLLER_DIRECTION_RX, 1);
-    config_byte = DL_I2C_receiveControllerData(I2C_controller_INST);
-    return (config_byte & 0x80) == 0;  // Check if RDY bit is cleared in bit 7
-
-}
-
-/*
-Function to initialize MCU as target: Pi communicates as the Controller and MCU as the Target
-*/
-
-
-
 /*Interrupt for MCU -> ADC
 * CASE: DL_I2C_IIDX_CONTROLLER_RX_DONE: ADC Reception Complete
     - ADC has finished sending data and it's fully received.
@@ -166,28 +123,31 @@ void I2C_controller_INST_IRQHandler(void)
                     DL_I2C_receiveControllerData(I2C_controller_INST);
                 }
             }
-            if (gRxADCcount == gRxADClen){
+            if (gRxADCcount >= gRxADClen){
                 //printf("ADC Bytes Received!\n");
+                gRxComplete = true;
                 DL_I2C_enableInterrupt(I2C_controller_INST, DL_I2C_INTERRUPT_CONTROLLER_STOP);
-                //gRxComplete = true;
+                
             }
             break;
         /*TRANSMIT data to ADC*/
         case DL_I2C_IIDX_CONTROLLER_TXFIFO_TRIGGER:
-            printf("TX FIFO with data!\n"); 
+            //printf("TX FIFO with data!\n"); 
             gI2C.status= I2C_STATUS_TX_INPROGRESS;
             if(gTxADCcount<gTxADClen){
                 gTxADCcount+= DL_I2C_fillControllerTXFIFO(I2C_controller_INST, &gTxPacket[gTxADCcount], (gTxADClen-gTxADCcount));
             } else{
                 /*Prevent overflow and just ignore data*/
                 DL_I2C_fillTargetTXFIFO(I2C_controller_INST, (uint8_t[]){0x00}, 1);
+                gTxComplete= true;
             }
             break;
         /*STOP condition*/
         case DL_I2C_IIDX_CONTROLLER_STOP:
             gTxComplete= true;
             gRxComplete = true;
-            printf("I2C Stop Detected- RX Complete");
+            //printf("I2C Stop Detected- RX Complete");
+            break;
         case DL_I2C_IIDX_CONTROLLER_ARBITRATION_LOST:
             //printf("Interrupt index for I2C controller Arbitration Lost!\n");
             break;
@@ -224,12 +184,13 @@ void I2C_target_INST_IRQHandler(void) {
         case DL_I2C_IIDX_TARGET_STOP:
             piTxComplete= true;
             piRxComplete = true;
+            DL_I2C_flushTargetTXFIFO(I2C_target_INST);
             break;
 
         /* TX FIFO trigger (Pi is reading data from MCU) */
-        /*TXFIFO to GET battery status is triggered when command is 0x01
-        - Pi on request of 0x01 will get a response of the battery status for all the slots
-        - Battery_StateUpdate function is called, which in turn calls the Battery_ReadState funtion to set the state of the batteries
+        /* GET battery status is triggered when command is 0x01
+           - Pi on request of 0x01 will get a response of the battery status for all the slots
+           - Battery_StateUpdate function is called, which in turn calls the Battery_ReadState funtion to set the state of the batteries
         -Pi on command of [0x02, slot_id] will GET the 'Battery Data' which is voltage, current and temperature for a given slot.
             - MCU reads the slot_id from Pi using DL_I2C_receiveTargetData()
             - piTxCount is set to 0
@@ -238,23 +199,31 @@ void I2C_target_INST_IRQHandler(void) {
                 - battery pointer variable points to the memory of the requested slot
                 - the values of voltage, current and temperature are then stored in battery_data struct
             - Once the values are in BatteryData struct we wait for the bus to be free
-            - Next we send the BatteryData to Pi using DL_I2C_fillTargetTXFIFO()
-            - Reset the TX counter for the next data.
+            - Next we send the BatteryData to Pi using DL_I2C_fillTargetRXFIFO()
+            - Reset the RX counter for the next data.
         */
         case DL_I2C_IIDX_TARGET_TXFIFO_TRIGGER:
-            printf("TX to PI triggered!\n");
-            if(!DL_I2C_isTargetTXFIFOEmpty(I2C_target_INST)){
+            printf("Pi GET data from MCU!\n");
+            if(!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)){
                 receivedCommand= DL_I2C_receiveTargetData(I2C_target_INST);
                 printf("Received Command: 0x%02X\n", receivedCommand);
 
                 if (receivedCommand == CMD_GET_BATTERY_STATUS){
                     printf("Battery status received.\n");
-                    Battery_StateUpdate();
+                    //Battery_StateUpdate();
+                    piTxCount= 0;
+                    piTxLen= NUM_SLOTS;
+                    piTxComplete = false;
+                    // Prepare data to be sent to Pi:
+                    for(uint8_t slot= 0; slot < NUM_SLOTS; slot++){
+                        // Read the battery status for each slot
+                        Battery_ReadState(slot);
+                        piTxPacket[slot]= batteries[slot].state;
+                    }
+                    //Filling up the FIFO
                     if(piTxCount < piTxLen){
                         while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
-                        DL_I2C_fillTargetTXFIFO(I2C_target_INST, &piTxPacket[piTxCount], 1);
-                        piTxCount++;
-                        
+                        piTxCount += DL_I2C_fillTargetTXFIFO(I2C_target_INST, &piTxPacket[piTxCount], (piTxLen-piTxCount));     
                     }
                     else {
                     /*
@@ -263,12 +232,13 @@ void I2C_target_INST_IRQHandler(void) {
                     while (
                         DL_I2C_transmitTargetDataCheck(I2C_target_INST, 0x00) != false);
                     }
+                    piTxComplete= true;
                 }
                 else if (receivedCommand == CMD_GET_BATTERY_DATA){
 
                     uint8_t requestedSlot= DL_I2C_receiveTargetData(I2C_target_INST);
                     while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
-                    printf("Battery Data!\n");
+                    printf("Battery Data Requested for Slot %d!\n", requestedSlot);
                         piTxCount= 0;
                         piTxLen= sizeof(BatteryData);
                         BatteryData battery_data;
@@ -281,7 +251,11 @@ void I2C_target_INST_IRQHandler(void) {
 
                             while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
 
-                            piTxCount += DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t*)&battery_data, piTxLen);
+                            DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t *)&battery_data, sizeof(BatteryData));
+
+                            //piTxCount += DL_I2C_fillTargetTXFIFO(I2C_target_INST, (uint8_t*)&battery_data, piTxLen);
+
+                            piTxComplete= true;
 
                             while (DL_I2C_getTargetStatus(I2C_target_INST) & DL_I2C_TARGET_STATUS_BUS_BUSY);
 
@@ -317,7 +291,7 @@ void I2C_target_INST_IRQHandler(void) {
 
          */
         case DL_I2C_IIDX_TARGET_RXFIFO_TRIGGER:
-            printf("RX FIFO triggered from PI.\n");
+            printf("Pi SET Battery limit to MCU.....\n");
             if(!DL_I2C_isTargetRXFIFOEmpty(I2C_target_INST)){
                 receivedCommand= DL_I2C_receiveTargetData(I2C_target_INST);
                 printf("Received Command: 0x%02X\n", receivedCommand);
@@ -392,8 +366,6 @@ void Reset_I2C_Bus() {
     printf("I2C Bus Status After Reset: 0x%08X\n", status);
 }
 
-
-
 /********MAIN function*************/
 
 int main(void)
@@ -405,8 +377,8 @@ int main(void)
     Battery_Init();
     
     //Reset_I2C_Bus();
-    NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
-    //NVIC_EnableIRQ(I2C_controller_INST_INT_IRQN);
+    //NVIC_EnableIRQ(I2C_target_INST_INT_IRQN);
+    NVIC_EnableIRQ(I2C_controller_INST_INT_IRQN);
 
     printf("............System Configuration Enabled...............\n");
     
@@ -415,30 +387,49 @@ int main(void)
     //I2C_scanBus();
     I2C_init(&gI2C);
 
-    // *Configure ADC*
-    ADC_PARAMS adc_params={
+    // *Configure ADC for voltage: Channel 1 and Channel 2*
+    ADC_PARAMS adc_voltage_params={
         .channel= 1,
         .resolution= 12,
         .continuous= 1,
         .gain= 1
-
     };
-    //printf("Configuring ADC....\n");
-    //**Set Configuration Register for ADC**
-    //ADC_SetConfigurationBytes(adc_params);
-    //uint16_t channel_a_value= 3300; // in mVolts
     
+    ADC_PARAMS adc_current_params={
+        .channel= 2,
+        .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);
+
     while (1)
-    {   
-        
-        //Battery_UpdateCurrentVoltage(adc_params);
-        //for(uint8_t slot_id= 0; slot_id < NUM_SLOTS; slot_id++){
-        //    CC_CV_ControlCharging(slot_id);
-        //}
-        //delay_cycles(5000);
-        //DAC_fastWrite(channel_a_value);
+    {   //Looping through the ADC Channels
+        delay_cycles(50000);
+
+        Battery_UpdateVoltage(adc_voltage_params);
         delay_cycles(100000);
+          
+        Battery_UpdateCurrent(adc_current_params);
+        delay_cycles(50000);
+   
+        //DAC_fastWrite(channel_a_value);
+        //delay_cycles(50000);
+
+        //CC-CV Cycle
+        for(uint8_t slot_id= 0; slot_id < NUM_SLOTS; slot_id++){
+            CC_CV_ControlCharging(slot_id);
+        }
+        
+        
 
     }   
 

+ 13 - 0
i2c_controller.syscfg

@@ -11,6 +11,9 @@
  */
 const Board = scripting.addModule("/ti/driverlib/Board");
 const CRC   = scripting.addModule("/ti/driverlib/CRC");
+const GPIO  = scripting.addModule("/ti/driverlib/GPIO", {}, false);
+const GPIO1 = GPIO.addInstance();
+const GPIO2 = GPIO.addInstance();
 const I2C   = scripting.addModule("/ti/driverlib/I2C", {}, false);
 const I2C1  = I2C.addInstance();
 const I2C2  = I2C.addInstance();
@@ -22,6 +25,16 @@ const I2C2  = I2C.addInstance();
 CRC.polynomial = "16_POLYNOMIAL";
 CRC.seed       = 0xFFFF;
 
+GPIO1.$name                         = "GPIO_Battery_Charging";
+GPIO1.port                          = "PORTB";
+GPIO1.associatedPins[0].$name       = "PIN_PB0";
+GPIO1.associatedPins[0].pin.$assign = "PB0";
+
+GPIO2.$name                         = "GPIO_Battery_Discharging";
+GPIO2.port                          = "PORTB";
+GPIO2.associatedPins[0].$name       = "PIN_PB8";
+GPIO2.associatedPins[0].pin.$assign = "PB8";
+
 I2C1.basicEnableController             = true;
 I2C1.advControllerTXFIFOTRIG           = "BYTES_1";
 I2C1.advAnalogGlitchFilter             = "DISABLED";

+ 0 - 9
i2c_target.c

@@ -56,15 +56,6 @@ void Battery_ReadState(uint8_t slot_id){
 }
 
 
-void Battery_StateUpdate(){
-    piTxCount= 0;
-    piTxLen = NUM_SLOTS;
-    for(uint8_t slot=0; slot< piTxLen; slot++){
-        Battery_ReadState(slot);
-        piTxPacket[slot]= batteries[slot].state;
-
-    }
-}
 
 /*
      {

+ 1 - 1
i2c_target.h

@@ -44,5 +44,5 @@ typedef struct{
 
 //void I2C_Init();
 //void ProcessI2Ccommand();
-void Battery_StateUpdate();
+void Battery_ReadState(uint8_t slot_id);
 #endif

+ 1 - 1
multiplexer.c

@@ -34,7 +34,7 @@ void Multiplexer_SelectChannel(uint8_t channel)
         return;
     }
     uint8_t data = (channel);
-    printf("Selecting Multiplexer Channel: 0x%02X\n", data);
+    //printf("Selecting Multiplexer 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);

Неке датотеке нису приказане због велике количине промена