changeset 253:769cf6273fe4

FCHG: beginning of battery simulation mode
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 12 May 2021 11:14:51 +0000
parents 27f17fbf0657
children 4533ef63fdb0
files src/cs/drivers/drv_app/fchg/fchg_api.c src/cs/drivers/drv_app/fchg/fchg_ffs_init.c src/cs/drivers/drv_app/fchg/fchg_func_i.h src/cs/drivers/drv_app/fchg/fchg_process.c src/cs/drivers/drv_app/fchg/fchg_struct.h src/cs/drivers/drv_app/fchg/fchg_task.c
diffstat 6 files changed, 104 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/src/cs/drivers/drv_app/fchg/fchg_api.c	Wed May 12 03:18:50 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_api.c	Wed May 12 11:14:51 2021 +0000
@@ -61,7 +61,7 @@
 		return RV_NOT_READY;
 	switch (arg) {
 	case FCHG_CHARGE_START:
-		if (!pwr_ctrl->config_present)
+		if (!pwr_ctrl->config_present && !pwr_ctrl->bsim_mode)
 			return RV_NOT_READY;
 		msg_id = USER_START_CHARGE_REQ;
 		break;
--- a/src/cs/drivers/drv_app/fchg/fchg_ffs_init.c	Wed May 12 03:18:50 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_ffs_init.c	Wed May 12 11:14:51 2021 +0000
@@ -50,3 +50,20 @@
 				FCHG_USE_ID);
 	}
 }
+
+void pwr_check_ffs_bsim(void)
+{
+	int rc;
+
+	rc = ffs_file_read("/etc/batterysim", &pwr_ctrl->bsim,
+			   sizeof(struct bsim_config));
+	if (rc == sizeof(struct bsim_config)) {
+		pwr_ctrl->bsim_mode = TRUE;
+		rvf_send_trace(
+		"FCHG: operating in battery simulation mode per FFS config!",
+				58, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
+				FCHG_USE_ID);
+	} else {
+		pwr_ctrl->bsim_mode = FALSE;
+	}
+}
--- a/src/cs/drivers/drv_app/fchg/fchg_func_i.h	Wed May 12 03:18:50 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_func_i.h	Wed May 12 11:14:51 2021 +0000
@@ -10,6 +10,7 @@
 void pwr_init_discharge(void);
 void pwr_load_ffs_batt_table(void);
 void pwr_load_ffs_charging_config(void);
+void pwr_check_ffs_bsim(void);
 void pwr_set_default_batt_table(void);
 
 void pwr_process_message(T_RV_HDR *msg_ptr);
--- a/src/cs/drivers/drv_app/fchg/fchg_process.c	Wed May 12 03:18:50 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_process.c	Wed May 12 11:14:51 2021 +0000
@@ -277,40 +277,36 @@
 	return 1;
 }
 
-void pwr_process_adc(struct pwr_adc_ind_s *msg)
+static void process_adc_regular(struct pwr_adc_ind_s *msg)
 {
-	pwr_ctrl->batt_mv = madc_vbat_2_physical(msg->data[0]);
-	if (!fchg_first_vbat)
-		capture_first_vbat();
-
 	switch (pwr_ctrl->state) {
 	case FCHG_STATE_NO_EXT_PWR:
 	case FCHG_STATE_PWR_PLUG_TIMER:
 	case FCHG_STATE_NO_CHARGING:
 		handle_discharge();
-		break;
+		return;
 	case FCHG_STATE_READY_TO_CHARGE:
 		handle_discharge();
 		if (!(msg->data[9] & CHGPRES)) {
 			pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
-			break;
+			return;
 		}
 		if (pwr_ctrl->batt_mv < pwr_ctrl->config.start_thresh)
 			start_charge_condition_met();
-		break;
+		return;
 	case FCHG_STATE_READY_TO_RECHARGE:
 		handle_discharge();
 		if (!(msg->data[9] & CHGPRES)) {
 			pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
-			break;
+			return;
 		}
 		if (pwr_ctrl->batt_mv < pwr_ctrl->config.restart_thresh)
 			start_charge_condition_met();
-		break;
+		return;
 	case FCHG_STATE_I2V_CAL_1:
 		if (!(msg->data[9] & CHGPRES)) {
 			pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
-			break;
+			return;
 		}
 		if (pwr_ctrl->config.bciconf)
 			start_i2v_cal();
@@ -318,7 +314,7 @@
 			pwr_ctrl->i2v_offset = 0;
 			start_ci_charging();
 		}
-		break;
+		return;
 	case FCHG_STATE_I2V_CAL_2:
 		pwr_ctrl->i2v_offset = msg->data[2];
 		ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
@@ -328,56 +324,96 @@
 		if (!(msg->data[9] & CHGPRES)) {
 			pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
 			pwr_init_discharge();
-			break;
+			return;
 		}
 		start_ci_charging();
-		break;
+		return;
 	case FCHG_STATE_CI_CHARGING:
 		ci_progress_trace(msg->data[2]);
 		if (!(msg->data[9] & CHGPRES)) {
 			ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
 			pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
 			pwr_init_discharge();
-			break;
+			return;
 		}
 		if (charging_time_limit_check())
-			break;
+			return;
 		if (pwr_ctrl->batt_mv >= pwr_ctrl->config.ci2cv_thresh)
 			start_cv_charging();
-		break;
+		return;
 	case FCHG_STATE_CV_CHARGING:
 		if (!(msg->data[9] & CHGPRES)) {
 			ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
 			pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
 			pwr_init_discharge();
-			break;
+			return;
 		}
 		if (cv_ichg_process(msg->data[2]))
-			break;
+			return;
 		if (overvoltage_end_charge_check())
-			break;
+			return;
 		if (charging_time_limit_check())
-			break;
+			return;
 		cv_ctrl_loop_high_check();
 		cv_ctrl_loop_low_check();
-		break;
+		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))
-			break;
+			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;
-		break;
+		return;
 	default:
-		rvf_send_trace("Invalid state in pwr_process_adc()", 32,
+		rvf_send_trace("Invalid state in pwr_process_adc()", 34,
 				pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
 				FCHG_USE_ID);
 	}
 }
 
+static void process_adc_bsim(struct pwr_adc_ind_s *msg)
+{
+	switch (pwr_ctrl->state) {
+	case FCHG_STATE_READY_TO_CHARGE:
+		if (!pwr_ctrl->bsim.start_enable)
+			return;
+		rvf_send_trace("BSIM: simulated charging auto-start", 35,
+				NULL_PARAM, RV_TRACE_LEVEL_DEBUG_MEDIUM,
+				FCHG_USE_ID);
+		pwr_ctrl->state = FCHG_STATE_I2V_CAL_1;
+		return;
+	case FCHG_STATE_I2V_CAL_1:
+		rvf_send_trace("BSIM: advancing from I2V_CAL_1 to I2V_CAL_2",
+				43, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW,
+				FCHG_USE_ID);
+		pwr_ctrl->state = FCHG_STATE_I2V_CAL_2;
+		return;
+	case FCHG_STATE_I2V_CAL_2:
+		rvf_send_trace("BSIM: advancing from I2V_CAL_2 to CI charging",
+				45, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW,
+				FCHG_USE_ID);
+		pwr_ctrl->state = FCHG_STATE_CI_CHARGING;
+		return;
+	default:
+		/* remaining states require no action in BSIM */
+		return;
+	}
+}
+
+void pwr_process_adc(struct pwr_adc_ind_s *msg)
+{
+	pwr_ctrl->batt_mv = madc_vbat_2_physical(msg->data[0]);
+	if (!fchg_first_vbat)
+		capture_first_vbat();
+	if (pwr_ctrl->bsim_mode)
+		process_adc_bsim(msg);
+	else
+		process_adc_regular(msg);
+}
+
 void pwr_handle_timer(void)
 {
 	if (pwr_ctrl->state != FCHG_STATE_PWR_PLUG_TIMER)
@@ -395,6 +431,14 @@
 				FCHG_USE_ID);
 		return;
 	}
