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
|
2005-03-03 07:25:43 +00:00
|
|
|
* Revisions copyright (C) 2005 by Gerald Van Baren
|
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"
|
2005-04-04 12:06:29 +00:00
|
|
|
#include "audio.h"
|
2005-02-06 10:14:50 +00:00
|
|
|
#include "mp3_playback.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"
|
2005-02-06 10:16:13 +00:00
|
|
|
#include "lcd.h"
|
2005-02-06 17:21:42 +00:00
|
|
|
#include "rtc.h"
|
2004-09-28 22:13:26 +00:00
|
|
|
#ifdef CONFIG_TUNER
|
2003-05-03 15:39:40 +00:00
|
|
|
#include "fmradio.h"
|
|
|
|
#endif
|
2005-03-18 11:38:47 +00:00
|
|
|
#ifdef IRIVER_H100
|
|
|
|
#include "uda1380.h"
|
|
|
|
#endif
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
/*
|
|
|
|
* Define DEBUG_FILE to create a csv (spreadsheet) with battery information
|
|
|
|
* in it (one sample per minute). This is only for very low level debug.
|
|
|
|
*/
|
|
|
|
#undef DEBUG_FILE
|
|
|
|
#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL)
|
|
|
|
#include "file.h"
|
|
|
|
#define DEBUG_FILE_NAME "/powermgmt.csv"
|
|
|
|
#define DEBUG_MESSAGE_LEN 133
|
|
|
|
static char debug_message[DEBUG_MESSAGE_LEN];
|
|
|
|
#define DEBUG_STACK ((0x1000)/sizeof(long))
|
|
|
|
static int fd; /* write debug information to this file */
|
2005-03-23 20:53:37 +00:00
|
|
|
static int wrcount;
|
2005-03-03 07:25:43 +00:00
|
|
|
#else
|
|
|
|
#define DEBUG_STACK 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef SIMULATOR /***********************************************************/
|
2002-08-07 11:30:39 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2005-03-24 07:31:59 +00:00
|
|
|
void reset_poweroff_timer(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
#else /* not SIMULATOR ******************************************************/
|
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-05-04 16:18:09 +00:00
|
|
|
static const unsigned int battery_level_dangerous[BATTERY_TYPES_COUNT] =
|
|
|
|
{
|
|
|
|
#if CONFIG_BATTERY == BATT_LIION2200 /* FM Recorder, LiIon */
|
|
|
|
280
|
|
|
|
#elif CONFIG_BATTERY == BATT_3AAA /* Ondio */
|
|
|
|
310, 345 /* alkaline, NiHM */
|
|
|
|
#elif CONFIG_BATTERY == BATT_LIPOL1300 /* iRiver H1x0 */
|
|
|
|
339
|
|
|
|
#else /* Player/recorder, NiMH */
|
|
|
|
475
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
static const short percent_to_volt_discharge[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 */
|
2005-04-10 15:10:44 +00:00
|
|
|
{ 310, 355, 363, 369, 372, 374, 376, 378, 380, 386, 405 } /* NiMH */
|
2005-04-06 23:53:19 +00:00
|
|
|
#elif CONFIG_BATTERY == BATT_LIPOL1300
|
|
|
|
{ 333, 341, 349, 358, 365, 373, 370, 386, 393, 400, 409 }
|
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
|
|
|
};
|
|
|
|
|
2005-03-23 20:53:37 +00:00
|
|
|
#ifdef HAVE_CHARGING
|
|
|
|
/* voltages (centivolt) of 0%, 10%, ... 100% when charging enabled */
|
|
|
|
static const short percent_to_volt_charge[11] =
|
|
|
|
{
|
|
|
|
/* values guessed, see
|
|
|
|
http://www.seattlerobotics.org/encoder/200210/LiIon2.pdf until someone
|
|
|
|
measures voltages over a charging cycle */
|
|
|
|
476, 544, 551, 556, 561, 564, 566, 576, 582, 584, 585 /* NiMH */
|
|
|
|
};
|
|
|
|
#endif /* HAVE_CHARGING */
|
2005-01-30 16:25:46 +00:00
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
#if defined(HAVE_CHARGE_CTRL) || CONFIG_BATTERY == BATT_LIION2200
|
|
|
|
charge_state_type charge_state; /* charging mode */
|
2005-01-30 16:25:46 +00:00
|
|
|
#endif
|
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
#ifdef HAVE_CHARGING
|
2005-03-23 20:53:37 +00:00
|
|
|
/*
|
|
|
|
* Flag that the charger has been plugged in/removed: this is set for exactly
|
|
|
|
* one time through the power loop when the charger has been plugged in.
|
|
|
|
*/
|
|
|
|
static enum {
|
|
|
|
NO_CHARGER,
|
|
|
|
CHARGER_UNPLUGGED, /* transient state */
|
|
|
|
CHARGER_PLUGGED, /* transient state */
|
|
|
|
CHARGER
|
|
|
|
} charger_input_state;
|
|
|
|
#endif
|
2005-03-03 07:25:43 +00:00
|
|
|
|
2002-12-03 14:33:20 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
2005-03-03 07:25:43 +00:00
|
|
|
int long_delta; /* long term delta battery voltage */
|
|
|
|
int short_delta; /* short term delta battery voltage */
|
|
|
|
|
2003-03-04 05:59:38 +00:00
|
|
|
char power_message[POWER_MESSAGE_LEN] = ""; /* message that's shown in
|
|
|
|
debug menu */
|
2005-03-03 07:25:43 +00:00
|
|
|
/* percentage at which charging
|
2003-03-04 05:59:38 +00:00
|
|
|
starts */
|
2005-03-03 07:25:43 +00:00
|
|
|
int powermgmt_last_cycle_startstop_min = 0; /* how many minutes ago was the
|
2003-03-04 05:59:38 +00:00
|
|
|
charging started or
|
|
|
|
stopped? */
|
|
|
|
int powermgmt_last_cycle_level = 0; /* which level had the
|
|
|
|
batteries at this time? */
|
2005-03-23 20:53:37 +00:00
|
|
|
int trickle_sec = 0; /* how many seconds should the
|
2003-03-04 05:59:38 +00:00
|
|
|
charger be enabled per
|
|
|
|
minute for trickle
|
|
|
|
charging? */
|
2005-03-23 20:53:37 +00:00
|
|
|
int pid_p = 0; /* PID proportional term */
|
|
|
|
int pid_i = 0; /* PID integral term */
|
|
|
|
#endif /* HAVE_CHARGE_CTRL */
|
2005-03-03 07:25:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Average battery voltage and charger voltage, filtered via a digital
|
|
|
|
* exponential filter.
|
|
|
|
*/
|
2005-03-23 20:53:37 +00:00
|
|
|
static unsigned int battery_centivolts;/* filtered battery voltage, centvolts */
|
|
|
|
static unsigned int avgbat; /* average battery voltage (filtering) */
|
2005-03-03 07:25:43 +00:00
|
|
|
#define BATT_AVE_SAMPLES 32 /* filter constant / @ 2Hz sample rate */
|
|
|
|
|
|
|
|
/* battery level (0-100%) of this minute, updated once per minute */
|
2005-03-23 20:53:37 +00:00
|
|
|
static int battery_percent = -1;
|
|
|
|
static int battery_capacity = BATTERY_CAPACITY_MIN; /* default value, mAH */
|
|
|
|
static int battery_type = 0;
|
|
|
|
|
|
|
|
/* Power history: power_history[0] is the newest sample */
|
|
|
|
unsigned short power_history[POWER_HISTORY_LEN];
|
2005-03-03 07:25:43 +00:00
|
|
|
|
|
|
|
static char power_stack[DEFAULT_STACK_SIZE + DEBUG_STACK];
|
2004-08-03 19:22:56 +00:00
|
|
|
static const char power_thread_name[] = "power";
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2005-03-23 20:53:37 +00:00
|
|
|
static int poweroff_timeout = 0;
|
|
|
|
static 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;
|
2005-03-23 20:53:37 +00:00
|
|
|
static long sleeptimer_endtick;
|
|
|
|
|
|
|
|
static long last_event_tick;
|
|
|
|
|
2005-04-10 15:10:44 +00:00
|
|
|
static void battery_status_update(void);
|
|
|
|
static int runcurrent(void);
|
2005-03-23 20:53:37 +00:00
|
|
|
|
|
|
|
void reset_poweroff_timer(void)
|
|
|
|
{
|
|
|
|
last_event_tick = current_tick;
|
|
|
|
}
|
2002-12-03 22:41:50 +00:00
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
#if BATTERY_TYPES_COUNT > 1
|
|
|
|
void set_battery_type(int type)
|
|
|
|
{
|
|
|
|
if (type != battery_type) {
|
|
|
|
battery_type = type;
|
2005-04-10 15:10:44 +00:00
|
|
|
battery_status_update(); /* recalculate the battery status */
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void set_battery_capacity(int capacity)
|
|
|
|
{
|
|
|
|
battery_capacity = capacity;
|
|
|
|
if (battery_capacity > BATTERY_CAPACITY_MAX)
|
|
|
|
battery_capacity = BATTERY_CAPACITY_MAX;
|
|
|
|
if (battery_capacity < BATTERY_CAPACITY_MIN)
|
|
|
|
battery_capacity = BATTERY_CAPACITY_MIN;
|
2005-04-10 15:10:44 +00:00
|
|
|
battery_status_update(); /* recalculate the battery status */
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2002-12-03 14:18:51 +00:00
|
|
|
int battery_time(void)
|
|
|
|
{
|
|
|
|
return powermgmt_est_runningtime_min;
|
|
|
|
}
|
|
|
|
|
2002-12-18 18:55:50 +00:00
|
|
|
/* Returns battery level in percent */
|
|
|
|
int battery_level(void)
|
|
|
|
{
|
2005-03-03 07:25:43 +00:00
|
|
|
return battery_percent;
|
2002-08-07 11:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Tells if the battery level is safe for disk writes */
|
|
|
|
bool battery_level_safe(void)
|
|
|
|
{
|
2005-05-04 16:18:09 +00:00
|
|
|
return battery_centivolts > battery_level_dangerous[battery_type];
|
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) {
|
2005-03-23 20:53:37 +00:00
|
|
|
sleeptimer_active = true;
|
2002-12-03 22:41:50 +00:00
|
|
|
sleeptimer_endtick = current_tick + seconds * HZ;
|
|
|
|
}
|
2003-03-04 05:59:38 +00:00
|
|
|
else {
|
2005-03-23 20:53:37 +00:00
|
|
|
sleeptimer_active = false;
|
2002-12-03 22:41:50 +00:00
|
|
|
sleeptimer_endtick = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_sleep_timer(void)
|
|
|
|
{
|
|
|
|
if(sleeptimer_active)
|
|
|
|
return (sleeptimer_endtick - current_tick) / HZ;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-03-23 20:53:37 +00:00
|
|
|
/* look into the percent_to_volt_* table and get a realistic battery level
|
|
|
|
percentage */
|
|
|
|
static int voltage_to_percent(int voltage, const short* table)
|
|
|
|
{
|
|
|
|
if (voltage <= table[0])
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
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) /* Tens digit, 10% per entry */
|
|
|
|
+ (((voltage - table[i]) * 10)
|
|
|
|
/ (table[i+1] - table[i])); /* Ones digit: interpolated */
|
|
|
|
}
|
|
|
|
}
|
2002-09-24 07:50:17 +00:00
|
|
|
|
2005-04-10 15:10:44 +00:00
|
|
|
/* update battery level and estimated runtime, called once per minute or
|
|
|
|
* when battery capacity / type settings are changed */
|
|
|
|
static void battery_status_update(void)
|
2005-03-23 20:53:37 +00:00
|
|
|
{
|
|
|
|
int level;
|
|
|
|
|
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
if (charge_state == DISCHARGING) {
|
2005-04-10 15:10:44 +00:00
|
|
|
level = voltage_to_percent(battery_centivolts,
|
2005-03-23 20:53:37 +00:00
|
|
|
percent_to_volt_discharge[battery_type]);
|
|
|
|
}
|
|
|
|
else if (charge_state == CHARGING) {
|
2005-04-10 15:10:44 +00:00
|
|
|
/* battery level is defined to be < 100% until charging is finished */
|
|
|
|
level = MIN(voltage_to_percent(battery_centivolts,
|
|
|
|
percent_to_volt_charge), 99);
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
2005-04-10 15:10:44 +00:00
|
|
|
else { /* in topoff/trickle charge, the battery is by definition 100% full */
|
2005-03-23 20:53:37 +00:00
|
|
|
level = 100;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* always use the discharge table */
|
|
|
|
level = voltage_to_percent(battery_centivolts,
|
|
|
|
percent_to_volt_discharge[battery_type]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef HAVE_MMC /* this adjustment is only needed for HD based */
|
|
|
|
if (battery_percent == -1) { /* first run of this procedure */
|
|
|
|
/* The battery voltage is usually a little lower directly after
|
|
|
|
turning on, because the disk was used heavily. Raise it by 5. % */
|
|
|
|
level = (level > 95) ? 100 : level + 5;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
battery_percent = level;
|
2005-04-10 15:10:44 +00:00
|
|
|
|
|
|
|
/* calculate estimated remaining running time */
|
|
|
|
/* discharging: remaining running time */
|
|
|
|
/* charging: remaining charging time */
|
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
if (charge_state == CHARGING) {
|
|
|
|
powermgmt_est_runningtime_min = (100 - level) * battery_capacity / 100
|
|
|
|
* 60 / (CURRENT_MAX_CHG - runcurrent());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
powermgmt_est_runningtime_min = level * battery_capacity / 100
|
|
|
|
* 60 / runcurrent();
|
|
|
|
}
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We shut off in the following cases:
|
|
|
|
* 1) The unit is idle, not playing music
|
|
|
|
* 2) The unit is playing music, but is paused
|
|
|
|
*
|
|
|
|
* 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-23 11:42:48 +00:00
|
|
|
static void handle_auto_poweroff(void)
|
|
|
|
{
|
|
|
|
long timeout = poweroff_idle_timeout_value[poweroff_timeout]*60*HZ;
|
2005-04-04 12:06:29 +00:00
|
|
|
int audio_stat = audio_status();
|
2002-09-24 07:50:17 +00:00
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
#ifdef HAVE_CHARGING
|
2005-03-23 20:53:37 +00:00
|
|
|
/*
|
|
|
|
* Inhibit shutdown as long as the charger is plugged in. If it is
|
|
|
|
* unplugged, wait for a timeout period and then shut down.
|
|
|
|
*/
|
|
|
|
if(charger_input_state == CHARGER) {
|
|
|
|
last_event_tick = current_tick;
|
2002-09-26 22:34:35 +00:00
|
|
|
}
|
2005-03-03 07:25:43 +00:00
|
|
|
#endif
|
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() &&
|
2005-04-04 12:06:29 +00:00
|
|
|
((audio_stat == 0) ||
|
|
|
|
((audio_stat == (AUDIO_STATUS_PLAY | AUDIO_STATUS_PAUSE)) &&
|
2003-10-17 14:49:00 +00:00
|
|
|
!sleeptimer_active)))
|
2002-09-23 11:42:48 +00:00
|
|
|
{
|
2005-03-23 20:53:37 +00:00
|
|
|
if(TIME_AFTER(current_tick, last_event_tick + timeout) &&
|
|
|
|
TIME_AFTER(current_tick, last_disk_activity + timeout))
|
2003-10-17 14:05:32 +00:00
|
|
|
{
|
2005-02-06 09:57:57 +00:00
|
|
|
shutdown_hw();
|
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))
|
|
|
|
{
|
2005-04-04 12:06:29 +00:00
|
|
|
audio_stop();
|
2005-03-03 07:25:43 +00:00
|
|
|
#ifdef HAVE_CHARGING
|
2005-03-23 20:53:37 +00:00
|
|
|
if((charger_input_state == CHARGER) ||
|
|
|
|
(charger_input_state == CHARGER_PLUGGED))
|
2002-12-04 12:53:10 +00:00
|
|
|
{
|
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
|
2005-03-03 07:25:43 +00:00
|
|
|
#endif
|
2002-12-04 12:53:10 +00:00
|
|
|
{
|
|
|
|
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);
|
2005-02-06 09:57:57 +00:00
|
|
|
shutdown_hw();
|
2002-12-04 12:53:10 +00:00
|
|
|
}
|
2002-12-03 22:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-09-23 11:42:48 +00:00
|
|
|
}
|
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
/*
|
|
|
|
* Estimate how much current we are drawing just to run.
|
|
|
|
*/
|
|
|
|
static int runcurrent(void)
|
|
|
|
{
|
|
|
|
int current;
|
|
|
|
|
2005-04-10 15:10:44 +00:00
|
|
|
#if MEM == 8 && !defined(HAVE_MMC)
|
|
|
|
/* assuming 192 kbps, the running time is 22% longer with 8MB */
|
|
|
|
current = (CURRENT_NORMAL*100/122);
|
|
|
|
#else
|
2005-03-03 07:25:43 +00:00
|
|
|
current = CURRENT_NORMAL;
|
2005-04-10 15:10:44 +00:00
|
|
|
#endif /* MEM == 8 */
|
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
if(usb_inserted()) {
|
|
|
|
current = CURRENT_USB;
|
|
|
|
}
|
2005-04-10 15:10:44 +00:00
|
|
|
|
|
|
|
if ((backlight_get_timeout() == 1) /* LED always on */
|
2005-03-03 07:25:43 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
2005-04-10 15:10:44 +00:00
|
|
|
|| (charger_inserted() && backlight_get_on_when_charging())
|
|
|
|
#endif
|
|
|
|
) {
|
2005-03-03 07:25:43 +00:00
|
|
|
current += CURRENT_BACKLIGHT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(current);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-02-06 17:21:42 +00:00
|
|
|
/* Check to see whether or not we've received an alarm in the last second */
|
|
|
|
#ifdef HAVE_ALARM_MOD
|
|
|
|
static void power_thread_rtc_process(void)
|
|
|
|
{
|
2005-02-16 12:18:16 +00:00
|
|
|
if (rtc_check_alarm_flag()) {
|
2005-03-03 07:25:43 +00:00
|
|
|
rtc_enable_alarm(false);
|
2005-02-16 12:18:16 +00:00
|
|
|
}
|
2005-02-06 17:21:42 +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
|
2005-03-03 07:25:43 +00:00
|
|
|
* rate than the slow periodic rate of the main power_thread loop.
|
|
|
|
*
|
|
|
|
* While we are waiting for the time to expire, we average the battery
|
|
|
|
* voltages.
|
2003-10-17 14:49:00 +00:00
|
|
|
*/
|
|
|
|
static void power_thread_sleep(int ticks)
|
|
|
|
{
|
2005-03-03 07:25:43 +00:00
|
|
|
int small_ticks;
|
2005-03-23 20:53:37 +00:00
|
|
|
|
|
|
|
while (ticks > 0) {
|
2005-03-03 07:25:43 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CHARGING
|
2005-03-23 20:53:37 +00:00
|
|
|
/*
|
|
|
|
* Detect charger plugged/unplugged transitions. On a plugged or
|
|
|
|
* unplugged event, we return immediately, run once through the main
|
|
|
|
* loop (including the subroutines), and end up back here where we
|
|
|
|
* transition to the appropriate steady state charger on/off state.
|
|
|
|
*/
|
|
|
|
if(charger_inserted()) {
|
|
|
|
switch(charger_input_state) {
|
|
|
|
case NO_CHARGER:
|
|
|
|
case CHARGER_UNPLUGGED:
|
|
|
|
charger_input_state = CHARGER_PLUGGED;
|
|
|
|
return;
|
|
|
|
case CHARGER_PLUGGED:
|
2005-04-06 11:12:22 +00:00
|
|
|
queue_broadcast(SYS_CHARGER_CONNECTED, NULL);
|
2005-03-23 20:53:37 +00:00
|
|
|
charger_input_state = CHARGER;
|
|
|
|
break;
|
|
|
|
case CHARGER:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else { /* charger not inserted */
|
|
|
|
switch(charger_input_state) {
|
|
|
|
case NO_CHARGER:
|
|
|
|
break;
|
|
|
|
case CHARGER_UNPLUGGED:
|
2005-04-06 11:12:22 +00:00
|
|
|
queue_broadcast(SYS_CHARGER_DISCONNECTED, NULL);
|
2005-03-23 20:53:37 +00:00
|
|
|
charger_input_state = NO_CHARGER;
|
|
|
|
break;
|
|
|
|
case CHARGER_PLUGGED:
|
|
|
|
case CHARGER:
|
|
|
|
charger_input_state = CHARGER_UNPLUGGED;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2005-03-03 07:25:43 +00:00
|
|
|
#endif
|
2005-03-23 20:53:37 +00:00
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
small_ticks = MIN(HZ/2, ticks);
|
2003-10-17 14:49:00 +00:00
|
|
|
sleep(small_ticks);
|
|
|
|
ticks -= small_ticks;
|
|
|
|
|
2005-02-06 17:21:42 +00:00
|
|
|
#ifdef HAVE_ALARM_MOD
|
2005-03-03 07:25:43 +00:00
|
|
|
power_thread_rtc_process();
|
2005-02-06 17:21:42 +00:00
|
|
|
#endif
|
2005-03-03 07:25:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do a digital exponential filter. We don't sample the battery if
|
|
|
|
* the disk is spinning unless we are in USB mode (the disk will most
|
|
|
|
* likely always be spinning in USB mode).
|
|
|
|
*/
|
|
|
|
if (!ata_disk_is_active() || usb_inserted()) {
|
|
|
|
avgbat = avgbat - (avgbat / BATT_AVE_SAMPLES) +
|
|
|
|
adc_read(ADC_UNREG_POWER);
|
|
|
|
/*
|
|
|
|
* battery_centivolts is the centivolt-scaled filtered battery value.
|
|
|
|
*/
|
2005-03-23 20:53:37 +00:00
|
|
|
battery_centivolts = ((avgbat / BATT_AVE_SAMPLES) *
|
|
|
|
BATTERY_SCALE_FACTOR) / 10000;
|
|
|
|
}
|
|
|
|
#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL)
|
|
|
|
/*
|
|
|
|
* If we have a lot of pending writes or if the disk is spining,
|
|
|
|
* fsync the debug log file.
|
|
|
|
*/
|
|
|
|
if((wrcount > 10) ||
|
|
|
|
((wrcount > 0) && ata_disk_is_active())) {
|
|
|
|
fsync(fd);
|
|
|
|
wrcount = 0;
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
2004-10-15 18:18:15 +00:00
|
|
|
#endif
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
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;
|
2005-03-03 07:25:43 +00:00
|
|
|
short *phps, *phpd; /* power history rotation pointers */
|
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
|
2005-03-23 20:53:37 +00:00
|
|
|
unsigned int target_voltage; /* desired topoff/trickle voltage level */
|
|
|
|
int charge_max_time_now = 0; /* max. charging duration, calculated at
|
|
|
|
beginning of charging */
|
2002-08-06 10:50:50 +00:00
|
|
|
#endif
|
2005-03-03 07:25:43 +00:00
|
|
|
|
|
|
|
/* initialize the voltages for the exponential filter */
|
2005-03-23 20:53:37 +00:00
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
avgbat = adc_read(ADC_UNREG_POWER) * BATT_AVE_SAMPLES;
|
2005-03-23 20:53:37 +00:00
|
|
|
battery_centivolts = ((avgbat / BATT_AVE_SAMPLES) * BATTERY_SCALE_FACTOR) / 10000;
|
2005-03-03 07:25:43 +00:00
|
|
|
|
|
|
|
#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL)
|
2005-03-23 20:53:37 +00:00
|
|
|
fd = -1;
|
|
|
|
wrcount = 0;
|
2005-03-03 07:25:43 +00:00
|
|
|
#endif
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* rotate the power history */
|
2005-03-03 07:25:43 +00:00
|
|
|
phpd = &power_history[POWER_HISTORY_LEN - 1];
|
|
|
|
phps = phpd - 1;
|
2002-08-06 10:50:50 +00:00
|
|
|
for (i = 0; i < POWER_HISTORY_LEN-1; i++)
|
2005-03-03 07:25:43 +00:00
|
|
|
*phpd-- = *phps--;
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
/* insert new value at the start, in centivolts 8-) */
|
|
|
|
power_history[0] = battery_centivolts;
|
|
|
|
|
2005-04-10 15:10:44 +00:00
|
|
|
/* update battery status every minute */
|
|
|
|
battery_status_update();
|
2003-03-03 13:23:11 +00:00
|
|
|
|
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) {
|
2005-03-03 07:25:43 +00:00
|
|
|
charge_state = TRICKLE;
|
2004-02-05 13:44:04 +00:00
|
|
|
} else {
|
2005-03-03 07:25:43 +00:00
|
|
|
charge_state = CHARGING;
|
2004-02-05 13:44:04 +00:00
|
|
|
}
|
|
|
|
} else {
|
2005-03-03 07:25:43 +00:00
|
|
|
charge_state = DISCHARGING;
|
2004-02-05 13:44:04 +00:00
|
|
|
}
|
2005-03-03 07:25:43 +00:00
|
|
|
#endif /* # if CONFIG_BATTERY == BATT_LIION2200 */
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
2005-03-23 20:53:37 +00:00
|
|
|
if (charger_input_state == CHARGER_PLUGGED) {
|
|
|
|
pid_p = 0;
|
|
|
|
pid_i = 0;
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN, "Charger plugged in");
|
2005-03-03 07:25:43 +00:00
|
|
|
/*
|
2005-03-23 20:53:37 +00:00
|
|
|
* The charger was just plugged in. If the battery level is
|
|
|
|
* nearly charged, just trickle. If the battery is low, start
|
|
|
|
* a full charge cycle. If the battery level is in between,
|
|
|
|
* top-off and then trickle.
|
2005-03-03 07:25:43 +00:00
|
|
|
*/
|
2005-03-23 20:53:37 +00:00
|
|
|
if(battery_percent > START_TOPOFF_CHG) {
|
|
|
|
powermgmt_last_cycle_level = battery_percent;
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
|
|
|
if(battery_percent >= START_TRICKLE_CHG) {
|
|
|
|
charge_state = TRICKLE;
|
|
|
|
} else {
|
|
|
|
charge_state = TOPOFF;
|
|
|
|
}
|
|
|
|
} else {
|
2005-03-03 07:25:43 +00:00
|
|
|
/*
|
2005-03-23 20:53:37 +00:00
|
|
|
* Start the charger full strength
|
2005-03-03 07:25:43 +00:00
|
|
|
*/
|
2005-03-23 20:53:37 +00:00
|
|
|
i = CHARGE_MAX_TIME_1500 * battery_capacity / 1500;
|
|
|
|
charge_max_time_now =
|
|
|
|
i * (100 + 35 - battery_percent) / 100;
|
|
|
|
if (charge_max_time_now > i) {
|
|
|
|
charge_max_time_now = i;
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
2005-03-23 20:53:37 +00:00
|
|
|
"ChgAt %d%% max %dm", battery_level(),
|
2005-03-03 07:25:43 +00:00
|
|
|
charge_max_time_now);
|
2005-03-23 20:53:37 +00:00
|
|
|
|
|
|
|
/* 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, charging\n");
|
|
|
|
powermgmt_last_cycle_level = battery_percent;
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
|
|
|
trickle_sec = 60;
|
|
|
|
long_delta = short_delta = 999999;
|
|
|
|
charge_state = CHARGING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (charge_state == CHARGING) {
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
|
|
|
"Chg %dm, max %dm", powermgmt_last_cycle_startstop_min,
|
|
|
|
charge_max_time_now);
|
|
|
|
/*
|
|
|
|
* Check the delta voltage over the last X minutes so we can do
|
|
|
|
* our end-of-charge logic based on the battery level change.
|
|
|
|
*/
|
|
|
|
if (powermgmt_last_cycle_startstop_min > CHARGE_MIN_TIME) {
|
|
|
|
short_delta = power_history[0] -
|
|
|
|
power_history[CHARGE_END_NEGD - 1];
|
|
|
|
}
|
|
|
|
if (powermgmt_last_cycle_startstop_min > CHARGE_END_ZEROD) {
|
|
|
|
/*
|
|
|
|
* Scan the history: if we have a big delta in the middle of
|
|
|
|
* our history, the long term delta isn't a valid end-of-charge
|
|
|
|
* indicator.
|
|
|
|
*/
|
|
|
|
long_delta = power_history[0] -
|
|
|
|
power_history[CHARGE_END_ZEROD - 1];
|
|
|
|
for(i = 0; i < CHARGE_END_ZEROD; i++) {
|
|
|
|
if(((power_history[i] - power_history[i+1]) > 5) ||
|
|
|
|
((power_history[i] - power_history[i+1]) < -5)) {
|
|
|
|
long_delta = 888888;
|
|
|
|
break;
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
2005-03-03 07:25:43 +00:00
|
|
|
|
2005-03-23 20:53:37 +00:00
|
|
|
/*
|
|
|
|
* End of charge criteria (any qualify):
|
|
|
|
* 1) Charged a long time
|
|
|
|
* 2) DeltaV went negative for a short time
|
|
|
|
* 3) DeltaV was close to zero for a long time
|
|
|
|
* Note: short_delta and long_delta are centivolts
|
|
|
|
*/
|
|
|
|
if ((powermgmt_last_cycle_startstop_min > charge_max_time_now) ||
|
|
|
|
(short_delta <= -5) || (long_delta < 5))
|
|
|
|
{
|
|
|
|
if (powermgmt_last_cycle_startstop_min > charge_max_time_now) {
|
|
|
|
DEBUGF("power: powermgmt_last_cycle_startstop_min > charge_max_time_now, "
|
|
|
|
"enough!\n");
|
|
|
|
/* have charged too long and deltaV detection did not
|
|
|
|
work! */
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
|
|
|
"Chg tmout %d min", charge_max_time_now);
|
|
|
|
/*
|
|
|
|
* Switch to trickle charging. We skip the top-off
|
|
|
|
* since we've effectively done the top-off operation
|
|
|
|
* already since we charged for the maximum full
|
|
|
|
* charge time.
|
|
|
|
*/
|
|
|
|
powermgmt_last_cycle_level = battery_percent;
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
|
|
|
charge_state = TRICKLE;
|
|
|
|
} else {
|
|
|
|
if(short_delta <= -5) {
|
|
|
|
DEBUGF("power: short-term negative"
|
|
|
|
" delta, enough!\n");
|
2005-03-03 07:25:43 +00:00
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
2005-03-23 20:53:37 +00:00
|
|
|
"end negd %d %dmin", short_delta,
|
|
|
|
powermgmt_last_cycle_startstop_min);
|
2005-03-03 07:25:43 +00:00
|
|
|
} else {
|
2005-03-23 20:53:37 +00:00
|
|
|
DEBUGF("power: long-term small "
|
|
|
|
"positive delta, enough!\n");
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
|
|
|
"end lowd %d %dmin", long_delta,
|
|
|
|
powermgmt_last_cycle_startstop_min);
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
2005-03-23 20:53:37 +00:00
|
|
|
/*
|
|
|
|
* Switch to top-off charging.
|
|
|
|
*/
|
2005-03-03 07:25:43 +00:00
|
|
|
powermgmt_last_cycle_level = battery_percent;
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
2005-03-23 20:53:37 +00:00
|
|
|
charge_state = TOPOFF;
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
2003-03-04 05:59:38 +00:00
|
|
|
}
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
|
|
|
else if (charge_state > CHARGING) /* top off or trickle */
|
|
|
|
{
|
|
|
|
/* Time to switch from topoff to trickle?
|
|
|
|
*/
|
|
|
|
if ((charge_state == TOPOFF) &&
|
|
|
|
(powermgmt_last_cycle_startstop_min > TOPOFF_MAX_TIME))
|
2005-03-03 07:25:43 +00:00
|
|
|
{
|
2005-03-23 20:53:37 +00:00
|
|
|
powermgmt_last_cycle_level = battery_percent;
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
|
|
|
charge_state = TRICKLE;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Adjust trickle charge time (proportional and integral terms).
|
|
|
|
* Note: I considered setting the level higher if the USB is
|
|
|
|
* plugged in, but it doesn't appear to be necessary and will
|
|
|
|
* generate more heat [gvb].
|
|
|
|
*/
|
|
|
|
if(charge_state == TOPOFF)
|
|
|
|
target_voltage = TOPOFF_VOLTAGE;
|
|
|
|
else
|
|
|
|
target_voltage = TRICKLE_VOLTAGE;
|
|
|
|
|
|
|
|
pid_p = target_voltage - battery_centivolts;
|
|
|
|
if((pid_p > PID_DEADZONE) || (pid_p < -PID_DEADZONE))
|
|
|
|
pid_p = pid_p * PID_PCONST;
|
|
|
|
else
|
|
|
|
pid_p = 0;
|
|
|
|
if(battery_centivolts < target_voltage) {
|
|
|
|
if(pid_i < 60) {
|
|
|
|
pid_i++; /* limit so it doesn't "wind up" */
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
2005-03-23 20:53:37 +00:00
|
|
|
} else {
|
|
|
|
if(pid_i > 0) {
|
|
|
|
pid_i--; /* limit so it doesn't "wind up" */
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
trickle_sec = pid_p + pid_i;
|
2002-12-15 18:08:06 +00:00
|
|
|
|
2005-03-23 20:53:37 +00:00
|
|
|
if(trickle_sec > 60) {
|
|
|
|
trickle_sec = 60;
|
|
|
|
}
|
|
|
|
if(trickle_sec < 0) {
|
2005-03-03 07:25:43 +00:00
|
|
|
trickle_sec = 0;
|
2003-01-19 20:42:09 +00:00
|
|
|
}
|
2005-03-03 07:25:43 +00:00
|
|
|
|
2005-03-23 20:53:37 +00:00
|
|
|
} else if (charge_state == DISCHARGING) {
|
|
|
|
trickle_sec = 0;
|
|
|
|
/*
|
|
|
|
* The charger is enabled here only in one case: if it was
|
|
|
|
* turned on at boot time (power_init). Turn it off now.
|
|
|
|
*/
|
|
|
|
if (charger_enabled)
|
2005-03-03 07:25:43 +00:00
|
|
|
charger_enable(false);
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
2005-03-23 20:53:37 +00:00
|
|
|
|
|
|
|
if (charger_input_state == CHARGER_UNPLUGGED) {
|
|
|
|
/*
|
|
|
|
* The charger was just unplugged.
|
|
|
|
*/
|
|
|
|
DEBUGF("power: charger disconnected, disabling\n");
|
|
|
|
|
|
|
|
charger_enable(false);
|
|
|
|
powermgmt_last_cycle_level = battery_percent;
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
|
|
|
trickle_sec = 0;
|
|
|
|
pid_p = 0;
|
|
|
|
pid_i = 0;
|
|
|
|
charge_state = DISCHARGING;
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN, "Charger: discharge");
|
|
|
|
}
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
#endif /* HAVE_CHARGE_CTRL*/
|
2005-03-03 07:25:43 +00:00
|
|
|
|
|
|
|
/* sleep for a minute */
|
|
|
|
|
2002-12-14 12:10:29 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
2005-03-03 07:25:43 +00:00
|
|
|
if(trickle_sec > 0) {
|
|
|
|
charger_enable(true);
|
|
|
|
power_thread_sleep(HZ * trickle_sec);
|
|
|
|
}
|
|
|
|
if(trickle_sec < 60)
|
|
|
|
charger_enable(false);
|
|
|
|
power_thread_sleep(HZ * (60 - trickle_sec));
|
2002-12-14 12:10:29 +00:00
|
|
|
#else
|
2005-03-03 07:25:43 +00:00
|
|
|
power_thread_sleep(HZ * 60);
|
2002-12-14 12:10:29 +00:00
|
|
|
#endif
|
2002-09-23 11:42:48 +00:00
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL)
|
2005-03-23 20:53:37 +00:00
|
|
|
if(usb_inserted()) {
|
|
|
|
if(fd >= 0) {
|
|
|
|
/* It is probably too late to close the file but we can try... */
|
|
|
|
close(fd);
|
|
|
|
fd = -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(fd < 0) {
|
|
|
|
fd = open(DEBUG_FILE_NAME, O_WRONLY | O_APPEND | O_CREAT);
|
|
|
|
if(fd >= 0) {
|
|
|
|
snprintf(debug_message, DEBUG_MESSAGE_LEN,
|
|
|
|
"cycle_min, bat_centivolts, bat_percent, chgr_state, charge_state, pid_p, pid_i, trickle_sec\n");
|
|
|
|
write(fd, debug_message, strlen(debug_message));
|
|
|
|
wrcount = 99; /* force a flush */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(fd >= 0) {
|
|
|
|
snprintf(debug_message, DEBUG_MESSAGE_LEN, "%d, %d, %d, %d, %d, %d, %d, %d\n",
|
|
|
|
powermgmt_last_cycle_startstop_min, battery_centivolts,
|
|
|
|
battery_percent, charger_input_state, charge_state, pid_p, pid_i, trickle_sec);
|
|
|
|
write(fd, debug_message, strlen(debug_message));
|
|
|
|
wrcount++;
|
|
|
|
}
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
2002-09-23 11:42:48 +00:00
|
|
|
handle_auto_poweroff();
|
2005-03-23 20:53:37 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
powermgmt_last_cycle_startstop_min++;
|
|
|
|
#endif
|
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));
|
2004-07-14 11:25:14 +00:00
|
|
|
|
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 */
|
|
|
|
|
2005-02-06 17:21:42 +00:00
|
|
|
/* Various hardware housekeeping tasks relating to shutting down the jukebox */
|
|
|
|
void shutdown_hw(void)
|
|
|
|
{
|
2005-03-24 07:31:59 +00:00
|
|
|
#ifndef SIMULATOR
|
2005-03-03 07:25:43 +00:00
|
|
|
#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL)
|
2005-03-23 20:53:37 +00:00
|
|
|
if(fd >= 0) {
|
2005-03-03 07:25:43 +00:00
|
|
|
close(fd);
|
2005-03-23 20:53:37 +00:00
|
|
|
fd = -1;
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
|
|
|
#endif
|
2005-04-04 12:06:29 +00:00
|
|
|
audio_stop();
|
2005-02-06 09:57:57 +00:00
|
|
|
ata_flush();
|
|
|
|
ata_spindown(1);
|
|
|
|
while(ata_disk_is_active())
|
2005-03-03 07:25:43 +00:00
|
|
|
sleep(HZ/10);
|
2005-02-06 09:57:57 +00:00
|
|
|
|
|
|
|
mp3_shutdown();
|
2005-03-18 11:38:47 +00:00
|
|
|
#ifdef IRIVER_H100
|
|
|
|
uda1380_close();
|
|
|
|
#endif
|
2005-02-06 09:57:57 +00:00
|
|
|
#if CONFIG_KEYPAD == ONDIO_PAD
|
|
|
|
backlight_off();
|
|
|
|
sleep(1);
|
|
|
|
lcd_set_contrast(0);
|
|
|
|
#endif
|
|
|
|
power_off();
|
2005-03-24 07:31:59 +00:00
|
|
|
#endif /* #ifndef SIMULATOR */
|
2005-02-06 09:57:57 +00:00
|
|
|
}
|