comparison chipsetsw/drivers/drv_app/lcc/lcc_handle_timers.c @ 0:509db1a7b7b8

initial import: leo2moko-r1
author Space Falcon <falcon@ivan.Harhan.ORG>
date Mon, 01 Jun 2015 03:24:05 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:509db1a7b7b8
1 /******************************************************************************
2 * Power Task (pwr)
3 * Design and coding by Svend Kristian Lindholm, skl@ti.com
4 *
5 * Timers
6 *
7 * $Id: pwr_handle_timers.c 1.1 Wed, 20 Aug 2003 10:22:37 +0200 skl $
8 *
9 ******************************************************************************/
10 #ifndef __PWR_HANDLE_TIMERS_C_
11 #define __PWR_HANDLE_TIMERS_C_
12
13 UINT32 rvf_get_tick_count(void);
14 void pwr_check_timers(void);
15 T_RVM_RETURN pwr_create_timer(UINT32 *timer_begin);
16 T_RVM_RETURN pwr_start_timer(UINT32 *timer_begin);
17 void stop_ci_charge(void);
18 void pwr_modulate_off(void);
19 void pwr_send_msg(uint32 msg_id ,T_RVF_ADDR_ID src_addr_id, T_RVF_ADDR_ID dest_addr_id);
20 int pwr_capacity(uint16 Vbat);
21 void mmi_send_msg(struct mmi_info_ind_s *event);
22 void end_charging_house_keeping(void);
23 void cv_charging_house_keeping(void);
24
25 // Timer functions
26
27 UINT32 pwr_timer_elapsed(UINT32 time_begin, UINT32 current_timer)
28 {
29 // return elapsed time in [ms]
30 return (current_timer - time_begin) * 60 / 13;
31 }
32
33 T_RVM_RETURN pwr_start_timer(UINT32 *timer_begin)
34 {
35 ttw(ttr(TTrTimer, "pwr_start_timer(%d)" NL, 0));
36
37 // Start timer (will be checked each time an ADC measurement is received)
38 *timer_begin = rvf_get_tick_count();
39
40 ttw(ttr(TTrTimer, "pwr_start_timer(%d)" NL, 0xFF));
41 return (RV_OK);
42 }
43
44 T_RVM_RETURN pwr_stop_timer(UINT32 *timer_begin)
45 {
46 ttw(ttr(TTrTimer, "pwr_stop_timer(%d)" NL, 0));
47
48 *timer_begin = 0;
49
50 ttw(ttr(TTrTimer, "pwr_stop_timer(%d)" NL, 0xFF));
51 return (RV_OK);
52 }
53
54 void pwr_stop_timers(void)
55 {
56 // Only stop charging related timers - not supervision timers
57
58 pwr_stop_timer(&pwr_ctrl->time_begin_T1);
59 pwr_stop_timer(&pwr_ctrl->time_begin_T2);
60 pwr_stop_timer(&pwr_ctrl->time_begin_T3);
61 pwr_stop_timer(&pwr_ctrl->time_begin_T4);
62 pwr_stop_timer(&pwr_ctrl->time_begin_mod_cycle);
63
64 }
65
66 T_RVM_RETURN pwr_create_timer(UINT32 *timer_begin)
67 {
68 ttw(ttr(TTrTimer, "pwr_create_timer(%d)" NL, 0));
69
70 *timer_begin = 0;
71
72 ttw(ttr(TTrTimer, "pwr_create_timer(%d)" NL, 0xFF));
73 return (RV_OK);
74 }
75
76 T_RV_RET process_pwr_handle_T1_expiration (T_PWR_REQ *request)
77 {
78 ttw(ttr(TTrTimer, "process_pwr_handle_T1_expiration(%d)" NL, 0));
79
80 switch (pwr_ctrl->state) {
81 case SUP :
82 break;
83 case CCI :
84 case CCV :
85 case LCI :
86 case LCV :
87 // Change state
88 pwr_ctrl->state = SUP;
89 end_charging_house_keeping();
90 break;
91 default :
92 {
93 // Exception Handling - Unknown State
94 ttr(TTrFatal, "process_pwr_handle_T1_expiration: Unknown State: %d" NL, pwr_ctrl->state);
95 break;
96 }
97 }
98 ttw(ttr(TTrTimer, "process_pwr_handle_T1_expiration(%d)" NL, 0xFF));
99 pwr_free(request);
100 }
101
102 T_RV_RET process_pwr_handle_T2_expiration (T_PWR_REQ *request)
103 {
104 ttw(ttr(TTrTimer, "process_pwr_handle_T2_expiration(%d)" NL, 0));
105 switch (pwr_ctrl->state) {
106 case SUP :
107 // Another timer did expire prior to the T2 timer - ignore this expiration
108 break;
109 case CCV :
110 case LCV :
111 // Change state
112 pwr_ctrl->state = SUP;
113 end_charging_house_keeping();
114 break;
115 default :
116 {
117 // Exception Handling - Unknown State
118 ttr(TTrFatal, "process_pwr_handle_T2_expiration: Unknown State: %d" NL, pwr_ctrl->state);
119 break;
120 }
121 }
122 pwr_free(request);
123 ttw(ttr(TTrTimer, "process_pwr_handle_T2_expiration(%d)" NL, 0xFF));
124 }
125
126 T_RV_RET process_pwr_handle_T3_expiration (T_PWR_REQ *request)
127 {
128 ttw(ttr(TTrTimer, "process_pwr_handle_T3_expiration(%d)" NL, 0));
129 switch (pwr_ctrl->state) {
130 case SUP :
131 // Stop & reset timers
132 pwr_stop_timers();
133 break;
134 case CCI :
135 case CCV :
136 case LCI :
137 case LCV :
138 break;
139 default :
140 {
141 // Exception Handling - Unknown State
142 ttr(TTrFatal, "process_pwr_handle_T3_expiration: Unknown State: %d" NL, pwr_ctrl->state);
143 break;
144 }
145 }
146 pwr_free(request);
147 ttw(ttr(TTrTimer, "process_pwr_handle_T3_expiration(%d)" NL, 0xFF));
148 }
149
150 T_RV_RET process_pwr_handle_T4_expiration (T_PWR_REQ *request)
151 {
152 ttw(ttr(TTrTimer, "process_pwr_handle_T4_expiration(%d)" NL, 0));
153 switch (pwr_ctrl->state) {
154 case SUP :
155 // Yes - since timing issues can make this timer expire in SUP state
156 // It can take a long time before we know if a charger has been unplugged
157 break;
158 case CCV :
159 case LCV :
160 pwr_stop_timer(&pwr_ctrl->time_begin_T4);
161 pwr_modulate_off();
162 // Wait for duty cycle timer to expire before restarting this timer
163 break;
164 default :
165 {
166 // Exception Handling - Unknown State
167 ttr(TTrFatal, "process_pwr_handle_T4_expiration: Unknown State: %d" NL, pwr_ctrl->state);
168 break;
169 }
170 }
171 pwr_free(request);
172 ttw(ttr(TTrTimer, "process_pwr_handle_T4_expiration(%d)" NL, 0xFF));
173 }
174
175 T_RV_RET process_pwr_handle_mod_cycle_expiration (T_PWR_REQ *request)
176 {
177 ttw(ttr(TTrTimer, "process_pwr_handle_mod_cycle_expiration(%d)" NL, 0));
178 switch (pwr_ctrl->state) {
179 case SUP :
180 // Yes - since timing issues can make this timer expire in SUP state
181 // It can take a long time before we know if a charger has been unplugged
182 break;
183 case CCV :
184 case LCV :
185 // Calculate k value
186 // Start DC timer
187 // Start T4 timer based on k value
188 // Modulation ON
189 cv_charging_house_keeping();
190 break;
191 default :
192 {
193 // Exception Handling - Unknown State
194 ttr(TTrFatal, "process_pwr_handle_mod_cycle_expiration: Unknown State: %d" NL, pwr_ctrl->state);
195 break;
196 }
197 }
198 pwr_free(request);
199 ttw(ttr(TTrTimer, "process_pwr_handle_mod_cycle_expiration(%d)" NL, 0xFF));
200 }
201
202 T_RV_RET process_pwr_handle_mmi_info_expiration(T_PWR_REQ *request)
203 {
204 struct mmi_info_ind_s MMI_Event;
205
206 ttw(ttr(TTrTimer, "process_pwr_handle_mmi_info_expiration(%d)" NL, 0));
207 switch (pwr_ctrl->state) {
208 case SUP :
209 // Send MMI info events according to configured repetition interval
210 if (pwr_ctrl->flag_mmi_registered == 1) {
211 pwr_ctrl->mmi_ptr->header.msg_id = MMI_BAT_SUPERVISION_INFO_IND;
212 pwr_ctrl->mmi_ptr->Vbat = pwr_cfg->data.Vbat_avg_mV;
213 pwr_ctrl->mmi_ptr->Tbat = pwr_cfg->data.Tbat_avg;
214 pwr_ctrl->mmi_ptr->Cbat = pwr_capacity(pwr_cfg->data.Vbat_avg_mV);
215 mmi_send_msg(pwr_ctrl->mmi_ptr);
216 }
217 // Restart timer
218 ttw(ttr(TTrTimerLow,"MMI info timer started(%d)" NL, 0));
219 pwr_start_timer(&pwr_ctrl->time_begin_mmi_rep);
220
221 break;
222 case INI :
223 case CCI :
224 case CCV :
225 case LCI :
226 case LCV :
227 // DON'T send any events when charging or initializing
228 break;
229 default :
230 {
231 // Exception Handling - Unknown State
232 ttr(TTrFatal, "process_pwr_handle_mmi_info_expiration: Unknown State: %d" NL, pwr_ctrl->state);
233 break;
234 }
235 }
236 pwr_free(request);
237 ttw(ttr(TTrTimer, "process_pwr_handle_mmi_info_expiration(%d)" NL, 0xFF));
238 }
239
240
241 void pwr_check_timers()
242 {
243 UINT32 timer;
244
245 ttw(ttr(TTrTimer, "pwr_check_timers(%d)" NL, 0));
246 // Check timers T1, T2, T3, T4, duty cycle & mmi timer - if expired send timer expired events
247 timer = rvf_get_tick_count();
248
249 // T1
250 if (pwr_ctrl->time_begin_T1 != 0) {
251 // Started - but is it expired?
252 pwr_ctrl->time_elapsed_T1 = pwr_timer_elapsed(pwr_ctrl->time_begin_T1, timer);
253 if (pwr_ctrl->time_elapsed_T1 > pwr_cfg->bat.T1) {
254 // EXPIRED!
255 ttw(ttr(TTrTimer, "T1 expired (%d):" NL, pwr_ctrl->time_elapsed_T1));
256 // 'Stop' timer
257 pwr_ctrl->time_elapsed_T1 = 0;
258 pwr_ctrl->time_begin_T1 = 0;
259 // Send timer expired event
260 pwr_send_msg(TIMER_T1_EXPIRED, pwr_ctrl->addr_id, pwr_ctrl->addr_id);
261 }
262 }
263 // T2
264 if (pwr_ctrl->time_begin_T2 != 0) {
265 // Started - but is it expired?
266 pwr_ctrl->time_elapsed_T2 = pwr_timer_elapsed(pwr_ctrl->time_begin_T2, timer);
267 if (pwr_ctrl->time_elapsed_T2 > pwr_cfg->bat.T2) {
268 // EXPIRED!
269 ttw(ttr(TTrTimer, "T2 expired (%d):" NL, pwr_ctrl->time_elapsed_T2));
270 // 'Stop' timer
271 pwr_ctrl->time_elapsed_T2 = 0;
272 pwr_ctrl->time_begin_T2 = 0;
273 // Send timer expired event
274 pwr_send_msg(TIMER_T2_EXPIRED, pwr_ctrl->addr_id, pwr_ctrl->addr_id);
275 }
276 }
277 // T3
278 if (pwr_ctrl->time_begin_T3 != 0) {
279 // Started - but is it expired?
280 pwr_ctrl->time_elapsed_T3 = pwr_timer_elapsed(pwr_ctrl->time_begin_T3, timer);
281 if (pwr_ctrl->time_elapsed_T3 > pwr_cfg->bat.T3) {
282 // EXPIRED!
283 ttw(ttr(TTrTimer, "T3 expired (%d):" NL, pwr_ctrl->time_elapsed_T3));
284 // 'Stop' timer
285 pwr_ctrl->time_elapsed_T3 = 0;
286 pwr_ctrl->time_begin_T3 = 0;
287 // Send timer expired event
288 pwr_send_msg(TIMER_T3_EXPIRED, pwr_ctrl->addr_id, pwr_ctrl->addr_id);
289 }
290 }
291 // T4
292 if (pwr_ctrl->time_begin_T4 != 0) {
293 // Started - but is it expired?
294 pwr_ctrl->time_elapsed_T4 = pwr_timer_elapsed(pwr_ctrl->time_begin_T4, timer);
295 if (pwr_ctrl->time_elapsed_T4 > pwr_cfg->data.T4) {
296 // EXPIRED!
297 ttw(ttr(TTrTimer, "T4 expired (%d):" NL, pwr_ctrl->time_elapsed_T4));
298 // 'Stop' timer
299 pwr_ctrl->time_elapsed_T4 = 0;
300 pwr_ctrl->time_begin_T4 = 0;
301 // Send timer expired event
302 pwr_send_msg(TIMER_T4_EXPIRED, pwr_ctrl->addr_id, pwr_ctrl->addr_id);
303 }
304 }
305 // Duty cycle timer (Lithium only)
306 if (pwr_ctrl->time_begin_mod_cycle != 0) {
307 // Started - but is it expired?
308 pwr_ctrl->time_elapsed_mod_cycle = pwr_timer_elapsed(pwr_ctrl->time_begin_mod_cycle, timer);
309 if (pwr_ctrl->time_elapsed_mod_cycle > pwr_cfg->common.mod_cycle) {
310 // EXPIRED!
311 ttw(ttr(TTrTimer, "DC expired (%d):" NL, pwr_ctrl->time_elapsed_mod_cycle));
312 // 'Stop' timer
313 pwr_ctrl->time_elapsed_mod_cycle = 0;
314 pwr_ctrl->time_begin_mod_cycle = 0;
315 // Send timer expired event
316 pwr_send_msg(TIMER_MOD_CYCLE_EXPIRED, pwr_ctrl->addr_id, pwr_ctrl->addr_id);
317 }
318 }
319 // MMI repetition timer
320 if (pwr_ctrl->time_begin_mmi_rep != 0) {
321 // Started - but has it expired?
322 pwr_ctrl->time_elapsed_mmi_rep = pwr_timer_elapsed(pwr_ctrl->time_begin_mmi_rep, timer);
323 if (pwr_ctrl->time_elapsed_mmi_rep > pwr_cfg->mmi.repetition) {
324 // EXPIRED!
325 // 'Stop' timer
326 pwr_ctrl->time_elapsed_mmi_rep = 0;
327 pwr_ctrl->time_begin_mmi_rep = 0;
328 ttw(ttr(TTrTimer, "MMI info timer expired (%d):" NL, pwr_ctrl->time_elapsed_mmi_rep));
329 // Send timer expired event
330 pwr_send_msg(TIMER_MMI_INFO_EXPIRED, pwr_ctrl->addr_id, pwr_ctrl->addr_id);
331 }
332 }
333 ttw(ttr(TTrTimer, "pwr_check_timers(%d)" NL, 0xFF));
334
335 }
336 #endif