annotate src/cs/drivers/drv_app/fchg/fchg_process.c @ 335:6c0659acf93e

FCHG: implemented functions for user-initiated charge start and stop
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 14 Dec 2017 20:41:18 +0000
parents d583a1f5bd6a
children 2f41f7883261
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * In this module we are going to implement the main process functions
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * for FCHG.
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include "fchg/fchg_env.h"
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include "fchg/fchg_func_i.h"
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include "rv/rv_general.h"
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include "rvf/rvf_api.h"
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include "rvm/rvm_use_id_list.h"
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include "abb/abb.h"
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <string.h>
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <stdio.h>
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 extern UINT16 madc_vbat_2_physical(UINT16 adc_val);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 void pwr_init_discharge(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 {
333
8a90038c0173 FCHG: store discharge state as threshold index rather than percentage
Mychaela Falconia <falcon@freecalypso.org>
parents: 331
diff changeset
19 pwr_ctrl->curr_disch_thresh = 0;
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 static void handle_discharge(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 UINT16 i;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 char trace[64];
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 /* first we need to find the current threshold we are at */
333
8a90038c0173 FCHG: store discharge state as threshold index rather than percentage
Mychaela Falconia <falcon@freecalypso.org>
parents: 331
diff changeset
28 i = pwr_ctrl->curr_disch_thresh;
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 /* is there one below? */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 if (++i == pwr_ctrl->nb_thresholds)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 /* are we crossing it? */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (pwr_ctrl->batt_mv >= pwr_ctrl->batt_thresholds[i].bat_voltage)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 /* yes, we crossed it - see if we fell even further down */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 while (i < pwr_ctrl->nb_thresholds &&
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 pwr_ctrl->batt_mv < pwr_ctrl->batt_thresholds[i].bat_voltage)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 i++;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 /* the last one was it */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 i--;
333
8a90038c0173 FCHG: store discharge state as threshold index rather than percentage
Mychaela Falconia <falcon@freecalypso.org>
parents: 331
diff changeset
41 pwr_ctrl->curr_disch_thresh = i;
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 sprintf(trace, "Battery fell through %u%% mark",
333
8a90038c0173 FCHG: store discharge state as threshold index rather than percentage
Mychaela Falconia <falcon@freecalypso.org>
parents: 331
diff changeset
43 pwr_ctrl->batt_thresholds[i].remain_capa);
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 rvf_send_trace(trace, strlen(trace), NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 RV_TRACE_LEVEL_WARNING, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 static void charge_progress_trace(char *mode, UINT16 ichg)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 char trace[64];
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 sprintf(trace, "%s charging: Vbat=%u Ichg=%u i2v=%u", mode,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 pwr_ctrl->batt_mv, ichg, pwr_ctrl->i2v_offset);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 rvf_send_trace(trace, strlen(trace), NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 static void start_i2v_cal(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 UINT16 bciconf;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 rvf_send_trace("Calibrating i2v offset", 22, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 pwr_ctrl->state = FCHG_STATE_I2V_CAL_2;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 bciconf = ABB_Read_Register_on_page(PAGE1, BCICONF);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 bciconf &= 0x3E0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 bciconf |= pwr_ctrl->config.bciconf;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 ABB_Write_Register_on_page(PAGE1, BCICONF, bciconf);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 /*
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 * Set the CHDISPA bit and start the zero calibration routine
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 * of the I to V converter
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0010);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0019);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 static void start_ci_charging(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 rvf_send_trace("Start CI charging", 17, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 pwr_ctrl->state = FCHG_STATE_CI_CHARGING;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 /* Select constant current charging. The charger is disabled */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0002);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 /* Program the DAC with the constant current value */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 ABB_Write_Register_on_page(PAGE0, CHGREG,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 pwr_ctrl->config.ci_current + pwr_ctrl->i2v_offset);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 /* Enable the charger */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0003);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 static void start_cv_charging(UINT16 code)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 rvf_send_trace("Start CV charging", 17, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 pwr_ctrl->state = FCHG_STATE_CV_CHARGING;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 /* Select constant voltage charging. The charger is disabled */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 /* figure out the DAC code */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 while (code &&
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 madc_vbat_2_physical(code) >= pwr_ctrl->config.charge_to_mv)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 code--;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 code++;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 rvf_send_trace("Voltage (DAC code) ", 19, code,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 /* Program the DAC with the constant voltage value */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 ABB_Write_Register_on_page(PAGE0, CHGREG, code);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 /* Enable the charger */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0001);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 static void start_charge_condition_met(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 rvf_send_trace("Charge start condition met", 26, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 if (pwr_ctrl->config.bciconf)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 start_i2v_cal();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 else {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 pwr_ctrl->i2v_offset = 0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 start_ci_charging();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 static int overvoltage_end_charge_check(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 if (pwr_ctrl->batt_mv < pwr_ctrl->config.overvoltage)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 return 0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 rvf_send_trace("Stopping charge by overvoltage condition", 40,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 pwr_init_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 return 1;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 void pwr_process_adc(struct pwr_adc_ind_s *msg)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 pwr_ctrl->batt_mv = madc_vbat_2_physical(msg->data[0]);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 switch (pwr_ctrl->state) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 case FCHG_STATE_NO_EXT_PWR:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 case FCHG_STATE_PWR_PLUG_TIMER:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 case FCHG_STATE_NO_CHARGING:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 handle_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 case FCHG_STATE_READY_TO_CHARGE:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 handle_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 if (pwr_ctrl->batt_mv >= pwr_ctrl->config.start_thresh)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152 start_charge_condition_met();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154 case FCHG_STATE_READY_TO_RECHARGE:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 handle_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 if (pwr_ctrl->batt_mv >= pwr_ctrl->config.restart_thresh)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161 start_charge_condition_met();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163 case FCHG_STATE_I2V_CAL_1:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 if (pwr_ctrl->config.bciconf)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 start_i2v_cal();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 else {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 pwr_ctrl->i2v_offset = 0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 start_ci_charging();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 case FCHG_STATE_I2V_CAL_2:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 pwr_ctrl->i2v_offset = msg->data[2];
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 rvf_send_trace("i2v offset (MADC code) ", 23,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 pwr_ctrl->i2v_offset,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 start_ci_charging();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 case FCHG_STATE_CI_CHARGING:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 charge_progress_trace("CI", msg->data[2]);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194 if (overvoltage_end_charge_check())
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
196 if (pwr_ctrl->batt_mv >= pwr_ctrl->config.charge_to_mv)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
197 start_cv_charging(msg->data[0]);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
198 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
199 case FCHG_STATE_CV_CHARGING:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
200 charge_progress_trace("CV", msg->data[2]);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
201 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
202 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
203 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
204 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
205 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 if (overvoltage_end_charge_check())
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
207 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208 if (msg->data[2] >=
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209 pwr_ctrl->config.end_current + pwr_ctrl->i2v_offset)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 rvf_send_trace("Stopping charge by low current condition", 40,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
212 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
213 FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
214 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
215 pwr_init_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
216 pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218 default:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 rvf_send_trace("Invalid state in pwr_process_adc()", 32,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221 FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
222 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223 }
334
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
224
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
225 void pwr_handle_timer(void)
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
226 {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
227 if (pwr_ctrl->state != FCHG_STATE_PWR_PLUG_TIMER)
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
228 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
229 rvf_send_trace("Timer expired, ready to charge", 30, NULL_PARAM,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
230 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
231 pwr_ctrl->state = FCHG_STATE_READY_TO_CHARGE;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
232 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
233
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
234 void pwr_charger_plug(void)
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
235 {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
236 if (pwr_ctrl->state != FCHG_STATE_NO_EXT_PWR) {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
237 rvf_send_trace("Charger plug event in unexpected state", 38,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
238 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
239 FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
240 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
241 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
242 if (!pwr_ctrl->config_present) {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
243 rvf_send_trace(
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
244 "Charger plugged in, but no config: won't charge", 47,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
245 NULL_PARAM, RV_TRACE_LEVEL_ERROR, FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
246 pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
247 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
248 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
249 if (pwr_ctrl->config.start_delay) {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
250 rvf_send_trace("Charger plug, starting timer", 28, NULL_PARAM,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
251 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
252 rvf_start_timer(FCHG_TIMER,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
253 RVF_MS_TO_TICKS(pwr_ctrl->config.start_delay),
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
254 FALSE);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
255 pwr_ctrl->state = FCHG_STATE_PWR_PLUG_TIMER;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
256 } else {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
257 rvf_send_trace("Charger plug, ready to charge", 29, NULL_PARAM,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
258 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
259 pwr_ctrl->state = FCHG_STATE_READY_TO_CHARGE;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
260 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
261 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
262
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
263 void pwr_charger_unplug(void)
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
264 {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
265 switch (pwr_ctrl->state) {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
266 case FCHG_STATE_NO_EXT_PWR:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
267 rvf_send_trace("Charger unplug, already handled", 31,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
268 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
269 FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
270 /* nothing to do */
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
271 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
272 case FCHG_STATE_PWR_PLUG_TIMER:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
273 case FCHG_STATE_READY_TO_CHARGE:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
274 case FCHG_STATE_READY_TO_RECHARGE:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
275 case FCHG_STATE_I2V_CAL_1:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
276 case FCHG_STATE_NO_CHARGING:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
277 rvf_send_trace("Charger unplug", 14, NULL_PARAM,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
278 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
279 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
280 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
281 case FCHG_STATE_I2V_CAL_2:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
282 case FCHG_STATE_CI_CHARGING:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
283 case FCHG_STATE_CV_CHARGING:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
284 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
285 rvf_send_trace("Charger unplug, charging stopped", 32,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
286 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
287 FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
288 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
289 pwr_init_discharge();
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
290 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
291 default:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
292 rvf_send_trace("Invalid state in pwr_charger_unplug()", 35,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
293 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
294 FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
295 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
296 }
335
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
297
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
298 void pwr_charge_start_req(void)
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
299 {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
300 switch (pwr_ctrl->state) {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
301 case FCHG_STATE_NO_EXT_PWR:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
302 rvf_send_trace("Cannot charge without a power source", 36,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
303 NULL_PARAM, RV_TRACE_LEVEL_ERROR, FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
304 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
305 case FCHG_STATE_NO_CHARGING:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
306 if (!pwr_ctrl->config_present) {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
307 rvf_send_trace("No config set, cannot charge", 28,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
308 NULL_PARAM, RV_TRACE_LEVEL_ERROR,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
309 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
310 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
311 }
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
312 /* FALL THRU */
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
313 case FCHG_STATE_PWR_PLUG_TIMER:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
314 case FCHG_STATE_READY_TO_CHARGE:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
315 case FCHG_STATE_READY_TO_RECHARGE:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
316 rvf_send_trace("Starting charge on user request", 31,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
317 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
318 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
319 pwr_ctrl->state = FCHG_STATE_I2V_CAL_1;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
320 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
321 case FCHG_STATE_I2V_CAL_1:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
322 case FCHG_STATE_I2V_CAL_2:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
323 case FCHG_STATE_CI_CHARGING:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
324 case FCHG_STATE_CV_CHARGING:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
325 rvf_send_trace(
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
326 "Charging already in progress, start request ignored",
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
327 51, NULL_PARAM, RV_TRACE_LEVEL_WARNING, FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
328 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
329 default:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
330 rvf_send_trace("Invalid state in pwr_charge_start_req()", 37,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
331 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
332 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
333 }
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
334 }
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
335
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
336 void pwr_charge_stop_req(void)
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
337 {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
338 switch (pwr_ctrl->state) {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
339 case FCHG_STATE_NO_EXT_PWR:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
340 case FCHG_STATE_NO_CHARGING:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
341 /* nothing to do */
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
342 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
343 case FCHG_STATE_PWR_PLUG_TIMER:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
344 case FCHG_STATE_READY_TO_CHARGE:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
345 case FCHG_STATE_READY_TO_RECHARGE:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
346 case FCHG_STATE_I2V_CAL_1:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
347 rvf_send_trace("Charging disabled by user request", 33,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
348 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
349 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
350 pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
351 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
352 case FCHG_STATE_I2V_CAL_2:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
353 case FCHG_STATE_CI_CHARGING:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
354 case FCHG_STATE_CV_CHARGING:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
355 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
356 rvf_send_trace("Charging stopped by user request", 32,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
357 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
358 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
359 pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
360 pwr_init_discharge();
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
361 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
362 default:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
363 rvf_send_trace("Invalid state in pwr_charge_stop_req()", 36,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
364 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
365 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
366 }
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
367 }