changeset 256:aec644728174

FCHG BSIM: ETM connection implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 13 May 2021 02:37:14 +0000
parents 1a4413c4eb7e
children 3edd1a600218
files components/fchg src/cs/drivers/drv_app/fchg/bsim_etm_cmd.c src/cs/drivers/drv_app/fchg/bsim_etm_cmd.h src/cs/drivers/drv_app/fchg/bsim_func_i.h src/cs/drivers/drv_app/fchg/bsim_start.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_task.c
diffstat 8 files changed, 222 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/components/fchg	Wed May 12 23:20:23 2021 +0000
+++ b/components/fchg	Thu May 13 02:37:14 2021 +0000
@@ -30,6 +30,15 @@
 CPPFLAGS="$CPPFLAGS -I$SRC/cs/drivers/drv_core/ulpd"
 CPPFLAGS="$CPPFLAGS -I$SRC/cs/drivers/drv_app"
 CPPFLAGS="$CPPFLAGS -I$SRC/cs/services"
+CPPFLAGS="$CPPFLAGS -I$SRC/cs/layer1/audio_cust0"
+CPPFLAGS="$CPPFLAGS -I$SRC/cs/layer1/audio_include"
+CPPFLAGS="$CPPFLAGS -I$SRC/cs/layer1/cust0"
+CPPFLAGS="$CPPFLAGS -I$SRC/cs/layer1/hmacs"
+CPPFLAGS="$CPPFLAGS -I$SRC/cs/layer1/include"
+CPPFLAGS="$CPPFLAGS -I$SRC/cs/layer1/p_include"
+CPPFLAGS="$CPPFLAGS -I$SRC/cs/layer1/tm_include"
+CPPFLAGS="$CPPFLAGS -I$SRC/cs/layer1/tm_cust0"
+CPPFLAGS="$CPPFLAGS -I$SRC/cs/layer1/dyn_dwl_include"
 
 # Source modules
 
@@ -47,3 +56,4 @@
 # Battery simulation mode
 
 cfile_plain $SRCDIR/bsim_start.c