+	if (pwr_ctrl->bsim_mode) {
+		rvf_send_trace("Charger plug in BSIM mode", 25, NULL_PARAM,
+				RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
+		pwr_ctrl->state = FCHG_STATE_READY_TO_CHARGE;
+		if (pwr_ctrl->event_handler)
+			pwr_ctrl->event_handler(FCHG_EVENT_CHARGER_PLUG);
+		return;
+	}
 	if (!pwr_ctrl->config_present) {
 		rvf_send_trace(
 			"Charger plugged in, but no config: won't charge", 47,
@@ -448,7 +492,7 @@
 		pwr_init_discharge();
 		break;
 	default:
-		rvf_send_trace("Invalid state in pwr_charger_unplug()", 35,
+		rvf_send_trace("Invalid state in pwr_charger_unplug()", 37,
 				pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
 				FCHG_USE_ID);
 	}
@@ -464,7 +508,7 @@
 				NULL_PARAM, RV_TRACE_LEVEL_ERROR, FCHG_USE_ID);
 		return;
 	case FCHG_STATE_NO_CHARGING:
-		if (!pwr_ctrl->config_present) {
+		if (!pwr_ctrl->config_present && !pwr_ctrl->bsim_mode) {
 			rvf_send_trace("No config set, cannot charge", 28,
 					NULL_PARAM, RV_TRACE_LEVEL_ERROR,
 					FCHG_USE_ID);
@@ -489,7 +533,7 @@
 			51, NULL_PARAM, RV_TRACE_LEVEL_WARNING, FCHG_USE_ID);
 		return;
 	default:
-		rvf_send_trace("Invalid state in pwr_charge_start_req()", 37,
+		rvf_send_trace("Invalid state in pwr_charge_start_req()", 39,
 				pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
 				FCHG_USE_ID);
 	}
@@ -525,7 +569,7 @@
 			pwr_ctrl->event_handler(FCHG_EVENT_CHARGING_STOPPED);
 		return;
 	default:
-		rvf_send_trace("Invalid state in pwr_charge_stop_req()", 36,
+		rvf_send_trace("Invalid state in pwr_charge_stop_req()", 38,
 				pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
 				FCHG_USE_ID);
 	}
--- a/src/cs/drivers/drv_app/fchg/fchg_struct.h	Wed May 12 03:18:50 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_struct.h	Wed May 12 11:14:51 2021 +0000
@@ -47,6 +47,12 @@
 	T_PWR_THRESHOLDS	percent_thresh[MAX_PERCENT_THRESH];
 };
 
+struct bsim_config {
+	UINT8	init_percent;
+	UINT8	start_enable;
+	UINT8	pad[2];
+};
+
 #define	ICHG_AVG_WINDOW	6
 
 typedef struct {
@@ -60,6 +66,8 @@
 	BOOL			config_present;
 	struct battery_config	batt;
 	UINT16			nb_percent_thresh;
+	struct bsim_config	bsim;
+	BOOL			bsim_mode;
 	/* state */
 	enum fchg_state		state;
 	UINT16			batt_mv;
--- a/src/cs/drivers/drv_app/fchg/fchg_task.c	Wed May 12 03:18:50 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_task.c	Wed May 12 11:14:51 2021 +0000
@@ -15,7 +15,7 @@
 
 	abb_status = ABB_Read_Status();
 	if (abb_status & CHGPRES) {
-		if (pwr_ctrl->config_present)
+		if (pwr_ctrl->config_present || pwr_ctrl->bsim_mode)
 			pwr_ctrl->state = FCHG_STATE_READY_TO_CHARGE;
 		else
 			pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
@@ -31,7 +31,9 @@
 	rvf_send_trace("FCHG task: Initialization", 25, NULL_PARAM,
 			RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
 	pwr_load_ffs_batt_table();
-	pwr_load_ffs_charging_config();
+	pwr_check_ffs_bsim();
+	if (!pwr_ctrl->bsim_mode)
+		pwr_load_ffs_charging_config();
 	set_initial_state();
 	/* just for safety, clear any previous BCI hardware state */
 	ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);