changeset 262:7420959e02ec

FCHG rework for charger plug/unplug debouncing
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 14 May 2021 05:38:55 +0000
parents 841a848ba762
children b5e8dfd114a7
files src/cs/drivers/drv_app/abb/board/abb_inth.c src/cs/drivers/drv_app/fchg/fchg_func_i.h src/cs/drivers/drv_app/fchg/fchg_messages.c src/cs/drivers/drv_app/fchg/fchg_process.c
diffstat 4 files changed, 70 insertions(+), 72 deletions(-) [+]
line wrap: on
line diff
--- a/src/cs/drivers/drv_app/abb/board/abb_inth.c	Fri May 14 02:49:05 2021 +0000
+++ b/src/cs/drivers/drv_app/abb/board/abb_inth.c	Fri May 14 05:38:55 2021 +0000
@@ -258,9 +258,14 @@
           addr->header.callback_func = NULL;
           if (rvf_send_msg(pwr_ctrl->addr_id, addr) != RV_OK) {
               rvf_send_trace("SPI FATAL: Send failed!",23, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW, LCC_USE_ID);
-      }
+          }
 #endif
       }
+      /*
+       * FreeCalypso addition: mask further charger interrupts,
+       * until unmasked in FCHG.
+       */
+      ABB_Write_Register_on_page(PAGE0, ITMASK, 0x008);
    }
 
 #endif /* RVM_PWR_SWE || RVM_LCC_SWE */
--- a/src/cs/drivers/drv_app/fchg/fchg_func_i.h	Fri May 14 02:49:05 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_func_i.h	Fri May 14 05:38:55 2021 +0000
@@ -16,8 +16,7 @@
 void pwr_process_message(T_RV_HDR *msg_ptr);
 void pwr_process_adc(struct pwr_adc_ind_s *msg);
 void pwr_handle_timer(void);
-void pwr_charger_plug(void);
-void pwr_charger_unplug(void);
+void pwr_charger_plug_unplug_ind(void);
 void pwr_charge_start_req(void);
 void pwr_charge_stop_req(void);
 
--- a/src/cs/drivers/drv_app/fchg/fchg_messages.c	Fri May 14 02:49:05 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_messages.c	Fri May 14 05:38:55 2021 +0000
@@ -19,10 +19,8 @@
 		pwr_charge_stop_req();
 		return;
 	case PWR_CHARGER_PLUGGED_IND:
-		pwr_charger_plug();
-		return;
 	case PWR_CHARGER_UNPLUGGED_IND:
-		pwr_charger_unplug();
+		pwr_charger_plug_unplug_ind();
 		return;
 	case PWR_ADC_IND:
 		pwr_process_adc((struct pwr_adc_ind_s *) msg_ptr);
--- a/src/cs/drivers/drv_app/fchg/fchg_process.c	Fri May 14 02:49:05 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_process.c	Fri May 14 05:38:55 2021 +0000
@@ -22,6 +22,8 @@
 #define	LEDC	0
 #endif
 
+#define	CHARGER_PLUG_DEBOUNCE_MS	200
+
 /*
  * The following global variable captures the initial battery voltage
  * at the time of system boot - it is intended to be used for various
@@ -288,7 +290,7 @@
 	case FCHG_STATE_READY_TO_CHARGE:
 		handle_discharge();
 		if (!(msg->data[9] & CHGPRES)) {
-			pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
+			pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
 			return;
 		}
 		if (pwr_ctrl->batt_mv < pwr_ctrl->config.start_thresh)
@@ -297,7 +299,7 @@
 	case FCHG_STATE_READY_TO_RECHARGE:
 		handle_discharge();
 		if (!(msg->data[9] & CHGPRES)) {
-			pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
+			pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
 			return;
 		}
 		if (pwr_ctrl->batt_mv < pwr_ctrl->config.restart_thresh)
@@ -305,7 +307,7 @@
 		return;
 	case FCHG_STATE_I2V_CAL_1:
 		if (!(msg->data[9] & CHGPRES)) {
-			pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
+			pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
 			return;
 		}
 		if (pwr_ctrl->config.bciconf)
@@ -322,7 +324,7 @@
 				pwr_ctrl->i2v_offset,
 				RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
 		if (!(msg->data[9] & CHGPRES)) {
-			pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
+			pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
 			pwr_init_discharge();
 			return;
 		}
@@ -332,7 +334,7 @@
 		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_ctrl->state = FCHG_STATE_NO_CHARGING;
 			pwr_init_discharge();
 			return;
 		}
@@ -344,7 +346,7 @@
 	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_ctrl->state = FCHG_STATE_NO_CHARGING;
 			pwr_init_discharge();
 			return;
 		}
@@ -416,64 +418,18 @@
 		process_adc_regular(msg);
 }
 
-void pwr_handle_timer(void)
-{
-	if (pwr_ctrl->state != FCHG_STATE_PWR_PLUG_TIMER)
-		return;
-	rvf_send_trace("Timer expired, ready to charge", 30, NULL_PARAM,
-			RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
-	pwr_ctrl->state = FCHG_STATE_READY_TO_CHARGE;
-}
-
-void pwr_charger_plug(void)
-{
-	if (pwr_ctrl->state != FCHG_STATE_NO_EXT_PWR) {
-		rvf_send_trace("Charger plug event in unexpected state", 38,
-				pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
-				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,
-			NULL_PARAM, RV_TRACE_LEVEL_ERROR, FCHG_USE_ID);
-		pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
-		return;
-	}
-	if (pwr_ctrl->config.start_delay) {
-		rvf_send_trace("Charger plug, starting timer", 28, NULL_PARAM,
-				RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
-		rvf_start_timer(FCHG_TIMER,
-				RVF_MS_TO_TICKS(pwr_ctrl->config.start_delay),
-				FALSE);
-		pwr_ctrl->state = FCHG_STATE_PWR_PLUG_TIMER;
-	} else {
-		rvf_send_trace("Charger plug, ready to charge", 29, 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);
-}
-
-void pwr_charger_unplug(void)
+void pwr_charger_plug_unplug_ind(void)
 {
 	switch (pwr_ctrl->state) {
 	case FCHG_STATE_NO_EXT_PWR:
-		rvf_send_trace("Charger unplug, already handled", 31,
-				NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW,
+		rvf_send_trace("Charger plug interrupt, beginning debounce",
+				42, NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW,
 				FCHG_USE_ID);
-		/* nothing to do */
+		pwr_ctrl->state = FCHG_STATE_PWR_PLUG_TIMER;
 		break;
 	case FCHG_STATE_PWR_PLUG_TIMER:
+		/* debouncing in action - nothing to do here */
+		break;
 	case FCHG_STATE_READY_TO_CHARGE:
 	case FCHG_STATE_READY_TO_RECHARGE:
 	case FCHG_STATE_I2V_CAL_1:
@@ -481,7 +437,9 @@
 	case FCHG_STATE_NO_CHARGING:
 		rvf_send_trace("Charger unplug", 14, NULL_PARAM,
 				RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
-		pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
+		pwr_ctrl->state = FCHG_STATE_PWR_PLUG_TIMER;
+		if (pwr_ctrl->event_handler)
+			pwr_ctrl->event_handler(FCHG_EVENT_CHARGER_UNPLUG);
 		break;
 	case FCHG_STATE_I2V_CAL_2:
 	case FCHG_STATE_CI_CHARGING:
@@ -490,22 +448,61 @@
 		rvf_send_trace("Charger unplug, charging stopped", 32,
 				NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
 				FCHG_USE_ID);
-		pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
+		pwr_ctrl->state = FCHG_STATE_PWR_PLUG_TIMER;
 		pwr_init_discharge();
+		if (pwr_ctrl->event_handler)
+			pwr_ctrl->event_handler(FCHG_EVENT_CHARGER_UNPLUG);
 		break;
 	default:
-		rvf_send_trace("Invalid state in pwr_charger_unplug()", 37,
-				pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
+		rvf_send_trace("Invalid state in pwr_charger_plug_unplug_ind()",
+				46, pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
 				FCHG_USE_ID);
 	}
-	if (pwr_ctrl->event_handler)
-		pwr_ctrl->event_handler(FCHG_EVENT_CHARGER_UNPLUG);
+	rvf_start_timer(FCHG_TIMER, RVF_MS_TO_TICKS(CHARGER_PLUG_DEBOUNCE_MS),
+			FALSE);
+	/* unmask further charger interrupts */
+	ABB_Write_Register_on_page(PAGE0, ITMASK, 0);
+}
+
+void pwr_handle_timer(void)
+{
+	SYS_UWORD16 abb_status;
+
+	if (pwr_ctrl->state != FCHG_STATE_PWR_PLUG_TIMER) {
+		rvf_send_trace(
+			"Charger debounce timer expired in unexpected state",
+				50, pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
+				FCHG_USE_ID);
+		return;
+	}
+	/* so was it a plug or an unplug after all? */
+	abb_status = ABB_Read_Status();
+	if (abb_status & CHGPRES) {
+		/* charger plug */
+		if (pwr_ctrl->config_present || pwr_ctrl->bsim_mode) {
+			rvf_send_trace("Charger plug, ready to charge", 29,
+					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);
+		} else {
+			rvf_send_trace(
+			"Charger plugged in, but no config: won't charge", 47,
+				NULL_PARAM, RV_TRACE_LEVEL_ERROR, FCHG_USE_ID);
+			pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
+		}
+	} else {
+		/* charger unplug, already handled */
+		pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
+	}
 }
 
 void pwr_charge_start_req(void)
 {
 	switch (pwr_ctrl->state) {
 	case FCHG_STATE_NO_EXT_PWR:
+	case FCHG_STATE_PWR_PLUG_TIMER:
 		rvf_send_trace("Cannot charge without a power source", 36,
 				NULL_PARAM, RV_TRACE_LEVEL_ERROR, FCHG_USE_ID);
 		return;
@@ -517,7 +514,6 @@
 			return;
 		}
 		/* FALL THRU */
-	case FCHG_STATE_PWR_PLUG_TIMER:
 	case FCHG_STATE_READY_TO_CHARGE:
 	case FCHG_STATE_READY_TO_RECHARGE:
 	case FCHG_STATE_RECHARGE_TIMER:
@@ -545,10 +541,10 @@
 {
 	switch (pwr_ctrl->state) {
 	case FCHG_STATE_NO_EXT_PWR:
+	case FCHG_STATE_PWR_PLUG_TIMER:
 	case FCHG_STATE_NO_CHARGING:
 		/* nothing to do */
 		return;
-	case FCHG_STATE_PWR_PLUG_TIMER:
 	case FCHG_STATE_READY_TO_CHARGE:
 	case FCHG_STATE_READY_TO_RECHARGE:
 	case FCHG_STATE_I2V_CAL_1: