|
|
@@ -6,21 +6,34 @@
|
|
|
#include "ti_msp_dl_config.h"
|
|
|
#include "src/controller/controller.h"
|
|
|
|
|
|
-static ChargingState charging_state= STATE_IDLE;
|
|
|
-static uint16_t cycle_count = 0;
|
|
|
+
|
|
|
// declaring static global variables to retain their values between multiple function calls till the end of the program
|
|
|
+
|
|
|
+static BatteryChargingState charging_state= STATE_IDLE; //setting default state of the battery charging state
|
|
|
+static uint16_t cycle_count= 0;
|
|
|
+
|
|
|
+//Battery Measurements Received
|
|
|
static uint16_t batt_voltage;
|
|
|
static int16_t batt_current;
|
|
|
+
|
|
|
+//Battery Limits Received
|
|
|
static uint16_t batt_min_voltage;
|
|
|
static uint16_t batt_max_voltage;
|
|
|
static uint16_t batt_cutoff_current;
|
|
|
static uint16_t batt_capacitance;
|
|
|
static int16_t batt_charge_discharge;
|
|
|
+//previous cycle count variable keeps track of the WWDT reset during the charging else by default it is 0
|
|
|
+static uint8_t previous_cycle_count;
|
|
|
+
|
|
|
// Functions for Charging and Discharging State
|
|
|
|
|
|
void CC_CV_UpdateChargingState(uint8_t slot_id) {
|
|
|
- //static variable to keep a log on the transition during charging and dischrging phases
|
|
|
- static ChargingState previous_state= STATE_IDLE;
|
|
|
+ //static variable to keep a log on the transition during charging and discharging phases
|
|
|
+ static BatteryChargingState previous_state= STATE_IDLE;
|
|
|
+ //Storing the charging state in the BatteryInfo struct for Pi monitoring:
|
|
|
+ BatteryInfo *battery= &battery_data[slot_id];
|
|
|
+ battery->battery_charging_state= charging_state;
|
|
|
+ battery->cycle_number= cycle_count;
|
|
|
// Flag for CV charging, the charging mode flips back to CC mode
|
|
|
static bool cv_charging_started = false;
|
|
|
batt_voltage = battery_data[slot_id].battery_measurement.voltage;
|
|
|
@@ -30,17 +43,16 @@ void CC_CV_UpdateChargingState(uint8_t slot_id) {
|
|
|
batt_cutoff_current = battery_data[slot_id].cut_off_current;
|
|
|
batt_capacitance = battery_data[slot_id].capacitance;
|
|
|
batt_charge_discharge= battery_data[slot_id].charge_discharge;
|
|
|
+ previous_cycle_count= battery_data[slot_id].previous_cycle_number;
|
|
|
|
|
|
-
|
|
|
- //Log for transitions
|
|
|
+ //For logging state transitions
|
|
|
if(charging_state != previous_state){
|
|
|
printf("State transition: %d -> %d\n", previous_state, charging_state);
|
|
|
previous_state= charging_state;
|
|
|
}
|
|
|
|
|
|
//Check if the slot is empty or if the battery limits are not set
|
|
|
- if (battery_data[slot_id].battery_state == STATE_EMPTY || (batt_min_voltage == 0 && batt_max_voltage == 0 &&
|
|
|
- batt_cutoff_current== 0 && batt_capacitance== 0)) {
|
|
|
+ if ((battery_data[slot_id].battery_state == STATE_EMPTY) || (battery_data[slot_id].batteryLimitReceived == false)) {
|
|
|
charging_state = STATE_IDLE;
|
|
|
return;
|
|
|
}
|
|
|
@@ -56,6 +68,7 @@ void CC_CV_UpdateChargingState(uint8_t slot_id) {
|
|
|
charging_state = STATE_PRE_CHARGE;
|
|
|
cv_charging_started = false;
|
|
|
}
|
|
|
+
|
|
|
// 2. Fast Charging Condition: CC Charging: cv_charging_started condition
|
|
|
// added to avoid toggling back to CC after CV state is reached
|
|
|
|
|
|
@@ -138,7 +151,7 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
|
|
|
case STATE_PRE_CHARGE:
|
|
|
//DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB6_PIN);
|
|
|
DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
|
|
|
- controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
|
|
|
+ controller_SetCurrent(TARGET_BASE_ADDRESS, slot_id, charge_current);
|
|
|
if (true) {
|
|
|
printf("PRE CHARGING: Slot %d at %d mA.\n", slot_id, charge_current);
|
|
|
}
|
|
|
@@ -148,7 +161,7 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
|
|
|
// reaches to (MAXIMUM_VOLTAGE-BATTERY_THRESHOLD)= 4150 mVolts
|
|
|
case STATE_CC_CHARGING:
|
|
|
DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
|
|
|
- controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
|
|
|
+ controller_SetCurrent(TARGET_BASE_ADDRESS, slot_id, charge_current);
|
|
|
printf("CC CHARGING: Slot %d, Current: %d mA, Voltage: %d mV.\n", slot_id, batt_current, batt_voltage);
|
|
|
break;
|
|
|
|
|
|
@@ -163,7 +176,7 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
|
|
|
charging_state= STATE_ERROR;
|
|
|
break;
|
|
|
}
|
|
|
- controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
|
|
|
+ controller_SetCurrent(TARGET_BASE_ADDRESS, slot_id, charge_current);
|
|
|
}else {
|
|
|
charging_state = STATE_FINAL_DISCHARGE;
|
|
|
dac_initialized= false;
|
|
|
@@ -175,7 +188,7 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
|
|
|
DL_GPIO_setPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB6_PIN);
|
|
|
DL_GPIO_setPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
|
|
|
//controller_EvaluateBatterySlotState(slot_id, BatteryMeasurement *measurement);
|
|
|
- controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, charge_current);
|
|
|
+ controller_SetCurrent(TARGET_BASE_ADDRESS, slot_id, charge_current);
|
|
|
printf("DISCHARGING: Slot %d at %d mA.\n", slot_id, batt_current);
|
|
|
|
|
|
//Safety Check
|
|
|
@@ -190,7 +203,7 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
|
|
|
battery_data[slot_id].battery_state= STATE_MEASUREMENT_DONE;
|
|
|
DL_GPIO_clearPins(GPIO_Battery_Charging_PORT, GPIO_Battery_Charging_PIN_PB6_PIN);
|
|
|
DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
|
|
|
- controller_SetCurrent(TARGET_MCU_ADDRESS, slot_id, 0);
|
|
|
+ controller_SetCurrent(TARGET_BASE_ADDRESS, slot_id, 0);
|
|
|
charging_state = STATE_IDLE;
|
|
|
break;
|
|
|
|