+cfile_plain $SRCDIR/bsim_etm_cmd.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/fchg/bsim_etm_cmd.c	Thu May 13 02:37:14 2021 +0000
@@ -0,0 +1,163 @@
+/*
+ * In this module we are going to implement our handling of
+ * ETM_BSIM command packets sent from a development host.
+ */
+
+#include "fchg/fchg_env.h"
+#include "fchg/fchg_func_i.h"
+#include "fchg/bsim_func_i.h"
+#include "fchg/bsim_etm_cmd.h"
+#include "rv/rv_general.h"
+#include "rvf/rvf_api.h"
+#include "rvm/rvm_use_id_list.h"
+#include "etm/etm.h"
+#include "etm/etm_api.h"
+
+static void bsim_cmd_query(T_ETM_PKT *resp)
+{
+	etm_pkt_put8(resp, pwr_ctrl->state);
+	etm_pkt_put8(resp,
+	pwr_ctrl->batt.percent_thresh[pwr_ctrl->curr_disch_thresh].remain_capa);
+	etm_pkt_put8(resp, pwr_ctrl->bsim.start_enable);
+	resp->status = BSIM_STAT_OK;
+}
+
+static void bsim_cmd_discharge(T_ETM_PKT *resp, UINT8 set_percent)
+{
+	UINT16 i;
+
+	switch (pwr_ctrl->state) {
+	case FCHG_STATE_NO_EXT_PWR:
+	case FCHG_STATE_PWR_PLUG_TIMER:
+	case FCHG_STATE_READY_TO_CHARGE:
+	case FCHG_STATE_READY_TO_RECHARGE:
+	case FCHG_STATE_RECHARGE_TIMER:
+	case FCHG_STATE_NO_CHARGING:
+		break;
+	default:
+		resp->status = BSIM_ERR_WRONG_STATE;
+		return;
+	}
+	for (i = 0; i < pwr_ctrl->nb_percent_thresh; i++) {
+		if (pwr_ctrl->batt.percent_thresh[i].remain_capa ==
+		    set_percent)
+			break;
+	}
+	if (i >= pwr_ctrl->nb_percent_thresh) {
+		resp->status = BSIM_ERR_INV_PERCENT;
+		return;
+	}
+	if (i <= pwr_ctrl->curr_disch_thresh) {
+		resp->status = BSIM_ERR_INV_DISCHARGE;
+		return;
+	}
+	pwr_ctrl->curr_disch_thresh = i;
+	if (pwr_ctrl->event_handler)
+		pwr_ctrl->event_handler(FCHG_EVENT_DISCHARGE);
+	resp->status = BSIM_STAT_OK;
+}
+
+static void bsim_cmd_chg_start(T_ETM_PKT *resp)
+{
+	switch (pwr_ctrl->state) {
+	case FCHG_STATE_READY_TO_CHARGE:
+	case FCHG_STATE_READY_TO_RECHARGE:
+		break;
+	default:
+		resp->status = BSIM_ERR_WRONG_STATE;
+		return;
+	}
+	rvf_send_trace("BSIM: simulated charging start", 30, NULL_PARAM,
+			RV_TRACE_LEVEL_DEBUG_MEDIUM, FCHG_USE_ID);
+	pwr_ctrl->state = FCHG_STATE_I2V_CAL_1;
+	resp->status = BSIM_STAT_OK;
+}
+
+static void bsim_cmd_chg_ci2cv(T_ETM_PKT *resp)
+{
+	if (pwr_ctrl->state != FCHG_STATE_CI_CHARGING) {
+		resp->status = BSIM_ERR_WRONG_STATE;
+		return;
+	}
+	rvf_send_trace("BSIM: simulated charging CI->CV", 31, NULL_PARAM,
+			RV_TRACE_LEVEL_DEBUG_MEDIUM, FCHG_USE_ID);
+	pwr_ctrl->state = FCHG_STATE_CV_CHARGING;
+	resp->status = BSIM_STAT_OK;
+}
+
+static void bsim_cmd_chg_complete(T_ETM_PKT *resp)
+{
+	if (pwr_ctrl->state != FCHG_STATE_CV_CHARGING) {
+		resp->status = BSIM_ERR_WRONG_STATE;
+		return;
+	}
+	rvf_send_trace("BSIM: simulated charging complete", 33, NULL_PARAM,
+			RV_TRACE_LEVEL_DEBUG_MEDIUM, FCHG_USE_ID);
+	pwr_init_discharge();
+	pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
+	if (pwr_ctrl->event_handler)
+		pwr_ctrl->event_handler(FCHG_EVENT_CHARGING_COMPLETE);
+	resp->status = BSIM_STAT_OK;
+}
+
+static void bsim_cmd_set_ichg(T_ETM_PKT *resp, UINT16 ichg)
+{
+	pwr_ctrl->ci_ichg = ichg;
+	pwr_ctrl->ichg_average = ichg;
+	resp->status = BSIM_STAT_OK;
+}
+
+static void bsim_cmd_start_enable(T_ETM_PKT *resp, UINT8 setting)
+{
+	pwr_ctrl->bsim.start_enable = setting;
+	resp->status = BSIM_STAT_OK;
+}
+
+void bsim_process_etm(T_ETM_DATA_READY *msg)
+{
+	T_ETM_PKT *resp;
+	uint8 fid;
+
+	resp = (T_ETM_PKT *) etm_malloc(sizeof(T_ETM_PKT));
+	if (!resp) {
+		rvf_send_trace(
+		    "Unable to respond to ETM_BSIM: etm_malloc() failed", 50,
+			NULL_PARAM, RV_TRACE_LEVEL_ERROR, FCHG_USE_ID);
+		return;
+	}
+	/* init response packet */
+	resp->mid = ETM_BSIM;
+	resp->size = 0;
+	resp->index = 0;
+	/* start processing command */
+	fid = msg->data[0];
+	etm_pkt_put8(resp, fid);
+	/* command dispatch */
+	switch (fid) {
+	case BSIM_CMD_QUERY:
+		bsim_cmd_query(resp);
+		break;
+	case BSIM_CMD_DISCHARGE:
+		bsim_cmd_discharge(resp, etm_get8(msg->data + 1));
+		break;
+	case BSIM_CMD_CHG_START:
+		bsim_cmd_chg_start(resp);
+		break;
+	case BSIM_CMD_CHG_CI2CV:
+		bsim_cmd_chg_ci2cv(resp);
+		break;
+	case BSIM_CMD_CHG_COMPLETE:
+		bsim_cmd_chg_complete(resp);
+		break;
+	case BSIM_CMD_SET_ICHG:
+		bsim_cmd_set_ichg(resp, etm_get16(msg->data + 1));
+		break;
+	case BSIM_CMD_START_ENABLE:
+		bsim_cmd_start_enable(resp, etm_get8(msg->data + 1));
+		break;
+	default:
+		resp->status = BSIM_ERR_BAD_CMD;
+	}
+	etm_pkt_send(resp);
+	etm_free(resp);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/fchg/bsim_etm_cmd.h	Thu May 13 02:37:14 2021 +0000
@@ -0,0 +1,25 @@
+/*
+ * This header file defines ETM commands that can be sent from a
+ * development host to FCHG operating in BSIM mode.
+ */
+
+#ifndef	__BSIM_ETM_CMD_H
+#define	__BSIM_ETM_CMD_H
+
+/* ETM_BSIM command opcodes */
+#define	BSIM_CMD_QUERY		0x00
+#define	BSIM_CMD_DISCHARGE	0x01	/* 1 byte with battery % follows */
+#define	BSIM_CMD_CHG_START	0x02
+#define	BSIM_CMD_CHG_CI2CV	0x03
+#define	BSIM_CMD_CHG_COMPLETE	0x04
+#define	BSIM_CMD_SET_ICHG	0x05	/* 2 bytes of Ichg value follow */
+#define	BSIM_CMD_START_ENABLE	0x06	/* 1 byte with flag follows */
+
+/* status response codes */
+#define	BSIM_STAT_OK		0x00
+#define	BSIM_ERR_BAD_CMD	0x02
+#define	BSIM_ERR_WRONG_STATE	0x03
+#define	BSIM_ERR_INV_PERCENT	0x04
+#define	BSIM_ERR_INV_DISCHARGE	0x05
+
+#endif	/* include guard */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/cs/drivers/drv_app/fchg/bsim_func_i.h	Thu May 13 02:37:14 2021 +0000
@@ -0,0 +1,13 @@
+/*
+ * Declarations of internal functions live here.
+ */
+
+#ifndef __BSIM_FUNC_I
+#define __BSIM_FUNC_I
+
+#include "etm/etm_api.h"
+
+void bsim_init_percent(void);
+void bsim_process_etm(T_ETM_DATA_READY *msg);
+
+#endif	/* include guard */
--- a/src/cs/drivers/drv_app/fchg/bsim_start.c	Wed May 12 23:20:23 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/bsim_start.c	Thu May 13 02:37:14 2021 +0000
@@ -4,7 +4,7 @@
  */
 
 #include "fchg/fchg_env.h"
-#include "fchg/fchg_func_i.h"
+#include "fchg/bsim_func_i.h"
 #include "rv/rv_general.h"
 #include "rvf/rvf_api.h"
 #include "rvm/rvm_use_id_list.h"
--- a/src/cs/drivers/drv_app/fchg/fchg_func_i.h	Wed May 12 23:20:23 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_func_i.h	Thu May 13 02:37:14 2021 +0000
@@ -21,6 +21,4 @@
 void pwr_charge_start_req(void);
 void pwr_charge_stop_req(void);
 
-void bsim_init_percent(void);
-
 #endif	/* include guard */
--- a/src/cs/drivers/drv_app/fchg/fchg_messages.c	Wed May 12 23:20:23 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_messages.c	Thu May 13 02:37:14 2021 +0000
@@ -4,6 +4,7 @@
 
 #include "fchg/fchg_env.h"
 #include "fchg/fchg_func_i.h"
+#include "fchg/bsim_func_i.h"
 #include "rv/rv_general.h"
 #include "rvf/rvf_api.h"
 #include "rvm/rvm_use_id_list.h"
@@ -26,6 +27,9 @@
 	case PWR_ADC_IND:
 		pwr_process_adc((struct pwr_adc_ind_s *) msg_ptr);
 		return;
+	case ETM_DATA_READY:
+		bsim_process_etm((T_ETM_DATA_READY *) msg_ptr);
+		return;
 	default:
 		rvf_send_trace("FCHG task: Received an unknown message", 38,
 				NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
--- a/src/cs/drivers/drv_app/fchg/fchg_task.c	Wed May 12 23:20:23 2021 +0000
+++ b/src/cs/drivers/drv_app/fchg/fchg_task.c	Thu May 13 02:37:14 2021 +0000
@@ -4,10 +4,13 @@
 
 #include "fchg/fchg_env.h"
 #include "fchg/fchg_func_i.h"
+#include "fchg/bsim_func_i.h"
 #include "rv/rv_general.h"
 #include "rvf/rvf_api.h"
 #include "rvm/rvm_use_id_list.h"
 #include "abb/abb.h"
+#include "etm/etm.h"
+#include "etm/etm_api.h"
 
 static void set_initial_state(void)
 {
@@ -37,9 +40,10 @@
 	set_initial_state();
 	/* just for safety, clear any previous BCI hardware state */
 	ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
-	if (pwr_ctrl->bsim_mode)
+	if (pwr_ctrl->bsim_mode) {
 		bsim_init_percent();
-	else
+		etm_register("BSIM", ETM_BSIM, 0, pwr_ctrl->addr_id, 0);
+	} else
 		pwr_init_discharge();
 
 	/* loop to process messages */