diff src/cs/drivers/drv_app/fchg/fchg_process.c @ 369:65d53dede3b2

FCHG: charging time limit implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 01 Jan 2018 18:11:24 +0000
parents 44795dbadbae
children e4d46979846f
line wrap: on
line diff
--- a/src/cs/drivers/drv_app/fchg/fchg_process.c	Mon Jan 01 00:38:38 2018 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_process.c	Mon Jan 01 18:11:24 2018 +0000
@@ -77,6 +77,8 @@
 			pwr_ctrl->config.ci_current + pwr_ctrl->i2v_offset);
 	/* Enable the charger */
 	ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0003);
+	/* The total charging time starts now */
+	pwr_ctrl->start_time = rvf_get_tick_count();
 }
 
 static void start_cv_charging(void)
@@ -182,7 +184,7 @@
 	    (pwr_ctrl->cv_dac_init - pwr_ctrl->config.cv_dac_max_decr))
 		return 0;
 	rvf_send_trace("Stopping charge by overvoltage condition", 40,
-			NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
+			NULL_PARAM, RV_TRACE_LEVEL_WARNING, FCHG_USE_ID);
 	ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
 	pwr_init_discharge();
 	pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
@@ -227,6 +229,20 @@
 	pwr_ctrl->cv_low_vbat_count = 0;
 }
 
+static int charging_time_limit_check(void)
+{
+	if ((rvf_get_tick_count() - pwr_ctrl->start_time) <
+	    RVF_SECS_TO_TICKS(pwr_ctrl->config.charge_time_limit))
+		return 0;
+	rvf_send_trace("Stopping charge by time exceeded condition", 42,
+			NULL_PARAM, RV_TRACE_LEVEL_WARNING, FCHG_USE_ID);
+	ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
+	pwr_init_discharge();
+	pwr_ctrl->state = FCHG_STATE_RECHARGE_TIMER;
+	pwr_ctrl->start_time = rvf_get_tick_count();
+	return 1;
+}
+
 void pwr_process_adc(struct pwr_adc_ind_s *msg)
 {
 	pwr_ctrl->batt_mv = madc_vbat_2_physical(msg->data[0]);
@@ -286,6 +302,8 @@
 			pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
 			return;
 		}
+		if (charging_time_limit_check())
+			return;
 		if (pwr_ctrl->batt_mv >= pwr_ctrl->config.ci2cv_thresh)
 			start_cv_charging();
 		return;
@@ -299,9 +317,21 @@
 			return;
 		if (overvoltage_end_charge_check())
 			return;
+		if (charging_time_limit_check())
+			return;
 		cv_ctrl_loop_high_check();
 		cv_ctrl_loop_low_check();
 		return;
+	case FCHG_STATE_RECHARGE_TIMER:
+		handle_discharge();
+		if ((rvf_get_tick_count() - pwr_ctrl->start_time) <
+		    RVF_SECS_TO_TICKS(pwr_ctrl->config.recharge_delay))
+			return;
+		rvf_send_trace("Restart time met, allowing new charging", 39,
+				NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
+				FCHG_USE_ID);
+		pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
+		return;
 	default:
 		rvf_send_trace("Invalid state in pwr_process_adc()", 32,
 				pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
@@ -360,6 +390,7 @@
 	case FCHG_STATE_READY_TO_CHARGE:
 	case FCHG_STATE_READY_TO_RECHARGE:
 	case FCHG_STATE_I2V_CAL_1:
+	case FCHG_STATE_RECHARGE_TIMER:
 	case FCHG_STATE_NO_CHARGING:
 		rvf_send_trace("Charger unplug", 14, NULL_PARAM,
 				RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
@@ -400,6 +431,7 @@
 	case FCHG_STATE_PWR_PLUG_TIMER:
 	case FCHG_STATE_READY_TO_CHARGE:
 	case FCHG_STATE_READY_TO_RECHARGE:
+	case FCHG_STATE_RECHARGE_TIMER:
 		rvf_send_trace("Starting charge on user request", 31,
 				NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
 				FCHG_USE_ID);
@@ -431,6 +463,7 @@
 	case FCHG_STATE_READY_TO_CHARGE:
 	case FCHG_STATE_READY_TO_RECHARGE:
 	case FCHG_STATE_I2V_CAL_1:
+	case FCHG_STATE_RECHARGE_TIMER:
 		rvf_send_trace("Charging disabled by user request", 33,
 				NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
 				FCHG_USE_ID);