annotate src/cs/drivers/drv_app/fchg/fchg_process.c @ 364:44795dbadbae

FCHG: bug in CV process caused the control loop to be skipped
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 30 Dec 2017 06:10:48 +0000
parents cce24b9de6db
children 65d53dede3b2
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);
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
16 extern UINT16 madc_vbat_inverse(UINT16 mv);
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 void pwr_init_discharge(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 {
333
8a90038c0173 FCHG: store discharge state as threshold index rather than percentage
Mychaela Falconia <falcon@freecalypso.org>
parents: 331
diff changeset
20 pwr_ctrl->curr_disch_thresh = 0;
331
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
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 static void handle_discharge(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 UINT16 i;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 char trace[64];
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 /* 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
29 i = pwr_ctrl->curr_disch_thresh;
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 /* is there one below? */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 if (++i == pwr_ctrl->nb_thresholds)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 /* are we crossing it? */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 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
35 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 /* 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
37 while (i < pwr_ctrl->nb_thresholds &&
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 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
39 i++;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 /* the last one was it */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 i--;
333
8a90038c0173 FCHG: store discharge state as threshold index rather than percentage
Mychaela Falconia <falcon@freecalypso.org>
parents: 331
diff changeset
42 pwr_ctrl->curr_disch_thresh = i;
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 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
44 pwr_ctrl->batt_thresholds[i].remain_capa);
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 rvf_send_trace(trace, strlen(trace), NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 RV_TRACE_LEVEL_WARNING, FCHG_USE_ID);
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
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 static void start_i2v_cal(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 UINT16 bciconf;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 rvf_send_trace("Calibrating i2v offset", 22, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 pwr_ctrl->state = FCHG_STATE_I2V_CAL_2;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 bciconf = ABB_Read_Register_on_page(PAGE1, BCICONF);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 bciconf &= 0x3E0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 bciconf |= pwr_ctrl->config.bciconf;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 ABB_Write_Register_on_page(PAGE1, BCICONF, bciconf);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 /*
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 * Set the CHDISPA bit and start the zero calibration routine
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 * of the I to V converter
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0010);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0019);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 static void start_ci_charging(void)
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 rvf_send_trace("Start CI charging", 17, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 pwr_ctrl->state = FCHG_STATE_CI_CHARGING;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 /* Select constant current charging. The charger is disabled */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0002);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 /* Program the DAC with the constant current value */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 ABB_Write_Register_on_page(PAGE0, CHGREG,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 pwr_ctrl->config.ci_current + pwr_ctrl->i2v_offset);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 /* Enable the charger */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0003);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
82 static void start_cv_charging(void)
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 {
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
84 UINT16 code;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
85
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 rvf_send_trace("Start CV charging", 17, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 pwr_ctrl->state = FCHG_STATE_CV_CHARGING;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 /* Select constant voltage charging. The charger is disabled */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 /* figure out the DAC code */
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
92 code = madc_vbat_inverse(pwr_ctrl->config.cv_init_set);
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 rvf_send_trace("Voltage (DAC code) ", 19, code,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 /* Program the DAC with the constant voltage value */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 ABB_Write_Register_on_page(PAGE0, CHGREG, code);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 /* Enable the charger */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0001);
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
99 /* CV control loop state init */
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
100 pwr_ctrl->cv_dac_init = code;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
101 pwr_ctrl->cv_dac_curr = code;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
102 pwr_ctrl->cv_high_vbat_count = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
103 pwr_ctrl->cv_low_vbat_count = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
104 /* Ichg averaging state init */
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
105 pwr_ctrl->ichg_fill_level = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
106 pwr_ctrl->ichg_ring_ptr = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
107 pwr_ctrl->ichg_low_count = 0;
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 }
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 static void start_charge_condition_met(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 rvf_send_trace("Charge start condition met", 26, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 if (pwr_ctrl->config.bciconf)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 start_i2v_cal();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 else {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 pwr_ctrl->i2v_offset = 0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 start_ci_charging();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 }
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
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
122 static void ci_progress_trace(UINT16 ichg)
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
123 {
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
124 char trace[64];
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
125
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
126 sprintf(trace, "CI charging: Vbat=%u Ichg=%u i2v=%u",
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
127 pwr_ctrl->batt_mv, ichg, pwr_ctrl->i2v_offset);
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
128 rvf_send_trace(trace, strlen(trace), NULL_PARAM,
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
129 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
130 }
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
131
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
132 static int cv_ichg_process(UINT16 ichg_new)
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
133 {
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
134 UINT16 ichg_clip, ichg_entry;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
135 UINT32 ichg_accum;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
136 UINT16 i;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
137 char trace[64];
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
138
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
139 if (pwr_ctrl->ichg_fill_level < ICHG_AVG_WINDOW)
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
140 pwr_ctrl->ichg_avg_buf[pwr_ctrl->ichg_fill_level++] = ichg_new;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
141 else {
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
142 ichg_clip = pwr_ctrl->ichg_average +
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
143 pwr_ctrl->config.ichg_max_spike;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
144 if (ichg_new > ichg_clip)
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
145 ichg_entry = ichg_clip;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
146 else
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
147 ichg_entry = ichg_new;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
148 pwr_ctrl->ichg_avg_buf[pwr_ctrl->ichg_ring_ptr++] = ichg_entry;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
149 if (pwr_ctrl->ichg_ring_ptr >= ICHG_AVG_WINDOW)
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
150 pwr_ctrl->ichg_ring_ptr = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
151 }
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
152 ichg_accum = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
153 for (i = 0; i < pwr_ctrl->ichg_fill_level; i++)
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
154 ichg_accum += pwr_ctrl->ichg_avg_buf[i];
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
155 pwr_ctrl->ichg_average = ichg_accum / pwr_ctrl->ichg_fill_level;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
156 sprintf(trace, "CV charging: Vbat=%u Ichg=%u Ichg_avg=%u i2v=%u",
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
157 pwr_ctrl->batt_mv, ichg_new, pwr_ctrl->ichg_average,
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
158 pwr_ctrl->i2v_offset);
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
159 rvf_send_trace(trace, strlen(trace), NULL_PARAM,
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
160 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
161 if (pwr_ctrl->ichg_average >
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
162 (pwr_ctrl->config.end_current + pwr_ctrl->i2v_offset)) {
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
163 pwr_ctrl->ichg_low_count = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
164 return 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
165 }
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
166 pwr_ctrl->ichg_low_count++;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
167 if (pwr_ctrl->ichg_low_count < pwr_ctrl->config.ichg_samples_needed)
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
168 return 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
169 rvf_send_trace("Stopping charge by low current condition", 40,
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
170 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
171 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
172 pwr_init_discharge();
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
173 pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
174 return 1;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
175 }
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
176
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 static int overvoltage_end_charge_check(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 if (pwr_ctrl->batt_mv < pwr_ctrl->config.overvoltage)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 return 0;
363
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
181 if (pwr_ctrl->cv_dac_curr !=
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
182 (pwr_ctrl->cv_dac_init - pwr_ctrl->config.cv_dac_max_decr))
364
44795dbadbae FCHG: bug in CV process caused the control loop to be skipped
Mychaela Falconia <falcon@freecalypso.org>
parents: 363
diff changeset
183 return 0;
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 rvf_send_trace("Stopping charge by overvoltage condition", 40,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 pwr_init_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 return 1;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191
363
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
192 static void cv_ctrl_loop_high_check(void)
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
193 {
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
194 if (pwr_ctrl->batt_mv < pwr_ctrl->config.cv_ctrl_loop_high) {
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
195 pwr_ctrl->cv_high_vbat_count = 0;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
196 return;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
197 }
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
198 pwr_ctrl->cv_high_vbat_count++;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
199 if (pwr_ctrl->cv_high_vbat_count < pwr_ctrl->config.cv_samples_needed)
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
200 return;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
201 if (pwr_ctrl->cv_dac_curr ==
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
202 (pwr_ctrl->cv_dac_init - pwr_ctrl->config.cv_dac_max_decr))
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
203 return;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
204 pwr_ctrl->cv_dac_curr--;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
205 ABB_Write_Register_on_page(PAGE0, CHGREG, pwr_ctrl->cv_dac_curr);
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
206 rvf_send_trace("Sub CV DAC", 10, pwr_ctrl->cv_dac_curr,
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
207 RV_TRACE_LEVEL_DEBUG_MEDIUM, FCHG_USE_ID);
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
208 pwr_ctrl->cv_high_vbat_count = 0;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
209 }
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
210
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
211 static void cv_ctrl_loop_low_check(void)
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
212 {
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
213 if (pwr_ctrl->batt_mv >= pwr_ctrl->config.cv_ctrl_loop_low) {
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
214 pwr_ctrl->cv_low_vbat_count = 0;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
215 return;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
216 }
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
217 pwr_ctrl->cv_low_vbat_count++;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
218 if (pwr_ctrl->cv_low_vbat_count < pwr_ctrl->config.cv_samples_needed)
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
219 return;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
220 if (pwr_ctrl->cv_dac_curr ==
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
221 (pwr_ctrl->cv_dac_init + pwr_ctrl->config.cv_dac_max_incr))
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
222 return;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
223 pwr_ctrl->cv_dac_curr++;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
224 ABB_Write_Register_on_page(PAGE0, CHGREG, pwr_ctrl->cv_dac_curr);
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
225 rvf_send_trace("Add CV DAC", 10, pwr_ctrl->cv_dac_curr,
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
226 RV_TRACE_LEVEL_DEBUG_MEDIUM, FCHG_USE_ID);
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
227 pwr_ctrl->cv_low_vbat_count = 0;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
228 }
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
229
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
230 void pwr_process_adc(struct pwr_adc_ind_s *msg)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
231 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
232 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
233
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
234 switch (pwr_ctrl->state) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
235 case FCHG_STATE_NO_EXT_PWR:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
236 case FCHG_STATE_PWR_PLUG_TIMER:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
237 case FCHG_STATE_NO_CHARGING:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
238 handle_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
239 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
240 case FCHG_STATE_READY_TO_CHARGE:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
241 handle_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
242 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
243 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
244 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
245 }
340
2f41f7883261 FCHG: logic error in the charge (re)start condition
Mychaela Falconia <falcon@freecalypso.org>
parents: 335
diff changeset
246 if (pwr_ctrl->batt_mv < pwr_ctrl->config.start_thresh)
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
247 start_charge_condition_met();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
248 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
249 case FCHG_STATE_READY_TO_RECHARGE:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
250 handle_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
251 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
252 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
253 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
254 }
340
2f41f7883261 FCHG: logic error in the charge (re)start condition
Mychaela Falconia <falcon@freecalypso.org>
parents: 335
diff changeset
255 if (pwr_ctrl->batt_mv < pwr_ctrl->config.restart_thresh)
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
256 start_charge_condition_met();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
257 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
258 case FCHG_STATE_I2V_CAL_1:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
259 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
260 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
261 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
262 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
263 if (pwr_ctrl->config.bciconf)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
264 start_i2v_cal();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
265 else {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
266 pwr_ctrl->i2v_offset = 0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
267 start_ci_charging();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
268 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
269 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
270 case FCHG_STATE_I2V_CAL_2:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
271 pwr_ctrl->i2v_offset = msg->data[2];
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
272 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
273 rvf_send_trace("i2v offset (MADC code) ", 23,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
274 pwr_ctrl->i2v_offset,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
275 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
276 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
277 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
278 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
279 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
280 start_ci_charging();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
281 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
282 case FCHG_STATE_CI_CHARGING:
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
283 ci_progress_trace(msg->data[2]);
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
284 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
285 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
286 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
287 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
288 }
360
ce6d71349af0 FCHG: data structure changes in preparation for the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 340
diff changeset
289 if (pwr_ctrl->batt_mv >= pwr_ctrl->config.ci2cv_thresh)
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
290 start_cv_charging();
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
291 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
292 case FCHG_STATE_CV_CHARGING:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
293 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
294 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
295 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
296 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
297 }
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
298 if (cv_ichg_process(msg->data[2]))
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
299 return;
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
300 if (overvoltage_end_charge_check())
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
301 return;
363
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
302 cv_ctrl_loop_high_check();
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
303 cv_ctrl_loop_low_check();
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
304 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
305 default:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
306 rvf_send_trace("Invalid state in pwr_process_adc()", 32,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
307 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
308 FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
309 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
310 }
334
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
311
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
312 void pwr_handle_timer(void)
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
313 {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
314 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
315 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
316 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
317 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
318 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
319 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
320
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
321 void pwr_charger_plug(void)
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
322 {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
323 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
324 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
325 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
326 FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
327 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
328 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
329 if (!pwr_ctrl->config_present) {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
330 rvf_send_trace(
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
331 "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
332 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
333 pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
334 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
335 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
336 if (pwr_ctrl->config.start_delay) {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
337 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
338 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
339 rvf_start_timer(FCHG_TIMER,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
340 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
341 FALSE);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
342 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
343 } else {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
344 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
345 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
346 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
347 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
348 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
349
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
350 void pwr_charger_unplug(void)
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
351 {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
352 switch (pwr_ctrl->state) {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
353 case FCHG_STATE_NO_EXT_PWR:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
354 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
355 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
356 FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
357 /* nothing to do */
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
358 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
359 case FCHG_STATE_PWR_PLUG_TIMER:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
360 case FCHG_STATE_READY_TO_CHARGE:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
361 case FCHG_STATE_READY_TO_RECHARGE:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
362 case FCHG_STATE_I2V_CAL_1:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
363 case FCHG_STATE_NO_CHARGING:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
364 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
365 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
366 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
367 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
368 case FCHG_STATE_I2V_CAL_2:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
369 case FCHG_STATE_CI_CHARGING:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
370 case FCHG_STATE_CV_CHARGING:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
371 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
372 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
373 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
374 FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
375 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
376 pwr_init_discharge();
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
377 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
378 default:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
379 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
380 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
381 FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
382 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
383 }
335
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
384
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
385 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
386 {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
387 switch (pwr_ctrl->state) {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
388 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
389 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
390 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
391 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
392 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
393 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
394 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
395 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
396 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
397 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
398 }
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
399 /* FALL THRU */
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
400 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
401 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
402 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
403 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
404 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
405 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
406 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
407 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
408 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
409 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
410 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
411 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
412 rvf_send_trace(
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
413 "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
414 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
415 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
416 default:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
417 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
418 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
419 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
420 }
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
421 }
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
422
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
423 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
424 {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
425 switch (pwr_ctrl->state) {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
426 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
427 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
428 /* nothing to do */
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
429 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
430 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
431 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
432 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
433 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
434 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
435 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
436 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
437 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
438 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
439 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
440 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
441 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
442 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
443 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
444 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
445 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
446 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
447 pwr_init_discharge();
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
448 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
449 default:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
450 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
451 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
452 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
453 }
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
454 }