changeset 61:75067af48bfd

FCHG updates for Tourmaline UI integration
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 22 Oct 2020 22:30:49 +0000
parents 48f280c19e16
children 089c1882788c
files src/cs/drivers/drv_app/fchg/fchg_api.c src/cs/drivers/drv_app/fchg/fchg_api.h src/cs/drivers/drv_app/fchg/fchg_common.h src/cs/drivers/drv_app/fchg/fchg_default_batt.c src/cs/drivers/drv_app/fchg/fchg_ffs_init.c src/cs/drivers/drv_app/fchg/fchg_process.c src/cs/drivers/drv_app/fchg/fchg_struct.h
diffstat 7 files changed, 114 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/cs/drivers/drv_app/fchg/fchg_api.c	Wed Oct 21 03:31:04 2020 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_api.c	Thu Oct 22 22:30:49 2020 +0000
@@ -11,12 +11,44 @@
 
 T_RV_RET fchg_get_current_state(struct fchg_user_state *rstruct)
 {
+	UINT16 curr_disch_thresh;
+	INT16 ichg_temp;
+
 	if (!pwr_ctrl)
 		return RV_NOT_READY;
 	rstruct->chg_state = pwr_ctrl->state;
 	rstruct->batt_mv = pwr_ctrl->batt_mv;
+	curr_disch_thresh = pwr_ctrl->curr_disch_thresh;
 	rstruct->batt_percent =
-	    pwr_ctrl->batt_thresholds[pwr_ctrl->curr_disch_thresh].remain_capa;
+	    pwr_ctrl->batt.percent_thresh[curr_disch_thresh].remain_capa;
+	switch (rstruct->chg_state) {
+	case FCHG_STATE_CI_CHARGING:
+		ichg_temp = pwr_ctrl->ci_ichg - pwr_ctrl->i2v_offset;
+		if (ichg_temp < 0)
+			ichg_temp = 0;
+		rstruct->ichg = ichg_temp;
+		rstruct->batt_bars = FCHG_BATT_BARS_CHARGING;
+		break;
+	case FCHG_STATE_CV_CHARGING:
+		ichg_temp = pwr_ctrl->ichg_average - pwr_ctrl->i2v_offset;
+		if (ichg_temp < 0)
+			ichg_temp = 0;
+		rstruct->ichg = ichg_temp;
+		rstruct->batt_bars = FCHG_BATT_BARS_CHARGING;
+		break;
+	default:
+		rstruct->ichg = 0;
+		if (curr_disch_thresh <= pwr_ctrl->batt.bars_thresh[0])
+			rstruct->batt_bars = 4;
+		else if (curr_disch_thresh <= pwr_ctrl->batt.bars_thresh[1])
+			rstruct->batt_bars = 3;
+		else if (curr_disch_thresh <= pwr_ctrl->batt.bars_thresh[2])
+			rstruct->batt_bars = 2;
+		else if (curr_disch_thresh <= pwr_ctrl->batt.bars_thresh[3])
+			rstruct->batt_bars = 1;
+		else
+			rstruct->batt_bars = 0;
+	}
 	return RV_OK;
 }
 
@@ -58,3 +90,11 @@
 	}
 	return RV_OK;
 }
+
+T_RV_RET fchg_register_event_handler(T_FCHG_EVENT_HANDLER handler)
+{
+	if (!pwr_ctrl)
+		return RV_NOT_READY;
+	pwr_ctrl->event_handler = handler;
+	return RV_OK;
+}
--- a/src/cs/drivers/drv_app/fchg/fchg_api.h	Wed Oct 21 03:31:04 2020 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_api.h	Thu Oct 22 22:30:49 2020 +0000
@@ -14,13 +14,18 @@
 	FCHG_CHARGE_START	= 1
 };
 
+#define	FCHG_BATT_BARS_CHARGING	0xFF
+
 struct fchg_user_state {
 	enum fchg_state	chg_state;
 	UINT16		batt_mv;
 	T_PWR_PERCENT	batt_percent;
+	UINT8		batt_bars;
+	UINT16		ichg;
 };
 
 T_RV_RET fchg_user_charge_control(enum fchg_user_charge_ctrl);
 T_RV_RET fchg_get_current_state(struct fchg_user_state *);
+T_RV_RET fchg_register_event_handler(T_FCHG_EVENT_HANDLER);
 
 #endif	/* include guard */
--- a/src/cs/drivers/drv_app/fchg/fchg_common.h	Wed Oct 21 03:31:04 2020 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_common.h	Thu Oct 22 22:30:49 2020 +0000
@@ -22,4 +22,16 @@
 /* from original PWR SWE */
 typedef UINT8 T_PWR_PERCENT;
 
+enum fchg_event {
+	FCHG_EVENT_DISCHARGE,
+	FCHG_EVENT_CHARGER_PLUG,
+	FCHG_EVENT_CHARGER_UNPLUG,
+	FCHG_EVENT_CHARGING_START,
+	FCHG_EVENT_CHARGING_COMPLETE,
+	FCHG_EVENT_CHARGING_STOPPED,
+	FCHG_EVENT_CHARGING_TIMEOUT
+};
+
+typedef void (*T_FCHG_EVENT_HANDLER)(enum fchg_event);
+
 #endif	/* include guard */
