comparison src/cs/drivers/drv_app/fchg/fchg_process.c @ 236:3c790d29748e

fchg_is_running startup sync flag change: set the flag when the first ADC message has been processed, and not merely when the task is running - this way the flag indicates that meaningful info about the state of the battery is available.
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 02 May 2021 04:25:48 +0000
parents 75067af48bfd
children 305d3bb691a5
comparison
equal deleted inserted replaced
235:ee04ca45053d 236:3c790d29748e
19 #if defined(CONFIG_TARGET_C155) || defined(CONFIG_TARGET_J100) 19 #if defined(CONFIG_TARGET_C155) || defined(CONFIG_TARGET_J100)
20 #define LEDC 0x20 20 #define LEDC 0x20
21 #else 21 #else
22 #define LEDC 0 22 #define LEDC 0
23 #endif 23 #endif
24
25 /*
26 * The following global variable is a system startup synchronization flag:
27 * it indicates that FCHG is running and has processed at least one
28 * ADC message, signifying that meaningful information about the state
29 * of the battery is available.
30 */
31 int fchg_is_running;
24 32
25 void pwr_init_discharge(void) 33 void pwr_init_discharge(void)
26 { 34 {
27 pwr_ctrl->curr_disch_thresh = 0; 35 pwr_ctrl->curr_disch_thresh = 0;
28 } 36 }
267 switch (pwr_ctrl->state) { 275 switch (pwr_ctrl->state) {
268 case FCHG_STATE_NO_EXT_PWR: 276 case FCHG_STATE_NO_EXT_PWR:
269 case FCHG_STATE_PWR_PLUG_TIMER: 277 case FCHG_STATE_PWR_PLUG_TIMER:
270 case FCHG_STATE_NO_CHARGING: 278 case FCHG_STATE_NO_CHARGING:
271 handle_discharge(); 279 handle_discharge();
272 return; 280 break;
273 case FCHG_STATE_READY_TO_CHARGE: 281 case FCHG_STATE_READY_TO_CHARGE:
274 handle_discharge(); 282 handle_discharge();
275 if (!(msg->data[9] & CHGPRES)) { 283 if (!(msg->data[9] & CHGPRES)) {
276 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR; 284 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
277 return; 285 break;
278 } 286 }
279 if (pwr_ctrl->batt_mv < pwr_ctrl->config.start_thresh) 287 if (pwr_ctrl->batt_mv < pwr_ctrl->config.start_thresh)
280 start_charge_condition_met(); 288 start_charge_condition_met();
281 return; 289 break;
282 case FCHG_STATE_READY_TO_RECHARGE: 290 case FCHG_STATE_READY_TO_RECHARGE:
283 handle_discharge(); 291 handle_discharge();
284 if (!(msg->data[9] & CHGPRES)) { 292 if (!(msg->data[9] & CHGPRES)) {
285 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR; 293 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
286 return; 294 break;
287 } 295 }
288 if (pwr_ctrl->batt_mv < pwr_ctrl->config.restart_thresh) 296 if (pwr_ctrl->batt_mv < pwr_ctrl->config.restart_thresh)
289 start_charge_condition_met(); 297 start_charge_condition_met();
290 return; 298 break;
291 case FCHG_STATE_I2V_CAL_1: 299 case FCHG_STATE_I2V_CAL_1:
292 if (!(msg->data[9] & CHGPRES)) { 300 if (!(msg->data[9] & CHGPRES)) {
293 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR; 301 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
294 return; 302 break;
295 } 303 }
296 if (pwr_ctrl->config.bciconf) 304 if (pwr_ctrl->config.bciconf)
297 start_i2v_cal(); 305 start_i2v_cal();
298 else { 306 else {
299 pwr_ctrl->i2v_offset = 0; 307 pwr_ctrl->i2v_offset = 0;
300 start_ci_charging(); 308 start_ci_charging();
301 } 309 }
302 return; 310 break;
303 case FCHG_STATE_I2V_CAL_2: 311 case FCHG_STATE_I2V_CAL_2:
304 pwr_ctrl->i2v_offset = msg->data[2]; 312 pwr_ctrl->i2v_offset = msg->data[2];
305 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0); 313 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
306 rvf_send_trace("i2v offset (MADC code) ", 23, 314 rvf_send_trace("i2v offset (MADC code) ", 23,
307 pwr_ctrl->i2v_offset, 315 pwr_ctrl->i2v_offset,
308 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID); 316 RV_TRACE_LEVEL_DEBUG_LOW, FCHG_USE_ID);
309 if (!(msg->data[9] & CHGPRES)) { 317 if (!(msg->data[9] & CHGPRES)) {
310 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR; 318 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
311 pwr_init_discharge(); 319 pwr_init_discharge();
312 return; 320 break;
313 } 321 }
314 start_ci_charging(); 322 start_ci_charging();
315 return; 323 break;
316 case FCHG_STATE_CI_CHARGING: 324 case FCHG_STATE_CI_CHARGING:
317 ci_progress_trace(msg->data[2]); 325 ci_progress_trace(msg->data[2]);
318 if (!(msg->data[9] & CHGPRES)) { 326 if (!(msg->data[9] & CHGPRES)) {
319 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0); 327 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
320 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR; 328 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
321 pwr_init_discharge(); 329 pwr_init_discharge();
322 return; 330 break;
323 } 331 }
324 if (charging_time_limit_check()) 332 if (charging_time_limit_check())
325 return; 333 break;
326 if (pwr_ctrl->batt_mv >= pwr_ctrl->config.ci2cv_thresh) 334 if (pwr_ctrl->batt_mv >= pwr_ctrl->config.ci2cv_thresh)
327 start_cv_charging(); 335 start_cv_charging();
328 return; 336 break;
329 case FCHG_STATE_CV_CHARGING: 337 case FCHG_STATE_CV_CHARGING:
330 if (!(msg->data[9] & CHGPRES)) { 338 if (!(msg->data[9] & CHGPRES)) {
331 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0); 339 ABB_Write_Register_on_page(PAGE0, BCICTL2, 0);
332 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR; 340 pwr_ctrl->state = FCHG_STATE_NO_EXT_PWR;
333 pwr_init_discharge(); 341 pwr_init_discharge();
334 return; 342 break;
335 } 343 }
336 if (cv_ichg_process(msg->data[2])) 344 if (cv_ichg_process(msg->data[2]))
337 return; 345 break;
338 if (overvoltage_end_charge_check()) 346 if (overvoltage_end_charge_check())
339 return; 347 break;
340 if (charging_time_limit_check()) 348 if (charging_time_limit_check())
341 return; 349 break;
342 cv_ctrl_loop_high_check(); 350 cv_ctrl_loop_high_check();
343 cv_ctrl_loop_low_check(); 351 cv_ctrl_loop_low_check();
344 return; 352 break;
345 case FCHG_STATE_RECHARGE_TIMER: 353 case FCHG_STATE_RECHARGE_TIMER:
346 handle_discharge(); 354 handle_discharge();
347 if ((rvf_get_tick_count() - pwr_ctrl->start_time) < 355 if ((rvf_get_tick_count() - pwr_ctrl->start_time) <
348 RVF_SECS_TO_TICKS(pwr_ctrl->config.recharge_delay)) 356 RVF_SECS_TO_TICKS(pwr_ctrl->config.recharge_delay))
349 return; 357 break;
350 rvf_send_trace("Restart time met, allowing new charging", 39, 358 rvf_send_trace("Restart time met, allowing new charging", 39,
351 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH, 359 NULL_PARAM, RV_TRACE_LEVEL_DEBUG_HIGH,
352 FCHG_USE_ID); 360 FCHG_USE_ID);
353 pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE; 361 pwr_ctrl->state = FCHG_STATE_READY_TO_RECHARGE;
354 return; 362 break;
355 default: 363 default:
356 rvf_send_trace("Invalid state in pwr_process_adc()", 32, 364 rvf_send_trace("Invalid state in pwr_process_adc()", 32,
357 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR, 365 pwr_ctrl->state, RV_TRACE_LEVEL_ERROR,
358 FCHG_USE_ID); 366 FCHG_USE_ID);
359 } 367 }
368 fchg_is_running = 1;
360 } 369 }
361 370
362 void pwr_handle_timer(void) 371 void pwr_handle_timer(void)
363 { 372 {
364 if (pwr_ctrl->state != FCHG_STATE_PWR_PLUG_TIMER) 373 if (pwr_ctrl->state != FCHG_STATE_PWR_PLUG_TIMER)