diff src/cs/drivers/drv_app/fchg/fchg_process.c @ 61:75067af48bfd

FCHG updates for Tourmaline UI integration
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 22 Oct 2020 22:30:49 +0000
parents 4e78acac3d88
children 3c790d29748e
line wrap: on
line diff
--- 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,