2002-08-06 10:50:50 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2002-12-14 15:48:08 +00:00
|
|
|
* Copyright (C) 2002 by Heikki Hannikainen, Uwe Freese
|
2002-08-06 10:50:50 +00:00
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "config.h"
|
2005-02-02 22:05:53 +00:00
|
|
|
#include "cpu.h"
|
2002-08-06 10:50:50 +00:00
|
|
|
#include "kernel.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "panic.h"
|
|
|
|
#include "adc.h"
|
|
|
|
#include "string.h"
|
2002-08-06 13:06:41 +00:00
|
|
|
#include "sprintf.h"
|
2002-08-12 12:19:25 +00:00
|
|
|
#include "ata.h"
|
2002-08-06 10:50:50 +00:00
|
|
|
#include "power.h"
|
2002-09-23 11:42:48 +00:00
|
|
|
#include "button.h"
|
|
|
|
#include "ata.h"
|
|
|
|
#include "mpeg.h"
|
2002-09-25 19:25:10 +00:00
|
|
|
#include "usb.h"
|
2002-08-06 10:50:50 +00:00
|
|
|
#include "powermgmt.h"
|
2002-12-14 22:05:01 +00:00
|
|
|
#include "backlight.h"
|
2004-09-28 22:13:26 +00:00
|
|
|
#ifdef CONFIG_TUNER
|
2003-05-03 15:39:40 +00:00
|
|
|
#include "fmradio.h"
|
|
|
|
#endif
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2004-06-22 07:16:31 +00:00
|
|
|
long last_event_tick;
|
|
|
|
|
|
|
|
void reset_poweroff_timer(void)
|
|
|
|
{
|
|
|
|
last_event_tick = current_tick;
|
|
|
|
}
|
|
|
|
|
2002-08-07 11:30:39 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
|
|
|
|
int battery_level(void)
|
|
|
|
{
|
2003-06-04 13:16:34 +00:00
|
|
|
return 75;
|
2002-08-07 11:30:39 +00:00
|
|
|
}
|
|
|
|
|
2002-12-03 14:18:51 +00:00
|
|
|
int battery_time(void)
|
|
|
|
{
|
|
|
|
return 500;
|
|
|
|
}
|
|
|
|
|
2002-08-07 11:30:39 +00:00
|
|
|
bool battery_level_safe(void)
|
|
|
|
{
|
2003-06-04 13:16:34 +00:00
|
|
|
return battery_level() >= 10;
|
2002-08-07 11:30:39 +00:00
|
|
|
}
|
|
|
|
|
2002-09-23 12:21:51 +00:00
|
|
|
void set_poweroff_timeout(int timeout)
|
|
|
|
{
|
|
|
|
(void)timeout;
|
|
|
|
}
|
|
|
|
|
2002-12-17 09:26:51 +00:00
|
|
|
void set_battery_capacity(int capacity)
|
|
|
|
{
|
|
|
|
(void)capacity;
|
|
|
|
}
|
2003-10-17 14:49:00 +00:00
|
|
|
|
|
|
|
void set_car_adapter_mode(bool setting)
|
|
|
|
{
|
|
|
|
(void)setting;
|
|
|
|
}
|
|
|
|
|
2002-08-07 11:30:39 +00:00
|
|
|
#else /* not SIMULATOR */
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2004-10-15 18:18:15 +00:00
|
|
|
int battery_capacity = BATTERY_CAPACITY_MIN; /* only a default value */
|
2003-03-04 05:59:38 +00:00
|
|
|
int battery_level_cached = -1; /* battery level of this minute, updated once
|
|
|
|
per minute */
|
2003-10-17 14:49:00 +00:00
|
|
|
static bool car_adapter_mode_enabled = false;
|
2002-12-18 18:55:50 +00:00
|
|
|
|
2004-08-03 19:22:56 +00:00
|
|
|
static const int poweroff_idle_timeout_value[15] =
|
2002-09-23 11:42:48 +00:00
|
|
|
{
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 30, 45, 60
|
|
|
|
};
|
|
|
|
|
2005-01-30 16:25:46 +00:00
|
|
|
static const short percent_to_volt_decharge[BATTERY_TYPES_COUNT][11] =
|
2003-03-04 05:59:38 +00:00
|
|
|
/* voltages (centivolt) of 0%, 10%, ... 100% when charging disabled */
|
2002-12-03 14:18:51 +00:00
|
|
|
{
|
2004-10-08 17:45:52 +00:00
|
|
|
#if CONFIG_BATTERY == BATT_LIION2200
|
2003-08-21 08:49:57 +00:00
|
|
|
/* measured values */
|
2005-01-30 16:25:46 +00:00
|
|
|
{ 260, 285, 295, 303, 311, 320, 330, 345, 360, 380, 400 }
|
|
|
|
#elif CONFIG_BATTERY == BATT_3AAA
|
|
|
|
/* measured values */
|
|
|
|
{ 280, 325, 341, 353, 364, 374, 385, 395, 409, 427, 475 }, /* alkaline */
|
|
|
|
{ 310, 355, 363, 369, 372, 374, 376, 378, 380, 386, 405 } /* NiMH */
|
2003-01-27 11:27:44 +00:00
|
|
|
#else /* NiMH */
|
2003-03-04 05:59:38 +00:00
|
|
|
/* original values were taken directly after charging, but it should show
|
|
|
|
100% after turning off the device for some hours, too */
|
2005-01-30 16:25:46 +00:00
|
|
|
{ 450, 481, 491, 497, 503, 507, 512, 514, 517, 525, 540 }
|
|
|
|
/* orig. values: ...,528,560 */
|
2003-01-27 11:27:44 +00:00
|
|
|
#endif
|
2002-12-03 14:18:51 +00:00
|
|
|
};
|
|
|
|
|
2002-12-15 18:08:06 +00:00
|
|
|
void set_battery_capacity(int capacity)
|
|
|
|
{
|
2002-12-16 22:58:48 +00:00
|
|
|
battery_capacity = capacity;
|
2002-12-18 18:55:50 +00:00
|
|
|
if (battery_capacity > BATTERY_CAPACITY_MAX)
|
|
|
|
battery_capacity = BATTERY_CAPACITY_MAX;
|
2004-10-14 00:57:23 +00:00
|
|
|
if (battery_capacity < BATTERY_CAPACITY_MIN)
|
|
|
|
battery_capacity = BATTERY_CAPACITY_MIN;
|
2002-12-15 18:08:06 +00:00
|
|
|
}
|
|
|
|
|
2005-01-30 16:25:46 +00:00
|
|
|
#if BATTERY_TYPES_COUNT > 1
|
|
|
|
static int battery_type = 0;
|
|
|
|
|
|
|
|
void set_battery_type(int type)
|
|
|
|
{
|
2005-02-01 18:00:15 +00:00
|
|
|
if (type != battery_type) {
|
|
|
|
battery_type = type;
|
|
|
|
battery_level_cached = -1; /* reset on type change */
|
|
|
|
}
|
2005-01-30 16:25:46 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2004-10-08 17:45:52 +00:00
|
|
|
#if defined(HAVE_CHARGE_CTRL) || CONFIG_BATTERY == BATT_LIION2200
|
2004-02-05 13:44:04 +00:00
|
|
|
int charge_state = 0; /* at the beginning, the
|
|
|
|
charger does nothing */
|
|
|
|
#endif
|
|
|
|
|
2002-12-03 14:33:20 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
2002-12-14 22:05:01 +00:00
|
|
|
|
2003-03-04 05:59:38 +00:00
|
|
|
char power_message[POWER_MESSAGE_LEN] = ""; /* message that's shown in
|
|
|
|
debug menu */
|
|
|
|
char charge_restart_level = CHARGE_RESTART_HI; /* level at which charging
|
|
|
|
starts */
|
|
|
|
int powermgmt_last_cycle_startstop_min = 25; /* how many minutes ago was the
|
|
|
|
charging started or
|
|
|
|
stopped? */
|
|
|
|
int powermgmt_last_cycle_level = 0; /* which level had the
|
|
|
|
batteries at this time? */
|
2002-12-14 22:05:01 +00:00
|
|
|
bool trickle_charge_enabled = true;
|
2003-03-04 05:59:38 +00:00
|
|
|
int trickle_sec = 0; /* how many seconds should the
|
|
|
|
charger be enabled per
|
|
|
|
minute for trickle
|
|
|
|
charging? */
|
2005-01-30 16:25:46 +00:00
|
|
|
static const short percent_to_volt_charge[11] =
|
2004-08-03 19:22:56 +00:00
|
|
|
/* voltages (centivolt) of 0%, 10%, ... 100% when charging enabled */
|
2002-12-03 14:18:51 +00:00
|
|
|
{
|
2003-03-04 05:59:38 +00:00
|
|
|
/* values guessed, see
|
|
|
|
http://www.seattlerobotics.org/encoder/200210/LiIon2.pdf until someone
|
|
|
|
measures voltages over a charging cycle */
|
2003-02-12 22:21:07 +00:00
|
|
|
476, 544, 551, 556, 561, 564, 566, 576, 582, 584, 585 /* NiMH */
|
2002-12-03 14:18:51 +00:00
|
|
|
};
|
2002-12-14 22:05:01 +00:00
|
|
|
|
|
|
|
void enable_trickle_charge(bool on)
|
|
|
|
{
|
|
|
|
trickle_charge_enabled = on;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_CHARGE_CTRL */
|
2002-12-03 14:18:51 +00:00
|
|
|
|
2005-02-06 02:34:17 +00:00
|
|
|
static long power_stack[DEFAULT_STACK_SIZE/sizeof(long)];
|
2004-08-03 19:22:56 +00:00
|
|
|
static const char power_thread_name[] = "power";
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2002-09-23 11:42:48 +00:00
|
|
|
static int poweroff_timeout = 0;
|
2002-09-24 07:50:17 +00:00
|
|
|
static long last_charge_time = 0;
|
2002-12-14 22:20:56 +00:00
|
|
|
int powermgmt_est_runningtime_min = -1;
|
2002-09-23 11:42:48 +00:00
|
|
|
|
2002-12-03 22:41:50 +00:00
|
|
|
static bool sleeptimer_active = false;
|
|
|
|
static unsigned long sleeptimer_endtick;
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
unsigned short power_history[POWER_HISTORY_LEN];
|
|
|
|
|
2002-12-03 14:18:51 +00:00
|
|
|
int battery_time(void)
|
|
|
|
{
|
|
|
|
return powermgmt_est_runningtime_min;
|
|
|
|
}
|
|
|
|
|
2003-03-04 05:59:38 +00:00
|
|
|
/* look into the percent_to_volt_* table and get a realistic battery level
|
|
|
|
percentage */
|
2005-01-30 16:25:46 +00:00
|
|
|
int voltage_to_percent(int voltage, const short* table)
|
2002-12-03 14:18:51 +00:00
|
|
|
{
|
|
|
|
if (voltage <= table[0])
|
|
|
|
return 0;
|
|
|
|
else
|
2003-03-04 05:59:38 +00:00
|
|
|
if (voltage >= table[10])
|
|
|
|
return 100;
|
|
|
|
else {
|
|
|
|
/* search nearest value */
|
|
|
|
int i = 0;
|
|
|
|
while ((i < 10) && (table[i+1] < voltage))
|
|
|
|
i++;
|
|
|
|
/* interpolate linear between the smaller and greater value */
|
|
|
|
return i * 10 /* 10th */
|
|
|
|
+ (voltage - table[i]) *
|
|
|
|
10 / (table[i+1] - table[i]); /* 1th */
|
|
|
|
}
|
2002-12-03 14:18:51 +00:00
|
|
|
}
|
|
|
|
|
2002-12-18 18:55:50 +00:00
|
|
|
/* update battery level, called only once per minute */
|
|
|
|
void battery_level_update(void)
|
2002-08-07 11:30:39 +00:00
|
|
|
{
|
|
|
|
int level = 0;
|
|
|
|
int c = 0;
|
|
|
|
int i;
|
2005-01-30 16:25:46 +00:00
|
|
|
#if BATTERY_TYPES_COUNT == 1 /* single type */
|
|
|
|
const int battery_type = 0;
|
|
|
|
#endif
|
2002-08-07 11:30:39 +00:00
|
|
|
|
2003-11-19 14:14:41 +00:00
|
|
|
/* calculate maximum over last 3 minutes (skip empty samples) */
|
2002-08-07 11:30:39 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
2003-11-19 14:14:41 +00:00
|
|
|
if (power_history[POWER_HISTORY_LEN-1-i] > c)
|
|
|
|
c = power_history[POWER_HISTORY_LEN-1-i];
|
2002-08-07 11:30:39 +00:00
|
|
|
|
|
|
|
if (c)
|
2003-11-19 14:14:41 +00:00
|
|
|
level = c;
|
2002-08-07 11:30:39 +00:00
|
|
|
else /* history was empty, get a fresh sample */
|
|
|
|
level = (adc_read(ADC_UNREG_POWER) * BATTERY_SCALE_FACTOR) / 10000;
|
|
|
|
|
|
|
|
if(level > BATTERY_LEVEL_FULL)
|
|
|
|
level = BATTERY_LEVEL_FULL;
|
|
|
|
|
|
|
|
if(level < BATTERY_LEVEL_EMPTY)
|
|
|
|
level = BATTERY_LEVEL_EMPTY;
|
|
|
|
|
2002-12-03 14:18:51 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
2002-12-18 18:55:50 +00:00
|
|
|
if (charge_state == 0) { /* decharge */
|
2005-01-30 16:25:46 +00:00
|
|
|
level = voltage_to_percent(level,
|
|
|
|
percent_to_volt_decharge[battery_type]);
|
2003-03-04 05:59:38 +00:00
|
|
|
}
|
|
|
|
else if (charge_state == 1) { /* charge */
|
|
|
|
level = voltage_to_percent(level, percent_to_volt_charge);
|
|
|
|
}
|
|
|
|
else {/* in trickle charge, the battery is per definition 100% full */
|
2002-12-18 18:55:50 +00:00
|
|
|
battery_level_cached = level = 100;
|
2002-12-03 14:18:51 +00:00
|
|
|
}
|
|
|
|
#else
|
2005-01-30 16:25:46 +00:00
|
|
|
level = voltage_to_percent(level,
|
|
|
|
percent_to_volt_decharge[battery_type]);
|
2003-03-04 05:59:38 +00:00
|
|
|
/* always use the decharge table */
|
2002-12-03 14:18:51 +00:00
|
|
|
#endif
|
|
|
|
|
2004-10-15 18:52:54 +00:00
|
|
|
#ifndef HAVE_MMC /* this adjustment is only needed for HD based */
|
2002-12-18 18:55:50 +00:00
|
|
|
if (battery_level_cached == -1) { /* first run of this procedure */
|
2003-03-04 05:59:38 +00:00
|
|
|
/* the battery voltage is usually a little lower directly after
|
|
|
|
turning on, because the disk was used heavily raise it by 5 % */
|
2002-12-18 18:55:50 +00:00
|
|
|
battery_level_cached = (level > 95) ? 100 : level + 5;
|
2003-03-04 05:59:38 +00:00
|
|
|
}
|
2004-10-15 18:52:54 +00:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2003-09-22 13:26:24 +00:00
|
|
|
/* the level is allowed to be -1 of the last value when usb not
|
|
|
|
connected and to be -3 of the last value when usb is connected */
|
2002-12-18 18:55:50 +00:00
|
|
|
if (usb_inserted()) {
|
|
|
|
if (level < battery_level_cached - 3)
|
|
|
|
level = battery_level_cached - 3;
|
2003-03-04 05:59:38 +00:00
|
|
|
}
|
|
|
|
else {
|
2002-12-18 18:55:50 +00:00
|
|
|
if (level < battery_level_cached - 1)
|
|
|
|
level = battery_level_cached - 1;
|
|
|
|
}
|
|
|
|
battery_level_cached = level;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns battery level in percent */
|
|
|
|
int battery_level(void)
|
|
|
|
{
|
2003-06-09 21:02:13 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
if ((charge_state==1) && (battery_level_cached==100))
|
|
|
|
return 99;
|
|
|
|
#endif
|
2002-12-18 18:55:50 +00:00
|
|
|
return battery_level_cached;
|
2002-08-07 11:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Tells if the battery level is safe for disk writes */
|
|
|
|
bool battery_level_safe(void)
|
|
|
|
{
|
|
|
|
/* I'm pretty sure we don't want an average over a long time here */
|
2002-08-12 12:19:25 +00:00
|
|
|
if (power_history[POWER_HISTORY_LEN-1])
|
|
|
|
return power_history[POWER_HISTORY_LEN-1] > BATTERY_LEVEL_DANGEROUS;
|
|
|
|
else
|
2005-02-05 10:56:22 +00:00
|
|
|
return adc_read(ADC_UNREG_POWER) > (BATTERY_LEVEL_DANGEROUS * 10000L) /
|
2003-03-04 05:59:38 +00:00
|
|
|
BATTERY_SCALE_FACTOR;
|
2002-08-07 11:30:39 +00:00
|
|
|
}
|
|
|
|
|
2002-09-23 11:42:48 +00:00
|
|
|
void set_poweroff_timeout(int timeout)
|
|
|
|
{
|
|
|
|
poweroff_timeout = timeout;
|
|
|
|
}
|
|
|
|
|
2002-12-03 22:41:50 +00:00
|
|
|
void set_sleep_timer(int seconds)
|
|
|
|
{
|
2003-03-04 05:59:38 +00:00
|
|
|
if(seconds) {
|
2002-12-03 22:41:50 +00:00
|
|
|
sleeptimer_active = true;
|
|
|
|
sleeptimer_endtick = current_tick + seconds * HZ;
|
|
|
|
}
|
2003-03-04 05:59:38 +00:00
|
|
|
else {
|
2002-12-03 22:41:50 +00:00
|
|
|
sleeptimer_active = false;
|
|
|
|
sleeptimer_endtick = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_sleep_timer(void)
|
|
|
|
{
|
|
|
|
if(sleeptimer_active)
|
|
|
|
return (sleeptimer_endtick - current_tick) / HZ;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-09-24 07:50:17 +00:00
|
|
|
/* We shut off in the following cases:
|
2002-09-26 22:34:35 +00:00
|
|
|
1) The unit is idle, not playing music
|
2002-09-24 07:50:17 +00:00
|
|
|
2) The unit is playing music, but is paused
|
|
|
|
|
2002-09-26 22:34:35 +00:00
|
|
|
We do not shut off in the following cases:
|
|
|
|
1) The USB is connected
|
|
|
|
2) The charger is connected
|
|
|
|
3) We are recording, or recording with pause
|
2002-09-24 07:50:17 +00:00
|
|
|
*/
|
2002-09-23 11:42:48 +00:00
|
|
|
static void handle_auto_poweroff(void)
|
|
|
|
{
|
|
|
|
long timeout = poweroff_idle_timeout_value[poweroff_timeout]*60*HZ;
|
2002-09-24 07:50:17 +00:00
|
|
|
int mpeg_stat = mpeg_status();
|
2002-09-26 22:34:35 +00:00
|
|
|
bool charger_is_inserted = charger_inserted();
|
|
|
|
static bool charger_was_inserted = false;
|
2002-09-24 07:50:17 +00:00
|
|
|
|
2002-09-26 22:34:35 +00:00
|
|
|
/* The was_inserted thing prevents the unit to shut down immediately
|
|
|
|
when the charger is extracted */
|
|
|
|
if(charger_is_inserted || charger_was_inserted)
|
|
|
|
{
|
2002-09-24 07:50:17 +00:00
|
|
|
last_charge_time = current_tick;
|
2002-09-26 22:34:35 +00:00
|
|
|
}
|
|
|
|
charger_was_inserted = charger_is_inserted;
|
2002-09-23 11:42:48 +00:00
|
|
|
|
2002-09-24 07:50:17 +00:00
|
|
|
if(timeout &&
|
2004-09-28 22:13:26 +00:00
|
|
|
#ifdef CONFIG_TUNER
|
2004-10-19 08:20:38 +00:00
|
|
|
(radio_get_status() != FMRADIO_PLAYING) &&
|
2003-05-03 15:39:40 +00:00
|
|
|
#endif
|
2002-09-25 19:25:10 +00:00
|
|
|
!usb_inserted() &&
|
2002-09-24 07:50:17 +00:00
|
|
|
(mpeg_stat == 0 ||
|
2003-10-17 14:49:00 +00:00
|
|
|
((mpeg_stat == (MPEG_STATUS_PLAY | MPEG_STATUS_PAUSE)) &&
|
|
|
|
!sleeptimer_active)))
|
2002-09-23 11:42:48 +00:00
|
|
|
{
|
2004-06-22 07:16:31 +00:00
|
|
|
if(TIME_AFTER(current_tick, last_event_tick + timeout) &&
|
2003-11-25 12:00:25 +00:00
|
|
|
TIME_AFTER(current_tick, last_disk_activity + timeout) &&
|
|
|
|
TIME_AFTER(current_tick, last_charge_time + timeout))
|
2003-10-17 14:05:32 +00:00
|
|
|
{
|
2003-11-25 12:00:25 +00:00
|
|
|
power_off();
|
2003-10-17 14:05:32 +00:00
|
|
|
}
|
2002-09-23 11:42:48 +00:00
|
|
|
}
|
2002-12-03 22:41:50 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Handle sleeptimer */
|
2003-10-17 14:05:32 +00:00
|
|
|
if(sleeptimer_active && !usb_inserted())
|
2002-12-03 22:41:50 +00:00
|
|
|
{
|
|
|
|
if(TIME_AFTER(current_tick, sleeptimer_endtick))
|
|
|
|
{
|
2003-10-17 14:05:32 +00:00
|
|
|
mpeg_stop();
|
2002-12-04 12:53:10 +00:00
|
|
|
if(charger_is_inserted)
|
|
|
|
{
|
2003-03-07 14:13:58 +00:00
|
|
|
DEBUGF("Sleep timer timeout. Stopping...\n");
|
2003-10-17 14:05:32 +00:00
|
|
|
set_sleep_timer(0);
|
2004-07-15 05:24:34 +00:00
|
|
|
backlight_off(); /* Nighty, nighty... */
|
2002-12-04 12:53:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGF("Sleep timer timeout. Shutting off...\n");
|
2004-06-14 05:03:49 +00:00
|
|
|
/* Make sure that the disk isn't spinning when
|
|
|
|
we cut the power */
|
|
|
|
while(ata_disk_is_active())
|
|
|
|
sleep(HZ);
|
2002-12-04 12:53:10 +00:00
|
|
|
power_off();
|
|
|
|
}
|
2002-12-03 22:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-09-23 11:42:48 +00:00
|
|
|
}
|
|
|
|
|
2003-10-17 14:49:00 +00:00
|
|
|
void set_car_adapter_mode(bool setting)
|
|
|
|
{
|
|
|
|
car_adapter_mode_enabled = setting;
|
|
|
|
}
|
|
|
|
|
2004-07-14 11:25:14 +00:00
|
|
|
static bool charger_power_is_on;
|
|
|
|
|
2004-10-15 18:18:15 +00:00
|
|
|
#ifdef HAVE_CHARGING
|
2003-10-17 14:49:00 +00:00
|
|
|
static void car_adapter_mode_processing(void)
|
|
|
|
{
|
|
|
|
static bool waiting_to_resume_play = false;
|
|
|
|
static long play_resume_time;
|
|
|
|
|
|
|
|
if (car_adapter_mode_enabled) {
|
|
|
|
|
|
|
|
if (waiting_to_resume_play) {
|
|
|
|
if (TIME_AFTER(current_tick, play_resume_time)) {
|
|
|
|
if (mpeg_status() & MPEG_STATUS_PAUSE) {
|
|
|
|
mpeg_resume();
|
|
|
|
}
|
|
|
|
waiting_to_resume_play = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (charger_power_is_on) {
|
|
|
|
|
|
|
|
/* if external power was turned off */
|
|
|
|
if (!charger_inserted()) {
|
|
|
|
|
|
|
|
charger_power_is_on = false;
|
|
|
|
|
|
|
|
/* if playing */
|
|
|
|
if ((mpeg_status() & MPEG_STATUS_PLAY) &&
|
|
|
|
!(mpeg_status() & MPEG_STATUS_PAUSE)) {
|
|
|
|
mpeg_pause();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* if external power was turned on */
|
|
|
|
if (charger_inserted()) {
|
|
|
|
|
|
|
|
charger_power_is_on = true;
|
|
|
|
|
|
|
|
/* if paused */
|
|
|
|
if (mpeg_status() & MPEG_STATUS_PAUSE) {
|
|
|
|
/* delay resume a bit while the engine is cranking */
|
|
|
|
play_resume_time = current_tick + HZ*5;
|
|
|
|
waiting_to_resume_play = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2004-10-15 18:18:15 +00:00
|
|
|
#endif
|
2003-10-17 14:49:00 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function is called to do the relativly long sleep waits from within the
|
|
|
|
* main power_thread loop while at the same time servicing any other periodic
|
|
|
|
* functions in the power thread which need to be called at a faster periodic
|
|
|
|
* rate than the slow periodic rate of the main power_thread loop
|
|
|
|
*/
|
|
|
|
static void power_thread_sleep(int ticks)
|
|
|
|
{
|
2004-10-15 18:18:15 +00:00
|
|
|
#ifdef HAVE_CHARGING
|
2003-10-17 14:49:00 +00:00
|
|
|
while (ticks > 0) {
|
|
|
|
int small_ticks = MIN(HZ/2, ticks);
|
|
|
|
sleep(small_ticks);
|
|
|
|
ticks -= small_ticks;
|
|
|
|
|
|
|
|
car_adapter_mode_processing();
|
|
|
|
}
|
2004-10-15 18:18:15 +00:00
|
|
|
#else
|
|
|
|
sleep(ticks); /* no fast-processing functions, sleep the whole time */
|
|
|
|
#endif
|
2003-10-17 14:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
/*
|
|
|
|
* This power thread maintains a history of battery voltage
|
2002-08-12 12:19:25 +00:00
|
|
|
* and implements a charging algorithm.
|
2002-12-14 12:10:29 +00:00
|
|
|
* For a complete description of the charging algorithm read
|
|
|
|
* docs/CHARGING_ALGORITHM.
|
2002-08-06 10:50:50 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
static void power_thread(void)
|
|
|
|
{
|
|
|
|
int i;
|
2002-08-12 12:19:25 +00:00
|
|
|
int avg, ok_samples, spin_samples;
|
2002-12-14 12:10:29 +00:00
|
|
|
int current = 0;
|
2004-10-08 17:45:52 +00:00
|
|
|
#if CONFIG_BATTERY == BATT_LIION2200
|
2004-02-05 13:44:04 +00:00
|
|
|
int charging_current;
|
|
|
|
#endif
|
2002-08-06 10:50:50 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
int delta;
|
|
|
|
int charged_time = 0;
|
2003-03-04 05:59:38 +00:00
|
|
|
int charge_max_time_now = 0; /* max. charging duration, calculated at
|
|
|
|
beginning of charging */
|
2002-12-14 12:10:29 +00:00
|
|
|
int charge_pause = 0; /* no charging pause at the beginning */
|
|
|
|
int trickle_time = 0; /* how many minutes trickle charging already? */
|
2002-08-06 10:50:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2003-11-19 14:14:41 +00:00
|
|
|
/* never read power while disk is spinning, unless in USB mode */
|
|
|
|
if (ata_disk_is_active() && !usb_inserted()) {
|
|
|
|
sleep(HZ * 2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2002-08-12 12:19:25 +00:00
|
|
|
/* Make POWER_AVG measurements and calculate an average of that to
|
|
|
|
* reduce the effect of backlights/disk spinning/other variation.
|
2002-08-06 10:50:50 +00:00
|
|
|
*/
|
2002-08-12 12:19:25 +00:00
|
|
|
ok_samples = spin_samples = avg = 0;
|
|
|
|
for (i = 0; i < POWER_AVG_N; i++) {
|
|
|
|
if (ata_disk_is_active()) {
|
|
|
|
if (!ok_samples) {
|
|
|
|
/* if we don't have any good non-disk-spinning samples,
|
|
|
|
* we take a sample anyway in case the disk is going
|
|
|
|
* to spin all the time.
|
|
|
|
*/
|
|
|
|
avg += adc_read(ADC_UNREG_POWER);
|
|
|
|
spin_samples++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (spin_samples) /* throw away disk-spinning samples */
|
|
|
|
spin_samples = avg = 0;
|
|
|
|
avg += adc_read(ADC_UNREG_POWER);
|
|
|
|
ok_samples++;
|
|
|
|
}
|
2003-10-17 14:49:00 +00:00
|
|
|
power_thread_sleep(HZ*POWER_AVG_SLEEP);
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
2002-08-12 12:19:25 +00:00
|
|
|
avg = avg / ((ok_samples) ? ok_samples : spin_samples);
|
2002-12-18 18:55:50 +00:00
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
/* rotate the power history */
|
|
|
|
for (i = 0; i < POWER_HISTORY_LEN-1; i++)
|
|
|
|
power_history[i] = power_history[i+1];
|
|
|
|
|
2002-08-12 12:19:25 +00:00
|
|
|
/* insert new value in the end, in centivolts 8-) */
|
2003-03-04 05:59:38 +00:00
|
|
|
power_history[POWER_HISTORY_LEN-1] =
|
|
|
|
(avg * BATTERY_SCALE_FACTOR) / 10000;
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2003-03-04 05:59:38 +00:00
|
|
|
/* update battery level every minute, ignoring first 15 minutes after
|
|
|
|
start charge/decharge */
|
2002-12-18 18:55:50 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
if ((powermgmt_last_cycle_startstop_min > 25) || (charge_state > 1))
|
|
|
|
#endif
|
|
|
|
battery_level_update();
|
|
|
|
|
2002-12-03 14:18:51 +00:00
|
|
|
/* calculate estimated remaining running time */
|
2002-12-18 18:55:50 +00:00
|
|
|
/* decharging: remaining running time */
|
|
|
|
/* charging: remaining charging time */
|
2002-12-14 12:10:29 +00:00
|
|
|
|
2002-12-03 14:18:51 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
2002-12-14 22:20:56 +00:00
|
|
|
if (charge_state == 1)
|
2003-03-04 05:59:38 +00:00
|
|
|
powermgmt_est_runningtime_min = (100 - battery_level()) *
|
|
|
|
battery_capacity / 100 * 60 / CURRENT_CHARGING;
|
2002-12-14 12:10:29 +00:00
|
|
|
else {
|
2003-01-09 20:16:37 +00:00
|
|
|
current = usb_inserted() ? CURRENT_USB : CURRENT_NORMAL;
|
2003-03-04 05:59:38 +00:00
|
|
|
if ((backlight_get_timeout() == 1) ||
|
|
|
|
(charger_inserted() && backlight_get_on_when_charging()))
|
|
|
|
/* LED always on or LED on when charger connected */
|
2002-12-14 12:10:29 +00:00
|
|
|
current += CURRENT_BACKLIGHT;
|
2003-03-04 05:59:38 +00:00
|
|
|
powermgmt_est_runningtime_min = battery_level() *
|
|
|
|
battery_capacity / 100 * 60 / current;
|
2003-03-03 13:23:11 +00:00
|
|
|
#if MEM == 8 /* assuming 192 kbps, the running time is 22% longer with 8MB */
|
2003-03-04 05:59:38 +00:00
|
|
|
powermgmt_est_runningtime_min =
|
|
|
|
powermgmt_est_runningtime_min * 122 / 100;
|
2003-03-03 13:23:11 +00:00
|
|
|
#endif /* MEM == 8 */
|
2002-12-14 22:05:01 +00:00
|
|
|
}
|
|
|
|
#else
|
2003-01-09 20:16:37 +00:00
|
|
|
current = usb_inserted() ? CURRENT_USB : CURRENT_NORMAL;
|
2002-12-14 22:05:01 +00:00
|
|
|
if (backlight_get_timeout() == 1) /* LED always on */
|
|
|
|
current += CURRENT_BACKLIGHT;
|
2003-03-04 05:59:38 +00:00
|
|
|
powermgmt_est_runningtime_min = battery_level() *
|
|
|
|
battery_capacity / 100 * 60 / current;
|
2003-03-03 13:23:11 +00:00
|
|
|
#if MEM == 8 /* assuming 192 kbps, the running time is 22% longer with 8MB */
|
2003-03-04 05:59:38 +00:00
|
|
|
powermgmt_est_runningtime_min =
|
|
|
|
powermgmt_est_runningtime_min * 122 / 100;
|
2003-03-03 13:23:11 +00:00
|
|
|
#endif /* MEM == 8 */
|
|
|
|
#endif /* HAVE_CHARGE_CONTROL */
|
|
|
|
|
2004-10-08 17:45:52 +00:00
|
|
|
#if CONFIG_BATTERY == BATT_LIION2200
|
2004-02-05 13:44:04 +00:00
|
|
|
/* We use the information from the ADC_EXT_POWER ADC channel, which
|
|
|
|
tells us the charging current from the LTC1734. When DC is
|
|
|
|
connected (either via the external adapter, or via USB), we try
|
|
|
|
to determine if it is actively charging or only maintaining the
|
|
|
|
charge. My tests show that ADC readings is below about 0x80 means
|
|
|
|
that the LTC1734 is only maintaining the charge. */
|
|
|
|
if(charger_inserted()) {
|
|
|
|
charging_current = adc_read(ADC_EXT_POWER);
|
|
|
|
if(charging_current < 0x80) {
|
|
|
|
charge_state = 2; /* Trickle */
|
|
|
|
} else {
|
|
|
|
charge_state = 1; /* Charging */
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
charge_state = 0; /* Not charging */
|
|
|
|
}
|
|
|
|
#else
|
2002-08-06 10:50:50 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
2002-12-03 14:18:51 +00:00
|
|
|
|
|
|
|
if (charge_pause > 0)
|
|
|
|
charge_pause--;
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
if (charger_inserted()) {
|
2002-12-15 18:08:06 +00:00
|
|
|
if (charge_state == 1) {
|
2002-08-06 10:50:50 +00:00
|
|
|
/* charger inserted and enabled */
|
|
|
|
charged_time++;
|
2003-03-04 05:59:38 +00:00
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
|
|
|
"Chg %dm max %dm", charged_time, charge_max_time_now);
|
2002-12-03 14:18:51 +00:00
|
|
|
|
|
|
|
if (charged_time > charge_max_time_now) {
|
2003-03-04 05:59:38 +00:00
|
|
|
DEBUGF("power: charged_time > charge_max_time_now, "
|
|
|
|
"enough!\n");
|
|
|
|
/* have charged too long and deltaV detection did not
|
|
|
|
work! */
|
2002-12-03 14:18:51 +00:00
|
|
|
powermgmt_last_cycle_level = battery_level();
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
2002-08-06 10:50:50 +00:00
|
|
|
charger_enable(false);
|
2003-03-04 05:59:38 +00:00
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
|
|
|
"Chg tmout %d min", charge_max_time_now);
|
|
|
|
/* disable charging for several hours from this point,
|
|
|
|
just to be sure */
|
2002-12-03 14:18:51 +00:00
|
|
|
charge_pause = CHARGE_PAUSE_LEN;
|
2003-03-04 05:59:38 +00:00
|
|
|
/* no trickle charge here, because the charging cycle
|
|
|
|
didn't end the right way */
|
|
|
|
charge_state = 0; /* 0: decharging/charger off, 1: charge,
|
|
|
|
2: top-off, 3: trickle */
|
2002-08-06 10:50:50 +00:00
|
|
|
} else {
|
|
|
|
if (charged_time > CHARGE_MIN_TIME) {
|
2003-03-04 05:59:38 +00:00
|
|
|
/* have charged continuously over the minimum charging
|
|
|
|
time, so we monitor for deltaV going
|
|
|
|
negative. Multiply thingsby 100 to get more
|
|
|
|
accuracy without floating point arithmetic.
|
|
|
|
power_history[] contains centivolts so after
|
|
|
|
multiplying by 100 the deltas are in tenths of
|
|
|
|
millivolts (delta of 5 is 0.0005 V).
|
2002-08-06 10:50:50 +00:00
|
|
|
*/
|
2003-03-04 05:59:38 +00:00
|
|
|
delta =
|
|
|
|
( power_history[POWER_HISTORY_LEN-1] * 100
|
|
|
|
+ power_history[POWER_HISTORY_LEN-2] * 100
|
|
|
|
- power_history[POWER_HISTORY_LEN-1-
|
|
|
|
CHARGE_END_NEGD+1] * 100
|
|
|
|
- power_history[POWER_HISTORY_LEN-1-
|
|
|
|
CHARGE_END_NEGD] * 100 )
|
|
|
|
/ CHARGE_END_NEGD / 2;
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2002-08-12 12:19:25 +00:00
|
|
|
if (delta < -100) { /* delta < -10 mV */
|
2003-03-04 05:59:38 +00:00
|
|
|
DEBUGF("power: short-term negative"
|
|
|
|
" delta, enough!\n");
|
2002-12-03 14:18:51 +00:00
|
|
|
powermgmt_last_cycle_level = battery_level();
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
2002-08-06 10:50:50 +00:00
|
|
|
charger_enable(false);
|
2003-03-04 05:59:38 +00:00
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
|
|
|
"end negd %d %dmin", delta, charged_time);
|
|
|
|
/* disable charging for several hours from this
|
|
|
|
point, just to be sure */
|
2002-12-03 14:18:51 +00:00
|
|
|
charge_pause = CHARGE_PAUSE_LEN;
|
2002-12-14 12:10:29 +00:00
|
|
|
/* enable trickle charging */
|
2002-12-14 22:05:01 +00:00
|
|
|
if (trickle_charge_enabled) {
|
2003-03-04 05:59:38 +00:00
|
|
|
trickle_sec = CURRENT_NORMAL * 60 /
|
|
|
|
CURRENT_CHARGING;
|
|
|
|
/* first guess, maybe consider if LED
|
|
|
|
backlight is on, disk is active,... */
|
2002-12-14 15:48:08 +00:00
|
|
|
trickle_time = 0;
|
2003-03-04 05:59:38 +00:00
|
|
|
charge_state = 2; /* 0: decharging/charger
|
|
|
|
off, 1: charge,
|
|
|
|
2: top-off, 3: trickle */
|
2002-12-16 15:02:23 +00:00
|
|
|
} else {
|
2003-03-04 05:59:38 +00:00
|
|
|
charge_state = 0; /* 0: decharging/charger
|
|
|
|
off, 1: charge,
|
|
|
|
2: top-off, 3: trickle */
|
2002-12-14 15:48:08 +00:00
|
|
|
}
|
2002-08-06 10:50:50 +00:00
|
|
|
} else {
|
2003-03-04 05:59:38 +00:00
|
|
|
/* if we didn't disable the charger in the
|
|
|
|
previous test, check for low positive delta */
|
|
|
|
delta =
|
|
|
|
( power_history[POWER_HISTORY_LEN-1] * 100
|
|
|
|
+ power_history[POWER_HISTORY_LEN-2] * 100
|
|
|
|
- power_history[POWER_HISTORY_LEN-1-
|
|
|
|
CHARGE_END_ZEROD+1] * 100
|
|
|
|
- power_history[POWER_HISTORY_LEN-1-
|
|
|
|
CHARGE_END_ZEROD] * 100 )
|
|
|
|
/ CHARGE_END_ZEROD / 2;
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2002-08-12 12:19:25 +00:00
|
|
|
if (delta < 1) { /* delta < 0.1 mV */
|
2003-03-04 05:59:38 +00:00
|
|
|
DEBUGF("power: long-term small "
|
|
|
|
"positive delta, enough!\n");
|
2002-12-03 14:18:51 +00:00
|
|
|
powermgmt_last_cycle_level = battery_level();
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
2002-08-06 10:50:50 +00:00
|
|
|
charger_enable(false);
|
2003-03-04 05:59:38 +00:00
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
|
|
|
"end lowd %d %dmin",
|
|
|
|
delta, charged_time);
|
|
|
|
/* disable charging for several hours from
|
|
|
|
this point, just to be sure */
|
2002-12-03 14:18:51 +00:00
|
|
|
charge_pause = CHARGE_PAUSE_LEN;
|
2002-12-14 12:10:29 +00:00
|
|
|
/* enable trickle charging */
|
2002-12-14 22:05:01 +00:00
|
|
|
if (trickle_charge_enabled) {
|
2003-03-04 05:59:38 +00:00
|
|
|
trickle_sec =
|
|
|
|
CURRENT_NORMAL * 60 / CURRENT_CHARGING;
|
|
|
|
/* first guess, maybe consider if LED
|
|
|
|
backlight is on, disk is active,... */
|
2002-12-14 15:48:08 +00:00
|
|
|
trickle_time = 0;
|
2003-03-04 05:59:38 +00:00
|
|
|
charge_state = 2;
|
|
|
|
/* 0: decharging/charger off, 1: charge,
|
|
|
|
2: top-off, 3: trickle */
|
2002-12-16 15:02:23 +00:00
|
|
|
} else {
|
2003-03-04 05:59:38 +00:00
|
|
|
charge_state = 0;
|
|
|
|
/* 0: decharging/charger off, 1: charge,
|
|
|
|
2: top-off, 3: trickle */
|
2002-12-14 15:48:08 +00:00
|
|
|
}
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-03-04 05:59:38 +00:00
|
|
|
}
|
|
|
|
else if (charge_state > 1) { /* top off or trickle? */
|
2002-12-15 18:08:06 +00:00
|
|
|
/* adjust trickle charge time */
|
2003-03-04 05:59:38 +00:00
|
|
|
if ( ((charge_state == 2) &&
|
|
|
|
(power_history[POWER_HISTORY_LEN-1] > TOPOFF_VOLTAGE))
|
|
|
|
|| ((charge_state == 3) &&
|
|
|
|
(power_history[POWER_HISTORY_LEN-1] >
|
|
|
|
TRICKLE_VOLTAGE)) ) { /* charging too much */
|
2002-12-15 18:08:06 +00:00
|
|
|
trickle_sec--;
|
2003-03-04 05:59:38 +00:00
|
|
|
}
|
|
|
|
else { /* charging too less */
|
2002-12-15 18:08:06 +00:00
|
|
|
trickle_sec++;
|
|
|
|
}
|
2002-12-14 12:10:29 +00:00
|
|
|
|
2003-03-04 05:59:38 +00:00
|
|
|
if (trickle_sec > 24)
|
|
|
|
trickle_sec = 24;
|
|
|
|
|
|
|
|
if (trickle_sec < 1)
|
|
|
|
trickle_sec = 1;
|
2002-12-15 18:08:06 +00:00
|
|
|
|
|
|
|
/* charge the calculated amount of seconds */
|
|
|
|
charger_enable(true);
|
2003-10-17 14:49:00 +00:00
|
|
|
power_thread_sleep(HZ * trickle_sec);
|
2002-12-15 18:08:06 +00:00
|
|
|
charger_enable(false);
|
|
|
|
|
|
|
|
/* trickle charging long enough? */
|
|
|
|
|
|
|
|
if (trickle_time++ > TRICKLE_MAX_TIME + TOPOFF_MAX_TIME) {
|
2003-03-04 05:59:38 +00:00
|
|
|
trickle_sec = 0; /* show in debug menu that trickle is
|
|
|
|
off */
|
|
|
|
charge_state = 0; /* 0: decharging/charger off, 1: charge,
|
|
|
|
2: top-off, 3: trickle */
|
2002-12-18 18:55:50 +00:00
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
2002-12-15 18:08:06 +00:00
|
|
|
}
|
2002-12-14 15:48:08 +00:00
|
|
|
|
2003-03-04 05:59:38 +00:00
|
|
|
if ((charge_state == 2) &&
|
|
|
|
(trickle_time > TOPOFF_MAX_TIME)) /* change state? */
|
|
|
|
charge_state = 3; /* 0: decharging/charger off, 1: charge,
|
|
|
|
2: top-off, 3: trickle */
|
2002-12-15 18:08:06 +00:00
|
|
|
|
|
|
|
} else { /* charge_state == 0 */
|
|
|
|
|
2003-03-04 05:59:38 +00:00
|
|
|
/* the charger is enabled here only in one case: if it was
|
|
|
|
turned on at boot time (power_init) */
|
2002-12-15 18:08:06 +00:00
|
|
|
/* turn it off now */
|
|
|
|
if (charger_enabled)
|
|
|
|
charger_enable(false);
|
2003-01-19 20:42:09 +00:00
|
|
|
}
|
2002-12-14 12:10:29 +00:00
|
|
|
|
2003-03-04 05:59:38 +00:00
|
|
|
/* Start new charge cycle? This must be possible also in
|
|
|
|
trickle/top-off, because when usb connected, */
|
2003-01-19 20:42:09 +00:00
|
|
|
/* the trickle charge amount may not be enough */
|
|
|
|
|
|
|
|
if ((charge_state == 0) || (charge_state > 1))
|
2002-08-06 10:50:50 +00:00
|
|
|
/* if battery is not full, enable charging */
|
2003-03-04 05:59:38 +00:00
|
|
|
/* make sure charging starts if 1%-lazyness in
|
|
|
|
battery_level_update() is too slow */
|
2002-12-18 18:55:50 +00:00
|
|
|
if ( (battery_level() < charge_restart_level)
|
2003-03-04 05:59:38 +00:00
|
|
|
|| (power_history[POWER_HISTORY_LEN-1] <
|
|
|
|
BATTERY_LEVEL_DANGEROUS)) {
|
2002-12-03 14:18:51 +00:00
|
|
|
if (charge_pause) {
|
2003-03-04 05:59:38 +00:00
|
|
|
DEBUGF("power: batt level < restart level,"
|
|
|
|
" but charge pause, not enabling\n");
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
|
|
|
"chg pause %d min", charge_pause);
|
2002-12-03 14:18:51 +00:00
|
|
|
} else {
|
2003-03-04 05:59:38 +00:00
|
|
|
/* calculate max charge time depending on current
|
|
|
|
battery level */
|
|
|
|
/* take 35% more because some more energy is used for
|
|
|
|
heating up the battery */
|
2002-12-14 22:46:00 +00:00
|
|
|
i = CHARGE_MAX_TIME_1500 * battery_capacity / 1500;
|
2003-03-04 05:59:38 +00:00
|
|
|
charge_max_time_now =
|
|
|
|
i * (100 + 35 - battery_level()) / 100;
|
2002-12-14 21:01:32 +00:00
|
|
|
if (charge_max_time_now > i) {
|
2003-03-04 05:59:38 +00:00
|
|
|
charge_max_time_now = i;
|
2002-12-03 14:18:51 +00:00
|
|
|
}
|
2003-03-04 05:59:38 +00:00
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
|
|
|
"ChgAt %d%% max %dm", battery_level(),
|
|
|
|
charge_max_time_now);
|
|
|
|
|
|
|
|
/* enable the charger after the max time calc is done,
|
|
|
|
because battery_level depends on if the charger is
|
|
|
|
on */
|
|
|
|
DEBUGF("power: charger inserted and battery"
|
|
|
|
" not full, enabling\n");
|
2002-12-03 14:18:51 +00:00
|
|
|
powermgmt_last_cycle_level = battery_level();
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
2002-12-14 12:10:29 +00:00
|
|
|
charged_time = 0;
|
2002-12-03 14:18:51 +00:00
|
|
|
|
|
|
|
charger_enable(true);
|
2003-03-04 05:59:38 +00:00
|
|
|
charge_state = 1; /* 0: decharging/charger off, 1:
|
|
|
|
charge, 2: top-off, 3: trickle */
|
|
|
|
/* clear the power history so that we don't use values
|
|
|
|
before discharge for the long-term delta
|
2002-12-14 15:48:08 +00:00
|
|
|
*/
|
|
|
|
for (i = 0; i < POWER_HISTORY_LEN-1; i++)
|
2003-03-04 05:59:38 +00:00
|
|
|
power_history[i] =
|
|
|
|
power_history[POWER_HISTORY_LEN-1];
|
2002-12-03 14:18:51 +00:00
|
|
|
}
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
2003-01-19 20:42:09 +00:00
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
} else {
|
|
|
|
/* charger not inserted */
|
2002-12-15 18:08:06 +00:00
|
|
|
if (charge_state > 0) {
|
2002-08-06 10:50:50 +00:00
|
|
|
/* charger not inserted but was enabled */
|
|
|
|
DEBUGF("power: charger disconnected, disabling\n");
|
2002-12-03 14:18:51 +00:00
|
|
|
powermgmt_last_cycle_level = battery_level();
|
2002-12-18 18:55:50 +00:00
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
2002-12-15 18:08:06 +00:00
|
|
|
/* show in debug menu that trickle is off */
|
|
|
|
trickle_sec = 0;
|
2002-08-06 10:50:50 +00:00
|
|
|
charger_enable(false);
|
2003-03-04 05:59:38 +00:00
|
|
|
charge_state = 0; /* 0: decharging/charger off, 1: charge, 2:
|
|
|
|
top-off, 3: trickle */
|
2002-08-06 10:50:50 +00:00
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN, "Charger disc");
|
|
|
|
}
|
|
|
|
/* charger not inserted and disabled, so we're discharging */
|
|
|
|
}
|
2002-12-03 14:18:51 +00:00
|
|
|
powermgmt_last_cycle_startstop_min++;
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
#endif /* HAVE_CHARGE_CTRL*/
|
2004-10-08 17:45:52 +00:00
|
|
|
#endif /* # if CONFIG_BATTERY == BATT_LIION2200 */
|
2002-08-06 10:50:50 +00:00
|
|
|
|
|
|
|
/* sleep for roughly a minute */
|
2002-12-14 12:10:29 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
i = 60 - trickle_sec - POWER_AVG_N * POWER_AVG_SLEEP;
|
|
|
|
#else
|
|
|
|
i = 60 - POWER_AVG_N * POWER_AVG_SLEEP;
|
|
|
|
#endif
|
|
|
|
if (i > 0)
|
2003-10-17 14:49:00 +00:00
|
|
|
power_thread_sleep(HZ*(i));
|
2002-09-23 11:42:48 +00:00
|
|
|
|
|
|
|
handle_auto_poweroff();
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-25 08:57:49 +00:00
|
|
|
void powermgmt_init(void)
|
2002-08-06 10:50:50 +00:00
|
|
|
{
|
2003-08-25 08:57:49 +00:00
|
|
|
power_init();
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
/* init history to 0 */
|
|
|
|
memset(power_history, 0x00, sizeof(power_history));
|
2003-11-19 14:14:41 +00:00
|
|
|
|
|
|
|
#if 0
|
2002-08-15 11:32:01 +00:00
|
|
|
/* initialize the history with a single sample to prevent level
|
|
|
|
flickering during the first minute of execution */
|
2003-03-04 05:59:38 +00:00
|
|
|
power_history[POWER_HISTORY_LEN-1] =
|
|
|
|
(adc_read(ADC_UNREG_POWER) * BATTERY_SCALE_FACTOR) / 10000;
|
2002-12-18 18:55:50 +00:00
|
|
|
/* calculate the first battery level */
|
|
|
|
battery_level_update();
|
2003-03-04 05:59:38 +00:00
|
|
|
/* calculate the remaining time to that the info screen displays something
|
|
|
|
useful */
|
|
|
|
powermgmt_est_runningtime_min =
|
|
|
|
battery_level() * battery_capacity / 100 * 60 / CURRENT_NORMAL;
|
2003-03-03 13:23:11 +00:00
|
|
|
#if MEM == 8 /* assuming 192 kbps, the running time is 22% longer with 8MB */
|
|
|
|
powermgmt_est_runningtime_min = powermgmt_est_runningtime_min * 122 / 100;
|
|
|
|
#endif
|
2002-12-18 18:55:50 +00:00
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN, "Powermgmt started");
|
2002-12-15 18:08:06 +00:00
|
|
|
|
|
|
|
/* if the battery is nearly empty, start charging immediately */
|
|
|
|
if (power_history[POWER_HISTORY_LEN-1] < BATTERY_LEVEL_DANGEROUS)
|
|
|
|
charger_enable(true);
|
2003-11-19 14:14:41 +00:00
|
|
|
#endif
|
2002-08-06 10:50:50 +00:00
|
|
|
#endif
|
2002-12-18 18:55:50 +00:00
|
|
|
|
2004-07-14 11:25:14 +00:00
|
|
|
charger_power_is_on = charger_inserted();
|
|
|
|
|
2003-03-04 05:59:38 +00:00
|
|
|
create_thread(power_thread, power_stack, sizeof(power_stack),
|
|
|
|
power_thread_name);
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
|
|
|
|
2002-08-07 11:30:39 +00:00
|
|
|
#endif /* SIMULATOR */
|
|
|
|
|