--- a/src/cs/drivers/drv_app/fchg/fchg_default_batt.c	Wed Oct 21 03:31:04 2020 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_default_batt.c	Thu Oct 22 22:30:49 2020 +0000
@@ -17,27 +17,31 @@
 	{4030, 85},
 	{3964, 80},
 	{3930, 75},
-	{3900, 70},
+	{3900, 70},	/* 4 bars */
 	{3882, 65},
 	{3847, 60},
 	{3805, 55},
-	{3786, 50},
+	{3786, 50},	/* 3 bars */
 	{3771, 45},
 	{3759, 40},
 	{3750, 35},
 	{3745, 30},
-	{3737, 25},
+	{3737, 25},	/* 2 bars */
 	{3719, 20},
 	{3688, 15},
 	{3663, 10},
-	{3539, 5},
+	{3539, 5},	/* 1 bar */
 	{3370, 0}
 };
 
+static const UINT8 default_batt_bars_table[NB_BARS_THRESH] = {6, 10, 15, 19};
+
 void pwr_set_default_batt_table(void)
 {
-	memcpy(pwr_ctrl->batt_thresholds, default_batt_table,
+	memcpy(pwr_ctrl->batt.percent_thresh, default_batt_table,
 		sizeof default_batt_table);
-	pwr_ctrl->nb_thresholds = sizeof(default_batt_table) /
-				  sizeof(T_PWR_THRESHOLDS);
+	pwr_ctrl->nb_percent_thresh = sizeof(default_batt_table) /
+				      sizeof(T_PWR_THRESHOLDS);
+	memcpy(pwr_ctrl->batt.bars_thresh, default_batt_bars_table,
+		sizeof default_batt_bars_table);
 }
--- a/src/cs/drivers/drv_app/fchg/fchg_ffs_init.c	Wed Oct 21 03:31:04 2020 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_ffs_init.c	Thu Oct 22 22:30:49 2020 +0000
@@ -35,10 +35,11 @@
 {
 	int rc;
 
-	rc = ffs_file_read("/etc/batterytab", pwr_ctrl->batt_thresholds,
-			   sizeof(pwr_ctrl->batt_thresholds));
-	if (rc >= (int)sizeof(T_PWR_THRESHOLDS)) {
-		pwr_ctrl->nb_thresholds = rc / sizeof(T_PWR_THRESHOLDS);
+	rc = ffs_file_read("/etc/batterytab2", &pwr_ctrl->batt,
+			   sizeof(struct battery_config));
+	if (rc >= (int)(sizeof(T_PWR_THRESHOLDS) * MIN_PERCENT_THRESH + 4)) {
+		pwr_ctrl->nb_percent_thresh =
+					(rc - 4) / sizeof(T_PWR_THRESHOLDS);
 		rvf_send_trace("FCHG: battery table loaded from FFS", 35,
 				NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
 				FCHG_USE_ID);
--- a/src/cs/drivers/drv_app/fchg/fchg_process.c	Wed Oct 21 03:31:04 2020 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_process.c	Thu Oct 22 22:30:49 2020 +0000
@@ -34,23 +34,24 @@
 
 	/* first we need to find the current threshold we are at */
 	i = pwr_ctrl->curr_disch_thresh;
-	/* is there one below? */
-	if (++i == pwr_ctrl->nb_thresholds)
+	/* are we at the bottom? */
+	if ((i + 1) == pwr_ctrl->nb_percent_thresh)
 		return;
-	/* are we crossing it? */
-	if (pwr_ctrl->batt_mv >= pwr_ctrl->batt_thresholds[i].bat_voltage)
+	/* are we crossing our current threshold? */
+	if (pwr_ctrl->batt_mv >= pwr_ctrl->batt.percent_thresh[i].bat_voltage)
 		return;
 	/* yes, we crossed it - see if we fell even further down */
-	while (i < pwr_ctrl->nb_thresholds &&
-	       pwr_ctrl->batt_mv < pwr_ctrl->batt_thresholds[i].bat_voltage)
+	while (i < pwr_ctrl->nb_percent_thresh-1 &&
+	       pwr_ctrl->batt_mv < pwr_ctrl->batt.percent_thresh[i].bat_voltage)
 		i++;
 	/* the last one was it */
-	i--;
 	pwr_ctrl->curr_disch_thresh = i;
 	sprintf(trace, "Battery fell through %u%% mark",
-		pwr_ctrl->batt_thresholds[i].remain_capa);
+		pwr_ctrl->batt.percent_thresh[i-1].remain_capa);
 	rvf_send_trace(trace, strlen(trace), NULL_PARAM,
 			RV_TRACE_LEVEL_WARNING, FCHG_USE_ID);
+	if (pwr_ctrl->event_handler)
+		pwr_ctrl->event_handler(FCHG_EVENT_DISCHARGE);
 }
 
 static void start_i2v_cal(void)
@@ -86,6 +87,8 @@
 	ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0003 | LEDC);
 	/* The total charging time starts now */
 	pwr_ctrl->start_time = rvf_get_tick_count();
+	if (pwr_ctrl->event_handler)
+		pwr_ctrl->event_handler(FCHG_EVENT_CHARGING_START);
 }
 
 static void start_cv_charging(void)
