summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJens Arnold <amiconn@rockbox.org>2005-04-10 15:10:44 +0000
committerJens Arnold <amiconn@rockbox.org>2005-04-10 15:10:44 +0000
commit06068b451d80a5949b080d756e082126245da180 (patch)
treeaa14d17034885bded0930b0054ca15713058492c
parent5f8241aa44a2d70532d603b2f83ac543137df442 (diff)
downloadrockbox-06068b451d80a5949b080d756e082126245da180.tar.gz
rockbox-06068b451d80a5949b080d756e082126245da180.zip
Bugfixes: (1) Estimated runtime was wrong for approx. 1 minute after boot or battery type/ capacity change because the recalculation was (only) done in the power thread's main loop. (2) Restored Ondio NiMH discharge table. Code cleanup: (1) Moved adjustment for 8MB mod to runcurrent() where it belongs and made it more precise (only adjust the normal current).
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@6263 a1c6a512-1295-4272-9138-f99709370657
-rw-r--r--firmware/powermgmt.c84
1 files changed, 41 insertions, 43 deletions
diff --git a/firmware/powermgmt.c b/firmware/powermgmt.c
index 319a99fde6..35838f2a49 100644
--- a/firmware/powermgmt.c
+++ b/firmware/powermgmt.c
@@ -110,7 +110,7 @@ static const short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] =
110#elif CONFIG_BATTERY == BATT_3AAA 110#elif CONFIG_BATTERY == BATT_3AAA
111 /* measured values */ 111 /* measured values */
112 { 280, 325, 341, 353, 364, 374, 385, 395, 409, 427, 475 }, /* alkaline */ 112 { 280, 325, 341, 353, 364, 374, 385, 395, 409, 427, 475 }, /* alkaline */
113 { 310, 355, 363, 369, 372, 374, 378, 378, 380, 386, 405 } /* NiMH */ 113 { 310, 355, 363, 369, 372, 374, 376, 378, 380, 386, 405 } /* NiMH */
114#elif CONFIG_BATTERY == BATT_LIPOL1300 114#elif CONFIG_BATTERY == BATT_LIPOL1300
115 { 333, 341, 349, 358, 365, 373, 370, 386, 393, 400, 409 } 115 { 333, 341, 349, 358, 365, 373, 370, 386, 393, 400, 409 }
116#else /* NiMH */ 116#else /* NiMH */
@@ -197,7 +197,8 @@ static long sleeptimer_endtick;
197 197
198static long last_event_tick; 198static long last_event_tick;
199 199
200static void battery_level_update(void); /* forward declaration */ 200static void battery_status_update(void);
201static int runcurrent(void);
201 202
202void reset_poweroff_timer(void) 203void reset_poweroff_timer(void)
203{ 204{
@@ -209,7 +210,7 @@ void set_battery_type(int type)
209{ 210{
210 if (type != battery_type) { 211 if (type != battery_type) {
211 battery_type = type; 212 battery_type = type;
212 battery_level_update(); /* recalculate the battery level */ 213 battery_status_update(); /* recalculate the battery status */
213 } 214 }
214} 215}
215#endif 216#endif
@@ -221,6 +222,7 @@ void set_battery_capacity(int capacity)
221 battery_capacity = BATTERY_CAPACITY_MAX; 222 battery_capacity = BATTERY_CAPACITY_MAX;
222 if (battery_capacity < BATTERY_CAPACITY_MIN) 223 if (battery_capacity < BATTERY_CAPACITY_MIN)
223 battery_capacity = BATTERY_CAPACITY_MIN; 224 battery_capacity = BATTERY_CAPACITY_MIN;
225 battery_status_update(); /* recalculate the battery status */
224} 226}
225 227
226int battery_time(void) 228int battery_time(void)
@@ -231,10 +233,6 @@ int battery_time(void)
231/* Returns battery level in percent */ 233/* Returns battery level in percent */
232int battery_level(void) 234int battery_level(void)
233{ 235{
234#ifdef HAVE_CHARGE_CTRL
235 if ((charge_state == CHARGING) && (battery_percent == 100))
236 return 99;
237#endif
238 return battery_percent; 236 return battery_percent;
239} 237}
240 238
@@ -290,20 +288,23 @@ static int voltage_to_percent(int voltage, const short* table)
290 } 288 }
291} 289}
292 290
293/* update battery level, called once per minute */ 291/* update battery level and estimated runtime, called once per minute or
294static void battery_level_update(void) 292 * when battery capacity / type settings are changed */
293static void battery_status_update(void)
295{ 294{
296 int level; 295 int level;
297 296
298#ifdef HAVE_CHARGE_CTRL 297#ifdef HAVE_CHARGE_CTRL
299 if (charge_state == DISCHARGING) { 298 if (charge_state == DISCHARGING) {
300 level = voltage_to_percent(battery_centivolts, 299 level = voltage_to_percent(battery_centivolts,
301 percent_to_volt_discharge[battery_type]); 300 percent_to_volt_discharge[battery_type]);
302 } 301 }
303 else if (charge_state == CHARGING) { 302 else if (charge_state == CHARGING) {
304 level = voltage_to_percent(battery_centivolts, percent_to_volt_charge); 303 /* battery level is defined to be < 100% until charging is finished */
304 level = MIN(voltage_to_percent(battery_centivolts,
305 percent_to_volt_charge), 99);
305 } 306 }
306 else { /* in trickle charge, the battery is by definition 100% full */ 307 else { /* in topoff/trickle charge, the battery is by definition 100% full */
307 level = 100; 308 level = 100;
308 } 309 }
309#else 310#else
@@ -320,6 +321,21 @@ static void battery_level_update(void)
320 } 321 }
321#endif 322#endif
322 battery_percent = level; 323 battery_percent = level;
324
325 /* calculate estimated remaining running time */
326 /* discharging: remaining running time */
327 /* charging: remaining charging time */
328#ifdef HAVE_CHARGE_CTRL
329 if (charge_state == CHARGING) {
330 powermgmt_est_runningtime_min = (100 - level) * battery_capacity / 100
331 * 60 / (CURRENT_MAX_CHG - runcurrent());
332 }
333 else
334#endif
335 {
336 powermgmt_est_runningtime_min = level * battery_capacity / 100
337 * 60 / runcurrent();
338 }
323} 339}
324 340
325/* 341/*
@@ -400,20 +416,24 @@ static int runcurrent(void)
400{ 416{
401 int current; 417 int current;
402 418
419#if MEM == 8 && !defined(HAVE_MMC)
420 /* assuming 192 kbps, the running time is 22% longer with 8MB */
421 current = (CURRENT_NORMAL*100/122);
422#else
403 current = CURRENT_NORMAL; 423 current = CURRENT_NORMAL;
424#endif /* MEM == 8 */
425
404 if(usb_inserted()) { 426 if(usb_inserted()) {
405 current = CURRENT_USB; 427 current = CURRENT_USB;
406 } 428 }
429
430 if ((backlight_get_timeout() == 1) /* LED always on */
407#ifdef HAVE_CHARGE_CTRL 431#ifdef HAVE_CHARGE_CTRL
408 if ((backlight_get_timeout() == 1) || /* LED always on */ 432 || (charger_inserted() && backlight_get_on_when_charging())
409 (charger_inserted() && backlight_get_on_when_charging())) { 433#endif
410 current += CURRENT_BACKLIGHT; 434 ) {
411 }
412#else
413 if (backlight_get_timeout() == 1) { /* LED always on */
414 current += CURRENT_BACKLIGHT; 435 current += CURRENT_BACKLIGHT;
415 } 436 }
416#endif
417 437
418 return(current); 438 return(current);
419} 439}
@@ -558,30 +578,8 @@ static void power_thread(void)
558 /* insert new value at the start, in centivolts 8-) */ 578 /* insert new value at the start, in centivolts 8-) */
559 power_history[0] = battery_centivolts; 579 power_history[0] = battery_centivolts;
560 580
561 /* update battery level every minute */ 581 /* update battery status every minute */
562 battery_level_update(); 582 battery_status_update();
563
564 /* calculate estimated remaining running time */
565 /* discharging: remaining running time */
566 /* charging: remaining charging time */
567
568 powermgmt_est_runningtime_min = battery_level() *
569 battery_capacity / 100 * 60 / runcurrent();
570#if MEM == 8 /* assuming 192 kbps, the running time is 22% longer with 8MB */
571 powermgmt_est_runningtime_min =
572 powermgmt_est_runningtime_min * 122 / 100;
573#endif /* MEM == 8 */
574
575#ifdef HAVE_CHARGE_CTRL
576 /*
577 * If we are charging, the "runtime" is estimated time till the battery
578 * is recharged.
579 */
580 if (charge_state == CHARGING) {
581 powermgmt_est_runningtime_min = (100 - battery_level()) *
582 battery_capacity / 100 * 60 / (CURRENT_MAX_CHG - runcurrent());
583 }
584#endif /* HAVE_CHARGE_CTRL */
585 583
586#if CONFIG_BATTERY == BATT_LIION2200 584#if CONFIG_BATTERY == BATT_LIION2200
587 /* We use the information from the ADC_EXT_POWER ADC channel, which 585 /* We use the information from the ADC_EXT_POWER ADC channel, which