annotate src/cs/drivers/drv_app/fchg/fchg_process.c @ 484:a1946652f71c

top README: update for the status of hybrid firmware
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 20 Jun 2018 08:18:48 +0000
parents 65d53dede3b2
children e4d46979846f
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);
369
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
80 /* The total charging time starts now */
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
81 pwr_ctrl->start_time = rvf_get_tick_count();
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 }
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 static void start_cv_charging(void)
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 {
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
86 UINT16 code;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
87
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 rvf_send_trace("Start CV charging", 17, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 pwr_ctrl->state = FCHG_STATE_CV_CHARGING;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 /* Select constant voltage charging. The charger is disabled */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 /* figure out the DAC code */
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
94 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
95 rvf_send_trace("Voltage (DAC code) ", 19, code,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 /* Program the DAC with the constant voltage value */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 ABB_Write_Register_on_page(PAGE0, CHGREG, code);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 /* Enable the charger */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 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
101 /* CV control loop state init */
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
102 pwr_ctrl->cv_dac_init = code;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
103 pwr_ctrl->cv_dac_curr = code;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
104 pwr_ctrl->cv_high_vbat_count = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
105 pwr_ctrl->cv_low_vbat_count = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
106 /* Ichg averaging state init */
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
107 pwr_ctrl->ichg_fill_level = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
108 pwr_ctrl->ichg_ring_ptr = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
109 pwr_ctrl->ichg_low_count = 0;
331
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
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 static void start_charge_condition_met(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 rvf_send_trace("Charge start condition met", 26, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 if (pwr_ctrl->config.bciconf)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 start_i2v_cal();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 else {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 pwr_ctrl->i2v_offset = 0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 start_ci_charging();
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
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
124 static void ci_progress_trace(UINT16 ichg)
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 char trace[64];
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
127
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
128 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
129 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
130 rvf_send_trace(trace, strlen(trace), NULL_PARAM,
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
131 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
132 }
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 static int cv_ichg_process(UINT16 ichg_new)
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
135 {
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
136 UINT16 ichg_clip, ichg_entry;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
137 UINT32 ichg_accum;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
138 UINT16 i;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
139 char trace[64];
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
140
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
141 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
142 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
143 else {
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
144 ichg_clip = pwr_ctrl->ichg_average +
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
145 pwr_ctrl->config.ichg_max_spike;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
146 if (ichg_new > ichg_clip)
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
147 ichg_entry = ichg_clip;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
148 else
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
149 ichg_entry = ichg_new;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
150 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
151 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
152 pwr_ctrl->ichg_ring_ptr = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
153 }
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
154 ichg_accum = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
155 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
156 ichg_accum += pwr_ctrl->ichg_avg_buf[i];
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
157 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
158 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
159 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
160 pwr_ctrl->i2v_offset);
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
161 rvf_send_trace(trace, strlen(trace), NULL_PARAM,
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
162 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
163 if (pwr_ctrl->ichg_average >
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
164 (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
165 pwr_ctrl->ichg_low_count = 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
166 return 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
167 }
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
168 pwr_ctrl->ichg_low_count++;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
169 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
170 return 0;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
171 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
172 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
173 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
174 pwr_init_discharge();
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
175 pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
176 return 1;
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
177 }
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
178
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 static int overvoltage_end_charge_check(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 if (pwr_ctrl->batt_mv < pwr_ctrl->config.overvoltage)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 return 0;
363
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
183 if (pwr_ctrl->cv_dac_curr !=
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
184 (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
185 return 0;
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 rvf_send_trace("Stopping charge by overvoltage condition", 40,
369
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
187 NULL_PARAM, RV_TRACE_LEVEL_WARNING, FCHG_USE_ID);
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 pwr_init_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 return 1;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193
363
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
194 static void cv_ctrl_loop_high_check(void)
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
195 {
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
196 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
197 pwr_ctrl->cv_high_vbat_count = 0;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
198 return;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
199 }
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
200 pwr_ctrl->cv_high_vbat_count++;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
201 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
202 return;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
203 if (pwr_ctrl->cv_dac_curr ==
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
204 (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
205 return;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
206 pwr_ctrl->cv_dac_curr--;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
207 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
208 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
209 RV_TRACE_LEVEL_DEBUG_MEDIUM, FCHG_USE_ID);
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
210 pwr_ctrl->cv_high_vbat_count = 0;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
211 }
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 static void cv_ctrl_loop_low_check(void)
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
214 {
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
215 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
216 pwr_ctrl->cv_low_vbat_count = 0;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
217 return;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
218 }
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
219 pwr_ctrl->cv_low_vbat_count++;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
220 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
221 return;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
222 if (pwr_ctrl->cv_dac_curr ==
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
223 (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
224 return;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
225 pwr_ctrl->cv_dac_curr++;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
226 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
227 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
228 RV_TRACE_LEVEL_DEBUG_MEDIUM, FCHG_USE_ID);
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
229 pwr_ctrl->cv_low_vbat_count = 0;
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
230 }
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
231
369
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
232 static int charging_time_limit_check(void)
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
233 {
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
234 if ((rvf_get_tick_count() - pwr_ctrl->start_time) <
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
235 RVF_SECS_TO_TICKS(pwr_ctrl->config.charge_time_limit))
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
236 return 0;
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
237 rvf_send_trace("Stopping charge by time exceeded condition", 42,
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
238 NULL_PARAM, RV_TRACE_LEVEL_WARNING, FCHG_USE_ID);
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
239 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
240 pwr_init_discharge();
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
241 pwr_ctrl->state = FCHG_STATE_RECHARGE_TIMER;
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
242 pwr_ctrl->start_time = rvf_get_tick_count();
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
243 return 1;
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
244 }
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
245
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
246 void pwr_process_adc(struct pwr_adc_ind_s *msg)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
247 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
248 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
249
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
250 switch (pwr_ctrl->state) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
251 case FCHG_STATE_NO_EXT_PWR:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
252 case FCHG_STATE_PWR_PLUG_TIMER:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
253 case FCHG_STATE_NO_CHARGING:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
254 handle_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
255 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
256 case FCHG_STATE_READY_TO_CHARGE:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
257 handle_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
258 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
259 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
260 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
261 }
340
2f41f7883261 FCHG: logic error in the charge (re)start condition
Mychaela Falconia <falcon@freecalypso.org>
parents: 335
diff changeset
262 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
263 start_charge_condition_met();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
264 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
265 case FCHG_STATE_READY_TO_RECHARGE:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
266 handle_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
267 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
268 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
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 }
340
2f41f7883261 FCHG: logic error in the charge (re)start condition
Mychaela Falconia <falcon@freecalypso.org>
parents: 335
diff changeset
271 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
272 start_charge_condition_met();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
273 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
274 case FCHG_STATE_I2V_CAL_1:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
275 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
276 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
277 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
278 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
279 if (pwr_ctrl->config.bciconf)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
280 start_i2v_cal();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
281 else {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
282 pwr_ctrl->i2v_offset = 0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
283 start_ci_charging();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
284 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
285 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
286 case FCHG_STATE_I2V_CAL_2:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
287 pwr_ctrl->i2v_offset = msg->data[2];
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
288 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
289 rvf_send_trace("i2v offset (MADC code) ", 23,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
290 pwr_ctrl->i2v_offset,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
291 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
292 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
293 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
294 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
295 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
296 start_ci_charging();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
297 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
298 case FCHG_STATE_CI_CHARGING:
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
299 ci_progress_trace(msg->data[2]);
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
300 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
301 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
302 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
303 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
304 }
369
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
305 if (charging_time_limit_check())
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
306 return;
360
ce6d71349af0 FCHG: data structure changes in preparation for the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 340
diff changeset
307 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
308 start_cv_charging();
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
309 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
310 case FCHG_STATE_CV_CHARGING:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
311 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
312 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
313 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
314 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
315 }
362
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
316 if (cv_ichg_process(msg->data[2]))
4c3d05866531 FCHG: beginning of the new version
Mychaela Falconia <falcon@freecalypso.org>
parents: 360
diff changeset
317 return;
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
318 if (overvoltage_end_charge_check())
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
319 return;
369
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
320 if (charging_time_limit_check())
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
321 return;
363
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
322 cv_ctrl_loop_high_check();
cce24b9de6db FCHG: CV control loop implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 362
diff changeset
323 cv_ctrl_loop_low_check();
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
324 return;
369
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
325 case FCHG_STATE_RECHARGE_TIMER:
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
326 handle_discharge();
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
327 if ((rvf_get_tick_count() - pwr_ctrl->start_time) <
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
328 RVF_SECS_TO_TICKS(pwr_ctrl->config.recharge_delay))
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
329 return;
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
330 rvf_send_trace("Restart time met, allowing new charging", 39,
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
331 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
332 FCHG_USE_ID);
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
333 pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
334 return;
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
335 default:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
336 rvf_send_trace("Invalid state in pwr_process_adc()", 32,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
337 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
338 FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
339 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
340 }
334
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
341
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
342 void pwr_handle_timer(void)
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
343 {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
344 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
345 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
346 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
347 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
348 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
349 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
350
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
351 void pwr_charger_plug(void)
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
352 {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
353 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
354 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
355 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
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 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
358 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
359 if (!pwr_ctrl->config_present) {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
360 rvf_send_trace(
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
361 "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
362 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
363 pwr_ctrl->state = FCHG_STATE_NO_CHARGING;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
364 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
365 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
366 if (pwr_ctrl->config.start_delay) {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
367 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
368 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
369 rvf_start_timer(FCHG_TIMER,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
370 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
371 FALSE);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
372 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
373 } else {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
374 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
375 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
376 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
377 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
378 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
379
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
380 void pwr_charger_unplug(void)
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
381 {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
382 switch (pwr_ctrl->state) {
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
383 case FCHG_STATE_NO_EXT_PWR:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
384 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
385 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_LOW,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
386 FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
387 /* nothing to do */
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
388 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
389 case FCHG_STATE_PWR_PLUG_TIMER:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
390 case FCHG_STATE_READY_TO_CHARGE:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
391 case FCHG_STATE_READY_TO_RECHARGE:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
392 case FCHG_STATE_I2V_CAL_1:
369
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
393 case FCHG_STATE_RECHARGE_TIMER:
334
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
394 case FCHG_STATE_NO_CHARGING:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
395 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
396 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
397 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
398 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
399 case FCHG_STATE_I2V_CAL_2:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
400 case FCHG_STATE_CI_CHARGING:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
401 case FCHG_STATE_CV_CHARGING:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
402 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
403 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
404 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
405 FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
406 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
407 pwr_init_discharge();
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
408 return;
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
409 default:
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
410 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
411 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
412 FCHG_USE_ID);
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
413 }
d583a1f5bd6a FCHG: charger plug and unplug functions implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 333
diff changeset
414 }
335
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
415
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
416 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
417 {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
418 switch (pwr_ctrl->state) {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
419 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
420 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
421 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
422 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
423 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
424 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
425 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
426 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
427 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
428 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
429 }
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
430 /* FALL THRU */
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
431 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
432 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
433 case FCHG_STATE_READY_TO_RECHARGE:
369
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
434 case FCHG_STATE_RECHARGE_TIMER:
335
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
435 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
436 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
437 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
438 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
439 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
440 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
441 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
442 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
443 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
444 rvf_send_trace(
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
445 "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
446 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
447 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
448 default:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
449 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
450 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
451 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
452 }
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
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
455 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
456 {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
457 switch (pwr_ctrl->state) {
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
458 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
459 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
460 /* nothing to do */
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
461 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
462 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
463 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
464 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
465 case FCHG_STATE_I2V_CAL_1:
369
65d53dede3b2 FCHG: charging time limit implemented
Mychaela Falconia <falcon@freecalypso.org>
parents: 364
diff changeset
466 case FCHG_STATE_RECHARGE_TIMER:
335
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
467 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
468 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
469 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
470 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
471 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
472 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
473 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
474 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
475 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
476 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
477 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
478 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
479 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
480 pwr_init_discharge();
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
481 return;
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
482 default:
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
483 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
484 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
485 FCHG_USE_ID);
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
486 }
6c0659acf93e FCHG: implemented functions for user-initiated charge start and stop
Mychaela Falconia <falcon@freecalypso.org>
parents: 334
diff changeset
487 }