@@ -132,6 +135,7 @@
 {
 	char trace[64];
 
+	pwr_ctrl->ci_ichg = ichg;
 	sprintf(trace, "CI charging: Vbat=%u Ichg=%u i2v=%u",
 		pwr_ctrl->batt_mv, ichg, pwr_ctrl->i2v_offset);
 	rvf_send_trace(trace, strlen(trace), NULL_PARAM,
@@ -180,6 +184,8 @@
 	ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
 	pwr_init_discharge();
 	pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
+	if (pwr_ctrl->event_handler)
+		pwr_ctrl->event_handler(FCHG_EVENT_CHARGING_COMPLETE);
 	return 1;
 }
 
@@ -195,6 +201,8 @@
 	ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
 	pwr_init_discharge();
 	pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
+	if (pwr_ctrl->event_handler)
+		pwr_ctrl->event_handler(FCHG_EVENT_CHARGING_COMPLETE);
 	return 1;
 }
 
@@ -247,6 +255,8 @@
 	pwr_init_discharge();
 	pwr_ctrl->state = FCHG_STATE_RECHARGE_TIMER;
 	pwr_ctrl->start_time = rvf_get_tick_count();
+	if (pwr_ctrl->event_handler)
+		pwr_ctrl->event_handler(FCHG_EVENT_CHARGING_TIMEOUT);
 	return 1;
 }
 
@@ -385,6 +395,8 @@
 				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);
 }
 
 void pwr_charger_unplug(void)
@@ -395,7 +407,7 @@
 				NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW,
 				FCHG_USE_ID);
 		/* nothing to do */
-		return;
+		break;
 	case FCHG_STATE_PWR_PLUG_TIMER:
 	case FCHG_STATE_READY_TO_CHARGE:
 	case FCHG_STATE_READY_TO_RECHARGE:
@@ -405,7 +417,7 @@
 		rvf_send_trace("Charger unplug", 14, NULL_PARAM,
 				RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
 		pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
-		return;
+		break;
 	case FCHG_STATE_I2V_CAL_2:
 	case FCHG_STATE_CI_CHARGING:
 	case FCHG_STATE_CV_CHARGING:
@@ -415,12 +427,14 @@
 				FCHG_USE_ID);
 		pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
 		pwr_init_discharge();
-		return;
+		break;
 	default:
 		rvf_send_trace("Invalid state in pwr_charger_unplug()", 35,
 				pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
 				FCHG_USE_ID);
 	}
+	if (pwr_ctrl->event_handler)
+		pwr_ctrl->event_handler(FCHG_EVENT_CHARGER_UNPLUG);
 }
 
 void pwr_charge_start_req(void)
@@ -488,6 +502,8 @@
 				FCHG_USE_ID);
 		pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
 		pwr_init_discharge();
+		if (pwr_ctrl->event_handler)
+			pwr_ctrl->event_handler(FCHG_EVENT_CHARGING_STOPPED);
 		return;
 	default:
 		rvf_send_trace("Invalid state in pwr_charge_stop_req()", 36,
--- a/src/cs/drivers/drv_app/fchg/fchg_struct.h	Wed Oct 21 03:31:04 2020 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_struct.h	Thu Oct 22 22:30:49 2020 +0000
@@ -38,7 +38,14 @@
 	T_PWR_PERCENT	remain_capa;
 } T_PWR_THRESHOLDS;
 
-#define	MAX_THRESHOLDS	101
+#define	MIN_PERCENT_THRESH	5
+#define	MAX_PERCENT_THRESH	21	/* allowing 5% steps */
+#define	NB_BARS_THRESH		4
+
+struct battery_config {
+	UINT8			bars_thresh[NB_BARS_THRESH];
+	T_PWR_THRESHOLDS	percent_thresh[MAX_PERCENT_THRESH];
+};
 
 #define	ICHG_AVG_WINDOW	6
 
@@ -46,17 +53,20 @@
 	/* RiViera boilerplate */
 	T_RVF_ADDR_ID		addr_id;
 	T_RVF_MB_ID		prim_id;
+	/* interface to upper layers */
+	T_FCHG_EVENT_HANDLER	event_handler;
 	/* configuration */
 	struct charging_config	config;
 	BOOL			config_present;
-	T_PWR_THRESHOLDS	batt_thresholds[MAX_THRESHOLDS];
-	UINT16			nb_thresholds;
+	struct battery_config	batt;
+	UINT16			nb_percent_thresh;
 	/* state */
 	enum fchg_state		state;
 	UINT16			batt_mv;
 	UINT16			curr_disch_thresh;
 	/* valid only during a charging cycle */
 	UINT16			i2v_offset;
+	UINT16			ci_ichg;
 	UINT16			cv_dac_init;
 	UINT16			cv_dac_curr;
 	UINT16			cv_high_vbat_count;