|
|
@@ -22,8 +22,8 @@ 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;
|
|
|
+//previous cycle count variable keeps track of the WWDT reset during the charging else by default it is 0, currently initialized to 0
|
|
|
+static uint8_t previous_cycle_counter= 0;
|
|
|
|
|
|
// Functions for Charging and Discharging State
|
|
|
|
|
|
@@ -43,77 +43,84 @@ 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;
|
|
|
|
|
|
+
|
|
|
//For logging state transitions
|
|
|
- if(charging_state != previous_state){
|
|
|
+ /*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) || (battery_data[slot_id].batteryLimitReceived == false)) {
|
|
|
+ if ((battery_data[slot_id].battery_state == STATE_EMPTY) || (battery_data[slot_id].batteryLimitReceived == 0)) {
|
|
|
charging_state = STATE_IDLE;
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
- //Check if the battery is overcharged
|
|
|
- if (batt_voltage > batt_max_voltage) {
|
|
|
- charging_state = STATE_ERROR;
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- //1. Pre Charge: if the battery is deeply discharged
|
|
|
- if ((batt_voltage < batt_min_voltage)) {
|
|
|
- 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
|
|
|
-
|
|
|
- else if (!cv_charging_started &&
|
|
|
- (batt_voltage >= batt_min_voltage) &&
|
|
|
- (batt_voltage < batt_max_voltage - BATTERY_THRESHOLD)) {
|
|
|
- charging_state = STATE_CC_CHARGING;
|
|
|
- }
|
|
|
- // 3. CV Charging condition: Changing the cv_charging_state to True, once the
|
|
|
- // CV mode is reached
|
|
|
-
|
|
|
- else if (batt_voltage >= batt_max_voltage - BATTERY_THRESHOLD) {
|
|
|
- charging_state = STATE_CV_CHARGING;
|
|
|
- cv_charging_started = true;
|
|
|
- }
|
|
|
-
|
|
|
- // 4. Cut-off check inside CV
|
|
|
-
|
|
|
- else if ((charging_state == STATE_CV_CHARGING) &&
|
|
|
- (batt_current <= batt_cutoff_current + BATTERY_THRESHOLD)) {
|
|
|
- if (cycle_count < MAX_CYCLES) {
|
|
|
- charging_state = STATE_DISCHARGING;
|
|
|
- cycle_count++;
|
|
|
- } else {
|
|
|
- charging_state = STATE_FINAL_DISCHARGE;
|
|
|
- }
|
|
|
+
|
|
|
+ if((battery_data[slot_id].battery_measurement.voltage == 0) && (battery_data[slot_id].battery_measurement.current == 0) && (battery_data[slot_id].battery_measurement.temperature == 0)){
|
|
|
+ charging_state= STATE_IDLE;
|
|
|
+ return;
|
|
|
}
|
|
|
+
|
|
|
+ if(battery_data[slot_id].batteryLimitReceived == 1){
|
|
|
+ //1. Pre Charge: if the battery is deeply discharged
|
|
|
+ if ((batt_voltage < batt_min_voltage)) {
|
|
|
+ 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
|
|
|
|
|
|
- // 5. State Discharging condition
|
|
|
+ else if (!cv_charging_started &&
|
|
|
+ (batt_voltage >= batt_min_voltage) &&
|
|
|
+ (batt_voltage < batt_max_voltage - BATTERY_THRESHOLD)) {
|
|
|
+ charging_state = STATE_CC_CHARGING;
|
|
|
+ }
|
|
|
+ // 3. CV Charging condition: Changing the cv_charging_state to True, once the CV mode is reached
|
|
|
|
|
|
- else if (charging_state == STATE_DISCHARGING &&
|
|
|
- batt_voltage <= batt_min_voltage + BATTERY_THRESHOLD) {
|
|
|
- DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
|
|
|
- charging_state = STATE_CC_CHARGING;
|
|
|
- cv_charging_started = false;
|
|
|
- }
|
|
|
+ else if (batt_voltage >= batt_max_voltage - BATTERY_THRESHOLD) {
|
|
|
+ charging_state = STATE_CV_CHARGING;
|
|
|
+ cv_charging_started = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 4. Cut-off check inside CV
|
|
|
+
|
|
|
+ else if ((charging_state == STATE_CV_CHARGING) &&
|
|
|
+ (batt_current <= batt_cutoff_current + BATTERY_THRESHOLD)) {
|
|
|
+ if (cycle_count < MAX_CYCLES) {
|
|
|
+ charging_state = STATE_DISCHARGING;
|
|
|
+ previous_cycle_counter= cycle_count;
|
|
|
+ cycle_count++;
|
|
|
+ } else {
|
|
|
+ charging_state = STATE_FINAL_DISCHARGE;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- // 6. Hold previous state if none of the condition matches (Previously,
|
|
|
- // CV_STATE was jumping directly to STATE_ERROR in else statement, to handle
|
|
|
- // the condition better, else condition is being changed).
|
|
|
- else {
|
|
|
- printf("[CC-CV] Current state: Voltage= %d mV, Current= %d mA\n",
|
|
|
- batt_voltage, batt_current);
|
|
|
+ // 5. State Discharging condition
|
|
|
+
|
|
|
+ else if (charging_state == STATE_DISCHARGING &&
|
|
|
+ batt_voltage <= batt_min_voltage + BATTERY_THRESHOLD) {
|
|
|
+ DL_GPIO_clearPins(GPIO_Battery_Discharging_PORT, GPIO_Battery_Discharging_PIN_PB7_PIN);
|
|
|
+ charging_state = STATE_CC_CHARGING;
|
|
|
+ cv_charging_started = false;
|
|
|
+ }
|
|
|
+
|
|
|
+ //6. Check if the battery is overcharged
|
|
|
+ else if (batt_voltage > batt_max_voltage) {
|
|
|
+ charging_state = STATE_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 7. Hold previous state if none of the condition matches (Previously,
|
|
|
+ // CV_STATE was jumping directly to STATE_ERROR in else statement, to handle
|
|
|
+ // the condition better, else condition is being changed).
|
|
|
+ else {
|
|
|
+ printf("[CC-CV] Current state: Voltage= %d mV, Current= %d mA\n",
|
|
|
+ batt_voltage, batt_current);
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
/*
|
|
|
Function for Battery Charging and Discharging:
|
|
|
@@ -216,7 +223,7 @@ void CC_CV_ControlCharging(uint8_t slot_id, int16_t charge_current) {
|
|
|
case STATE_IDLE:
|
|
|
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);
|
|
|
- charging_state = STATE_PRE_CHARGE;
|
|
|
+ //charging_state = STATE_PRE_CHARGE;
|
|
|
break;
|
|
|
|
|
|
default:
|