annotate src/cs/drivers/drv_app/fchg/fchg_process.c @ 331:8166b0afcf8c

FCHG: main ADC process implemented
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 14 Dec 2017 16:22:30 +0000
parents
children 8a90038c0173
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
331
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * In this module we are going to implement the main process functions
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * for FCHG.
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include "fchg/fchg_env.h"
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include "fchg/fchg_func_i.h"
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include "rv/rv_general.h"
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include "rvf/rvf_api.h"
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include "rvm/rvm_use_id_list.h"
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include "abb/abb.h"
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <string.h>
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <stdio.h>
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 extern UINT16 madc_vbat_2_physical(UINT16 adc_val);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 void pwr_init_discharge(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 pwr_ctrl->curr_percent = pwr_ctrl->batt_thresholds[0].remain_capa;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 static void handle_discharge(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 UINT16 i;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 char trace[64];
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 /* first we need to find the current threshold we are at */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 for (i = 0; i < pwr_ctrl->nb_thresholds; i++)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 if (pwr_ctrl->batt_thresholds[i].remain_capa ==
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 pwr_ctrl->curr_percent)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 break;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 /* is there one below? */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 if (++i == pwr_ctrl->nb_thresholds)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 /* are we crossing it? */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 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
37 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 /* 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
39 while (i < pwr_ctrl->nb_thresholds &&
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 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
41 i++;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 /* the last one was it */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 i--;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 pwr_ctrl->curr_percent = pwr_ctrl->batt_thresholds[i].remain_capa;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 sprintf(trace, "Battery fell through %u%% mark",
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 pwr_ctrl->curr_percent);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 rvf_send_trace(trace, strlen(trace), NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 RV_TRACE_LEVEL_WARNING, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 static void charge_progress_trace(char *mode, UINT16 ichg)
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 char trace[64];
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 sprintf(trace, "%s charging: Vbat=%u Ichg=%u i2v=%u", mode,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 pwr_ctrl->batt_mv, ichg, pwr_ctrl->i2v_offset);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 rvf_send_trace(trace, strlen(trace), NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 static void start_i2v_cal(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 UINT16 bciconf;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 rvf_send_trace("Calibrating i2v offset", 22, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 pwr_ctrl->state = FCHG_STATE_I2V_CAL_2;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 bciconf = ABB_Read_Register_on_page(PAGE1, BCICONF);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 bciconf &= 0x3E0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 bciconf |= pwr_ctrl->config.bciconf;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 ABB_Write_Register_on_page(PAGE1, BCICONF, bciconf);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 /*
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 * Set the CHDISPA bit and start the zero calibration routine
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 * of the I to V converter
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0010);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0019);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 static void start_ci_charging(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 rvf_send_trace("Start CI charging", 17, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 pwr_ctrl->state = FCHG_STATE_CI_CHARGING;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 /* Select constant current charging. The charger is disabled */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0002);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 /* Program the DAC with the constant current value */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 ABB_Write_Register_on_page(PAGE0, CHGREG,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 pwr_ctrl->config.ci_current + pwr_ctrl->i2v_offset);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 /* Enable the charger */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0003);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 static void start_cv_charging(UINT16 code)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 rvf_send_trace("Start CV charging", 17, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 pwr_ctrl->state = FCHG_STATE_CV_CHARGING;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 /* Select constant voltage charging. The charger is disabled */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 /* figure out the DAC code */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 while (code &&
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 madc_vbat_2_physical(code) >= pwr_ctrl->config.charge_to_mv)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 code--;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 code++;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 rvf_send_trace("Voltage (DAC code) ", 19, code,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 /* Program the DAC with the constant voltage value */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 ABB_Write_Register_on_page(PAGE0, CHGREG, code);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 /* Enable the charger */
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0x0001);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 static void start_charge_condition_met(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 rvf_send_trace("Charge start condition met", 26, NULL_PARAM,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 if (pwr_ctrl->config.bciconf)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 start_i2v_cal();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 else {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 pwr_ctrl->i2v_offset = 0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 start_ci_charging();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 static int overvoltage_end_charge_check(void)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 if (pwr_ctrl->batt_mv < pwr_ctrl->config.overvoltage)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 return 0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 rvf_send_trace("Stopping charge by overvoltage condition", 40,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 pwr_init_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 return 1;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 void pwr_process_adc(struct pwr_adc_ind_s *msg)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 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
141
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 switch (pwr_ctrl->state) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 case FCHG_STATE_NO_EXT_PWR:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 case FCHG_STATE_PWR_PLUG_TIMER:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 case FCHG_STATE_NO_CHARGING:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 handle_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 case FCHG_STATE_READY_TO_CHARGE:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 handle_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
152 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
153 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
154 if (pwr_ctrl->batt_mv >= pwr_ctrl->config.start_thresh)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
155 start_charge_condition_met();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
156 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
157 case FCHG_STATE_READY_TO_RECHARGE:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
158 handle_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
159 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
160 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
161 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
162 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
163 if (pwr_ctrl->batt_mv >= pwr_ctrl->config.restart_thresh)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
164 start_charge_condition_met();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
165 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
166 case FCHG_STATE_I2V_CAL_1:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
167 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
168 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
169 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
170 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
171 if (pwr_ctrl->config.bciconf)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
172 start_i2v_cal();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
173 else {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
174 pwr_ctrl->i2v_offset = 0;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
175 start_ci_charging();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
176 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
177 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
178 case FCHG_STATE_I2V_CAL_2:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
179 pwr_ctrl->i2v_offset = msg->data[2];
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
180 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
181 rvf_send_trace("i2v offset (MADC code) ", 23,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
182 pwr_ctrl->i2v_offset,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
183 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
184 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
185 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
186 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
187 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
188 start_ci_charging();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
189 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
190 case FCHG_STATE_CI_CHARGING:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
191 charge_progress_trace("CI", msg->data[2]);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
192 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
193 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
194 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
195 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
196 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
197 if (overvoltage_end_charge_check())
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
198 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
199 if (pwr_ctrl->batt_mv >= pwr_ctrl->config.charge_to_mv)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
200 start_cv_charging(msg->data[0]);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
201 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
202 case FCHG_STATE_CV_CHARGING:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
203 charge_progress_trace("CV", msg->data[2]);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
204 if (!(msg->data[9] & CHGPRES)) {
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
205 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
206 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
207 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
208 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
209 if (overvoltage_end_charge_check())
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
210 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
211 if (msg->data[2] >=
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
212 pwr_ctrl->config.end_current + pwr_ctrl->i2v_offset)
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
213 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
214 rvf_send_trace("Stopping charge by low current condition", 40,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
215 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
216 FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
217 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
218 pwr_init_discharge();
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
219 pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
220 return;
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
221 default:
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
222 rvf_send_trace("Invalid state in pwr_process_adc()", 32,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
223 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
224 FCHG_USE_ID);
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
225 }
8166b0afcf8c FCHG: main ADC process implemented
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
226 }