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"
|
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"
|
2007-02-18 02:04:47 +00:00
|
|
|
#if CONFIG_TUNER
|
2003-05-03 15:39:40 +00:00
|
|
|
#include "fmradio.h"
|
|
|
|
#endif
|
2007-05-03 18:08:00 +00:00
|
|
|
#include "sound.h"
|
2005-12-06 13:27:15 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
|
|
|
#include "font.h"
|
|
|
|
#endif
|
2006-11-11 01:18:57 +00:00
|
|
|
#if defined(HAVE_RECORDING) && (CONFIG_CODEC == SWCODEC)
|
|
|
|
#include "pcm_record.h"
|
|
|
|
#endif
|
2005-07-05 00:03:18 +00:00
|
|
|
#include "logf.h"
|
2005-11-19 01:30:42 +00:00
|
|
|
#include "lcd-remote.h"
|
2006-03-25 14:31:44 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2007-03-05 01:31:26 +00:00
|
|
|
#if (defined(IAUDIO_X5) || defined(IAUDIO_M5)) && !defined (SIMULATOR)
|
2006-10-25 23:24:07 +00:00
|
|
|
#include "pcf50606.h"
|
2006-11-14 02:46:59 +00:00
|
|
|
#include "lcd-remote-target.h"
|
2006-09-12 14:37:22 +00:00
|
|
|
#endif
|
2006-09-12 14:20:29 +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
|
2006-06-06 22:23:52 +00:00
|
|
|
#if defined(DEBUG_FILE) && (CONFIG_CHARGING == CHARGING_CONTROL)
|
2005-03-03 07:25:43 +00:00
|
|
|
#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
|
|
|
|
|
2005-09-14 09:08:26 +00:00
|
|
|
static int shutdown_timeout = 0;
|
2007-08-12 19:49:03 +00:00
|
|
|
#if CONFIG_CHARGING >= CHARGING_MONITOR
|
|
|
|
charge_state_type charge_state; /* charging mode */
|
|
|
|
#endif
|
|
|
|
|
2007-11-03 05:00:49 +00:00
|
|
|
static void send_battery_level_event(void);
|
|
|
|
static int last_sent_battery_level = 100;
|
|
|
|
|
2007-08-12 19:49:03 +00:00
|
|
|
#if CONFIG_CHARGING
|
|
|
|
charger_input_state_type charger_input_state IDATA_ATTR;
|
|
|
|
#endif
|
2005-09-14 09:08:26 +00:00
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
#ifdef SIMULATOR /***********************************************************/
|
2002-08-07 11:30:39 +00:00
|
|
|
|
2007-08-15 23:57:27 +00:00
|
|
|
#define BATT_MINMVOLT 2500 /* minimum millivolts of battery */
|
|
|
|
#define BATT_MAXMVOLT 4500 /* maximum millivolts of battery */
|
2006-03-25 14:31:44 +00:00
|
|
|
#define BATT_MAXRUNTIME (10 * 60) /* maximum runtime with full battery in minutes */
|
|
|
|
|
2007-11-03 13:21:40 +00:00
|
|
|
static unsigned int battery_millivolts = (unsigned int)BATT_MAXMVOLT;
|
|
|
|
static int battery_percent = 100; /* battery capacity level in percent */
|
|
|
|
static int powermgmt_est_runningtime_min = BATT_MAXRUNTIME; /* estimated remaining time in minutes */
|
2006-03-25 14:31:44 +00:00
|
|
|
|
|
|
|
static void battery_status_update(void)
|
|
|
|
{
|
2007-11-03 13:21:40 +00:00
|
|
|
static time_t last_change = 0;
|
|
|
|
static bool charging = false;
|
|
|
|
time_t now;
|
2006-03-25 14:31:44 +00:00
|
|
|
|
|
|
|
time(&now);
|
2007-11-03 13:21:40 +00:00
|
|
|
if (last_change < now)
|
|
|
|
{
|
2006-03-25 14:31:44 +00:00
|
|
|
last_change = now;
|
|
|
|
|
|
|
|
/* change the values: */
|
2007-11-03 13:21:40 +00:00
|
|
|
if (charging)
|
|
|
|
{
|
2007-11-03 13:45:12 +00:00
|
|
|
if (battery_millivolts >= BATT_MAXMVOLT)
|
2007-11-03 13:21:40 +00:00
|
|
|
{
|
|
|
|
/* Pretend the charger was disconnected */
|
|
|
|
charging = false;
|
|
|
|
queue_broadcast(SYS_CHARGER_DISCONNECTED, 0);
|
|
|
|
last_sent_battery_level = 100;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-11-03 13:45:12 +00:00
|
|
|
if (battery_millivolts <= BATT_MINMVOLT)
|
2007-11-03 13:21:40 +00:00
|
|
|
{
|
|
|
|
/* Pretend the charger was connected */
|
|
|
|
charging = true;
|
|
|
|
queue_broadcast(SYS_CHARGER_CONNECTED, 0);
|
|
|
|
last_sent_battery_level = 0;
|
|
|
|
}
|
|
|
|
}
|
2007-11-03 13:45:12 +00:00
|
|
|
if (charging)
|
|
|
|
battery_millivolts += (BATT_MAXMVOLT - BATT_MINMVOLT) / 50;
|
|
|
|
else
|
|
|
|
battery_millivolts -= (BATT_MAXMVOLT - BATT_MINMVOLT) / 100;
|
2006-03-25 14:31:44 +00:00
|
|
|
|
2007-11-03 13:21:40 +00:00
|
|
|
battery_percent = 100 * (battery_millivolts - BATT_MINMVOLT) / (BATT_MAXMVOLT - BATT_MINMVOLT);
|
|
|
|
powermgmt_est_runningtime_min = battery_percent * BATT_MAXRUNTIME / 100;
|
2006-03-25 14:31:44 +00:00
|
|
|
}
|
2007-11-03 05:00:49 +00:00
|
|
|
send_battery_level_event();
|
2006-03-25 14:31:44 +00:00
|
|
|
}
|
|
|
|
|
2007-08-15 23:57:27 +00:00
|
|
|
void battery_read_info(int *voltage, int *level)
|
2006-10-30 11:33:38 +00:00
|
|
|
{
|
|
|
|
battery_status_update();
|
|
|
|
|
|
|
|
if (voltage)
|
2007-11-03 13:21:40 +00:00
|
|
|
*voltage = battery_millivolts;
|
2006-10-30 11:33:38 +00:00
|
|
|
|
|
|
|
if (level)
|
2007-11-03 13:21:40 +00:00
|
|
|
*level = battery_percent;
|
2006-10-30 11:33:38 +00:00
|
|
|
}
|
|
|
|
|
2006-01-25 22:28:55 +00:00
|
|
|
unsigned int battery_voltage(void)
|
|
|
|
{
|
2006-03-25 14:31:44 +00:00
|
|
|
battery_status_update();
|
2007-11-03 13:21:40 +00:00
|
|
|
return battery_millivolts;
|
2006-01-25 22:28:55 +00:00
|
|
|
}
|
|
|
|
|
2002-08-07 11:30:39 +00:00
|
|
|
int battery_level(void)
|
|
|
|
{
|
2006-03-25 14:31:44 +00:00
|
|
|
battery_status_update();
|
2007-11-03 13:21:40 +00:00
|
|
|
return battery_percent;
|
2002-08-07 11:30:39 +00:00
|
|
|
}
|
|
|
|
|
2002-12-03 14:18:51 +00:00
|
|
|
int battery_time(void)
|
|
|
|
{
|
2006-03-25 14:31:44 +00:00
|
|
|
battery_status_update();
|
2007-11-03 13:21:40 +00:00
|
|
|
return powermgmt_est_runningtime_min;
|
2002-12-03 14:18:51 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2007-08-12 19:49:03 +00:00
|
|
|
#if BATTERY_TYPES_COUNT > 1
|
|
|
|
void set_battery_type(int type)
|
2005-03-24 07:31:59 +00:00
|
|
|
{
|
2007-08-12 19:49:03 +00:00
|
|
|
(void)type;
|
2005-03-24 07:31:59 +00:00
|
|
|
}
|
2007-08-12 19:49:03 +00:00
|
|
|
#endif
|
2005-03-24 07:31:59 +00:00
|
|
|
|
2007-08-12 19:49:03 +00:00
|
|
|
void reset_poweroff_timer(void)
|
|
|
|
{
|
|
|
|
}
|
2005-03-24 07:31:59 +00:00
|
|
|
|
2008-04-20 19:00:52 +00:00
|
|
|
#ifdef HAVE_ACCESSORY_SUPPLY
|
|
|
|
void accessory_supply_set(bool enable)
|
|
|
|
{
|
|
|
|
(void)enable;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
#else /* not SIMULATOR ******************************************************/
|
2002-12-18 18:55:50 +00:00
|
|
|
|
2006-06-06 22:23:52 +00:00
|
|
|
#if CONFIG_CHARGING == CHARGING_CONTROL
|
2005-03-03 07:25:43 +00:00
|
|
|
int long_delta; /* long term delta battery voltage */
|
|
|
|
int short_delta; /* short term delta battery voltage */
|
2006-03-04 09:48:53 +00:00
|
|
|
bool disk_activity_last_cycle = false; /* flag set to aid charger time
|
|
|
|
* calculation */
|
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 */
|
2006-06-06 22:23:52 +00:00
|
|
|
#endif /* CONFIG_CHARGING == CHARGING_CONTROL */
|
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 avgbat; /* average battery voltage (filtering) */
|
2007-08-15 23:57:27 +00:00
|
|
|
static unsigned int battery_millivolts;/* filtered battery voltage, millivolts */
|
2007-08-17 06:45:18 +00:00
|
|
|
|
2006-11-11 01:18:57 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
2006-11-14 02:46:59 +00:00
|
|
|
#define BATT_AVE_SAMPLES 32 /* filter constant / @ 2Hz sample rate */
|
2006-11-11 01:18:57 +00:00
|
|
|
#else
|
2007-08-17 06:45:18 +00:00
|
|
|
#define BATT_AVE_SAMPLES 128 /* slw filter constant for all others */
|
2006-11-11 01:18:57 +00:00
|
|
|
#endif
|
2005-03-03 07:25:43 +00:00
|
|
|
|
|
|
|
/* battery level (0-100%) of this minute, updated once per minute */
|
2005-03-23 20:53:37 +00:00
|
|
|
static int battery_percent = -1;
|
2006-04-15 13:07:21 +00:00
|
|
|
static int battery_capacity = BATTERY_CAPACITY_DEFAULT; /* default value, mAh */
|
2007-11-08 15:37:39 +00:00
|
|
|
#if BATTERY_TYPES_COUNT > 1
|
2005-03-23 20:53:37 +00:00
|
|
|
static int battery_type = 0;
|
2007-11-08 15:37:39 +00:00
|
|
|
#else
|
|
|
|
#define battery_type 0
|
|
|
|
#endif
|
2005-03-23 20:53:37 +00:00
|
|
|
|
|
|
|
/* Power history: power_history[0] is the newest sample */
|
|
|
|
unsigned short power_history[POWER_HISTORY_LEN];
|
2005-03-03 07:25:43 +00:00
|
|
|
|
2006-11-11 01:18:57 +00:00
|
|
|
static char power_stack[DEFAULT_STACK_SIZE/2 + 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;
|
|
|
|
|
2007-08-15 23:57:27 +00:00
|
|
|
static int voltage_to_battery_level(int battery_millivolts);
|
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
|
|
|
|
2007-08-15 23:57:27 +00:00
|
|
|
void battery_read_info(int *voltage, int *level)
|
2006-10-30 11:33:38 +00:00
|
|
|
{
|
2007-08-17 06:45:18 +00:00
|
|
|
int millivolts = battery_adc_voltage();
|
2006-10-30 11:33:38 +00:00
|
|
|
|
|
|
|
if (voltage)
|
2007-08-15 23:57:27 +00:00
|
|
|
*voltage = millivolts;
|
2006-10-30 11:33:38 +00:00
|
|
|
|
|
|
|
if (level)
|
2007-08-15 23:57:27 +00:00
|
|
|
*level = voltage_to_battery_level(millivolts);
|
2006-10-30 11:33:38 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2007-08-15 23:57:27 +00:00
|
|
|
/* Returns filtered battery voltage [millivolts] */
|
2006-11-11 01:18:57 +00:00
|
|
|
unsigned int battery_voltage(void)
|
|
|
|
{
|
2007-08-15 23:57:27 +00:00
|
|
|
return battery_millivolts;
|
2006-11-11 01:18:57 +00:00
|
|
|
}
|
|
|
|
|
2002-08-07 11:30:39 +00:00
|
|
|
/* Tells if the battery level is safe for disk writes */
|
|
|
|
bool battery_level_safe(void)
|
|
|
|
{
|
2007-08-15 23:57:27 +00:00
|
|
|
return battery_millivolts > 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;
|
|
|
|
}
|
|
|
|
|
2006-11-11 01:18:57 +00:00
|
|
|
/* look into the percent_to_volt_* table and get a realistic battery level */
|
2005-03-23 20:53:37 +00:00
|
|
|
static int voltage_to_percent(int voltage, const short* table)
|
|
|
|
{
|
|
|
|
if (voltage <= table[0])
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
if (voltage >= table[10])
|
|
|
|
return 100;
|
2006-04-15 13:07:21 +00:00
|
|
|
else {
|
2005-03-23 20:53:37 +00:00
|
|
|
/* 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 */
|
2007-08-15 23:57:27 +00:00
|
|
|
static int voltage_to_battery_level(int battery_millivolts)
|
2005-03-23 20:53:37 +00:00
|
|
|
{
|
|
|
|
int level;
|
|
|
|
|
2007-08-17 06:45:18 +00:00
|
|
|
#if defined(CONFIG_CHARGER) \
|
|
|
|
&& (defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES))
|
|
|
|
/* Checking for iriver is a temporary kludge.
|
|
|
|
* This code needs rework/unification */
|
2006-11-11 01:18:57 +00:00
|
|
|
if (charger_input_state == NO_CHARGER) {
|
|
|
|
/* discharging. calculate new battery level and average with last */
|
2007-08-15 23:57:27 +00:00
|
|
|
level = voltage_to_percent(battery_millivolts,
|
2006-11-11 01:18:57 +00:00
|
|
|
percent_to_volt_discharge[battery_type]);
|
|
|
|
if (level != (battery_percent - 1))
|
|
|
|
level = (level + battery_percent + 1) / 2;
|
|
|
|
}
|
|
|
|
else if (charger_input_state == CHARGER_UNPLUGGED) {
|
|
|
|
/* just unplugged. adjust filtered values */
|
2007-08-15 23:57:27 +00:00
|
|
|
battery_millivolts -= percent_to_volt_charge[battery_percent/10] -
|
2006-11-11 01:18:57 +00:00
|
|
|
percent_to_volt_discharge[0][battery_percent/10];
|
2007-08-15 23:57:27 +00:00
|
|
|
avgbat = battery_millivolts * 1000 * BATT_AVE_SAMPLES;
|
2006-11-11 01:18:57 +00:00
|
|
|
level = battery_percent;
|
|
|
|
}
|
|
|
|
else if (charger_input_state == CHARGER_PLUGGED) {
|
|
|
|
/* just plugged in. adjust battery values */
|
2007-08-15 23:57:27 +00:00
|
|
|
battery_millivolts += percent_to_volt_charge[battery_percent/10] -
|
2006-11-11 01:18:57 +00:00
|
|
|
percent_to_volt_discharge[0][battery_percent/10];
|
2007-08-15 23:57:27 +00:00
|
|
|
avgbat = battery_millivolts * 1000 * BATT_AVE_SAMPLES;
|
2006-11-11 01:18:57 +00:00
|
|
|
level = MIN(12 * battery_percent / 10, 99);
|
|
|
|
}
|
|
|
|
else { /* charging. calculate new battery level */
|
2007-08-15 23:57:27 +00:00
|
|
|
level = voltage_to_percent(battery_millivolts,
|
2006-11-11 01:18:57 +00:00
|
|
|
percent_to_volt_charge);
|
|
|
|
}
|
|
|
|
#elif CONFIG_CHARGING >= CHARGING_MONITOR
|
2005-03-23 20:53:37 +00:00
|
|
|
if (charge_state == DISCHARGING) {
|
2007-08-15 23:57:27 +00:00
|
|
|
level = voltage_to_percent(battery_millivolts,
|
2006-10-30 11:33:38 +00:00
|
|
|
percent_to_volt_discharge[battery_type]);
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
|
|
|
else if (charge_state == CHARGING) {
|
2005-04-10 15:10:44 +00:00
|
|
|
/* battery level is defined to be < 100% until charging is finished */
|
2007-08-15 23:57:27 +00:00
|
|
|
level = MIN(voltage_to_percent(battery_millivolts,
|
2005-04-10 15:10:44 +00:00
|
|
|
percent_to_volt_charge), 99);
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
2006-11-11 01:18:57 +00:00
|
|
|
else { /* in topoff/trickle charge, battery is by definition 100% full */
|
2005-03-23 20:53:37 +00:00
|
|
|
level = 100;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* always use the discharge table */
|
2007-08-15 23:57:27 +00:00
|
|
|
level = voltage_to_percent(battery_millivolts,
|
2006-10-30 11:33:38 +00:00
|
|
|
percent_to_volt_discharge[battery_type]);
|
2005-03-23 20:53:37 +00:00
|
|
|
#endif
|
|
|
|
|
2006-10-30 11:33:38 +00:00
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void battery_status_update(void)
|
|
|
|
{
|
2007-08-15 23:57:27 +00:00
|
|
|
int level = voltage_to_battery_level(battery_millivolts);
|
2006-10-30 11:33:38 +00:00
|
|
|
|
2005-04-10 15:10:44 +00:00
|
|
|
/* calculate estimated remaining running time */
|
|
|
|
/* discharging: remaining running time */
|
|
|
|
/* charging: remaining charging time */
|
2006-06-06 22:23:52 +00:00
|
|
|
#if CONFIG_CHARGING >= CHARGING_MONITOR
|
2005-04-10 15:10:44 +00:00
|
|
|
if (charge_state == CHARGING) {
|
2006-11-11 01:18:57 +00:00
|
|
|
powermgmt_est_runningtime_min = (100 - level) * battery_capacity * 60
|
|
|
|
/ 100 / (CURRENT_MAX_CHG - runcurrent());
|
2005-04-10 15:10:44 +00:00
|
|
|
}
|
|
|
|
else
|
2007-08-17 06:45:18 +00:00
|
|
|
#elif CONFIG_CHARGING \
|
|
|
|
&& (defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES))
|
|
|
|
/* Checking for iriver is a temporary kludge.
|
|
|
|
* This code needs rework/unification */
|
2006-11-11 01:18:57 +00:00
|
|
|
if (charger_inserted()) {
|
|
|
|
#ifdef IRIVER_H300_SERIES
|
|
|
|
/* H300_SERIES use CURRENT_MAX_CHG for basic charge time (80%)
|
|
|
|
* plus 110 min top off charge time */
|
|
|
|
powermgmt_est_runningtime_min = ((100-level) * battery_capacity * 80
|
|
|
|
/100 / CURRENT_MAX_CHG) + 110;
|
|
|
|
#else
|
|
|
|
/* H100_SERIES scaled for 160 min basic charge time (80%) on
|
|
|
|
* 1600 mAh battery plus 110 min top off charge time */
|
|
|
|
powermgmt_est_runningtime_min = ((100 - level) * battery_capacity
|
|
|
|
/ 993) + 110;
|
|
|
|
#endif
|
|
|
|
level = (level * 80) / 100;
|
|
|
|
if (level > 72) { /* > 91% */
|
|
|
|
int i = POWER_HISTORY_LEN;
|
|
|
|
int d = 1;
|
|
|
|
#ifdef HAVE_CHARGE_STATE
|
|
|
|
if (charge_state == DISCHARGING)
|
|
|
|
d = -2;
|
2005-04-10 15:10:44 +00:00
|
|
|
#endif
|
2006-11-11 01:18:57 +00:00
|
|
|
while ((i > 2) && (d > 0)) /* search zero or neg. delta */
|
|
|
|
d = power_history[0] - power_history[--i];
|
|
|
|
if ((((d == 0) && (i > 6)) || (d == -1)) && (i < 118)) {
|
|
|
|
/* top off charging */
|
|
|
|
level = MIN(80 + (i*19 / 113), 99); /* show 81% .. 99% */
|
|
|
|
powermgmt_est_runningtime_min = MAX(116 - i, 0);
|
|
|
|
}
|
|
|
|
else if ((d < 0) || (i > 117)) {
|
|
|
|
/* charging finished */
|
|
|
|
level = 100;
|
|
|
|
powermgmt_est_runningtime_min = battery_capacity * 60
|
|
|
|
/ runcurrent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* BATT_LIPOL1300 */
|
2005-04-10 15:10:44 +00:00
|
|
|
{
|
2007-08-15 23:57:27 +00:00
|
|
|
if ((battery_millivolts + 20) > percent_to_volt_discharge[0][0])
|
2006-11-11 01:18:57 +00:00
|
|
|
powermgmt_est_runningtime_min = (level + battery_percent) * 60 *
|
|
|
|
battery_capacity / 200 / runcurrent();
|
2008-04-12 07:14:13 +00:00
|
|
|
|
|
|
|
else if (battery_millivolts <= battery_level_shutoff[0])
|
|
|
|
powermgmt_est_runningtime_min = 0;
|
|
|
|
|
2006-11-11 01:18:57 +00:00
|
|
|
else
|
2007-08-15 23:57:27 +00:00
|
|
|
powermgmt_est_runningtime_min = (battery_millivolts -
|
2006-11-11 01:18:57 +00:00
|
|
|
battery_level_shutoff[0]) / 2;
|
2005-04-10 15:10:44 +00:00
|
|
|
}
|
2006-11-11 01:18:57 +00:00
|
|
|
|
|
|
|
battery_percent = level;
|
2007-11-03 05:00:49 +00:00
|
|
|
send_battery_level_event();
|
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
|
2006-11-11 01:18:57 +00:00
|
|
|
* 3) The battery level has reached shutdown limit
|
2005-03-23 20:53:37 +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
|
2006-08-28 22:38:41 +00:00
|
|
|
* 4) The radio is playing
|
2005-03-23 20:53:37 +00:00
|
|
|
*/
|
2002-09-23 11:42:48 +00:00
|
|
|
static void handle_auto_poweroff(void)
|
|
|
|
{
|
2007-11-25 20:05:04 +00:00
|
|
|
long timeout = 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
|
|
|
|
2007-02-18 05:32:06 +00:00
|
|
|
#if CONFIG_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.
|
|
|
|
*/
|
2005-12-06 13:27:15 +00:00
|
|
|
if(charger_input_state == CHARGER || audio_stat == AUDIO_STATUS_PLAY) {
|
2005-03-23 20:53:37 +00:00
|
|
|
last_event_tick = current_tick;
|
2002-09-26 22:34:35 +00:00
|
|
|
}
|
2005-03-03 07:25:43 +00:00
|
|
|
#endif
|
2005-08-06 10:12:19 +00:00
|
|
|
|
2007-08-17 06:45:18 +00:00
|
|
|
#ifndef NO_LOW_BATTERY_SHUTDOWN
|
2006-11-11 01:18:57 +00:00
|
|
|
/* switch off unit if battery level is too low for reliable operation */
|
2007-08-15 23:57:27 +00:00
|
|
|
if(battery_millivolts < battery_level_shutoff[battery_type]) {
|
2006-11-11 01:18:57 +00:00
|
|
|
if(!shutdown_timeout) {
|
|
|
|
backlight_on();
|
|
|
|
sys_poweroff();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2002-09-24 07:50:17 +00:00
|
|
|
if(timeout &&
|
2007-02-18 02:04:47 +00:00
|
|
|
#if CONFIG_TUNER && !defined(BOOTLOADER)
|
2006-08-28 22:38:41 +00:00
|
|
|
(!(get_radio_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-09-14 09:08:26 +00:00
|
|
|
sys_poweroff();
|
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();
|
2007-02-18 05:32:06 +00:00
|
|
|
#if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_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");
|
2005-09-14 09:08:26 +00:00
|
|
|
sys_poweroff();
|
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-06-23 02:18:29 +00:00
|
|
|
if(usb_inserted()
|
2006-03-16 18:25:45 +00:00
|
|
|
#if defined(HAVE_USB_POWER)
|
|
|
|
#if (CURRENT_USB < CURRENT_NORMAL)
|
2005-06-23 02:18:29 +00:00
|
|
|
|| usb_powered()
|
2006-03-16 18:25:45 +00:00
|
|
|
#else
|
2006-03-16 17:34:37 +00:00
|
|
|
&& !usb_powered()
|
2006-03-16 18:25:45 +00:00
|
|
|
#endif
|
2005-06-23 02:18:29 +00:00
|
|
|
#endif
|
|
|
|
)
|
|
|
|
{
|
2005-03-03 07:25:43 +00:00
|
|
|
current = CURRENT_USB;
|
|
|
|
}
|
2005-04-10 15:10:44 +00:00
|
|
|
|
2007-04-12 22:12:13 +00:00
|
|
|
#if defined(HAVE_BACKLIGHT) && !defined(BOOTLOADER)
|
2005-11-23 20:12:33 +00:00
|
|
|
if (backlight_get_current_timeout() == 0) /* LED always on */
|
2005-03-03 07:25:43 +00:00
|
|
|
current += CURRENT_BACKLIGHT;
|
2005-07-11 20:29:05 +00:00
|
|
|
#endif
|
2005-03-03 07:25:43 +00:00
|
|
|
|
2006-11-11 01:18:57 +00:00
|
|
|
#if defined(HAVE_RECORDING) && defined(CURRENT_RECORD)
|
2006-11-14 02:46:59 +00:00
|
|
|
if (audio_status() & AUDIO_STATUS_RECORD)
|
2006-11-11 01:18:57 +00:00
|
|
|
current += CURRENT_RECORD;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_SPDIF_POWER
|
2006-11-14 02:46:59 +00:00
|
|
|
if (spdif_powered())
|
2006-11-11 01:18:57 +00:00
|
|
|
current += CURRENT_SPDIF_OUT;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_REMOTE_LCD
|
2006-11-14 02:46:59 +00:00
|
|
|
if (remote_detect())
|
2006-11-11 01:18:57 +00:00
|
|
|
current += CURRENT_REMOTE;
|
|
|
|
#endif
|
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
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 */
|
2007-02-28 21:55:11 +00:00
|
|
|
#ifdef HAVE_RTC_ALARM
|
2006-04-15 13:07:21 +00:00
|
|
|
static void power_thread_rtc_process(void)
|
2005-02-06 17:21:42 +00:00
|
|
|
{
|
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
|
|
|
|
2007-02-18 05:32:06 +00:00
|
|
|
#if CONFIG_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.
|
|
|
|
*/
|
2006-03-02 13:37:51 +00:00
|
|
|
if(charger_inserted()
|
2007-02-01 10:47:22 +00:00
|
|
|
#ifdef HAVE_USB_POWER /* USB powered or USB inserted both provide power */
|
2006-03-02 13:37:51 +00:00
|
|
|
|| usb_powered()
|
2007-02-18 05:32:06 +00:00
|
|
|
#if CONFIG_CHARGING
|
2007-02-01 10:47:22 +00:00
|
|
|
|| (usb_inserted() && usb_charging_enabled())
|
|
|
|
#endif
|
2006-03-02 13:37:51 +00:00
|
|
|
#endif
|
|
|
|
) {
|
2005-03-23 20:53:37 +00:00
|
|
|
switch(charger_input_state) {
|
|
|
|
case NO_CHARGER:
|
|
|
|
case CHARGER_UNPLUGGED:
|
|
|
|
charger_input_state = CHARGER_PLUGGED;
|
|
|
|
return;
|
|
|
|
case CHARGER_PLUGGED:
|
2006-12-19 16:50:07 +00:00
|
|
|
queue_broadcast(SYS_CHARGER_CONNECTED, 0);
|
2007-11-03 05:00:49 +00:00
|
|
|
last_sent_battery_level = 0;
|
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:
|
2006-12-19 16:50:07 +00:00
|
|
|
queue_broadcast(SYS_CHARGER_DISCONNECTED, 0);
|
2007-11-03 05:00:49 +00:00
|
|
|
last_sent_battery_level = 100;
|
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
|
2006-06-06 22:23:52 +00:00
|
|
|
#if CONFIG_CHARGING == CHARGING_MONITOR
|
2006-01-19 07:47:34 +00:00
|
|
|
switch (charger_input_state) {
|
|
|
|
case CHARGER_UNPLUGGED:
|
|
|
|
case NO_CHARGER:
|
2006-03-13 16:11:30 +00:00
|
|
|
charge_state = DISCHARGING;
|
2006-01-19 07:47:34 +00:00
|
|
|
break;
|
|
|
|
case CHARGER_PLUGGED:
|
|
|
|
case CHARGER:
|
|
|
|
if (charging_state()) {
|
|
|
|
charge_state = CHARGING;
|
|
|
|
} else {
|
|
|
|
charge_state = DISCHARGING;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2006-04-15 13:07:21 +00:00
|
|
|
|
2006-06-06 22:23:52 +00:00
|
|
|
#endif /* CONFIG_CHARGING == CHARGING_MONITOR */
|
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-09-14 09:08:26 +00:00
|
|
|
/* If the power off timeout expires, the main thread has failed
|
|
|
|
to shut down the system, and we need to force a power off */
|
|
|
|
if(shutdown_timeout) {
|
|
|
|
shutdown_timeout -= small_ticks;
|
|
|
|
if(shutdown_timeout <= 0)
|
|
|
|
power_off();
|
|
|
|
}
|
|
|
|
|
2007-02-28 21:55:11 +00:00
|
|
|
#ifdef HAVE_RTC_ALARM
|
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()) {
|
2007-08-17 06:45:18 +00:00
|
|
|
avgbat += battery_adc_voltage() - (avgbat / BATT_AVE_SAMPLES);
|
2005-03-03 07:25:43 +00:00
|
|
|
/*
|
2007-08-15 23:57:27 +00:00
|
|
|
* battery_millivolts is the millivolt-scaled filtered battery value.
|
2005-03-03 07:25:43 +00:00
|
|
|
*/
|
2007-08-17 06:45:18 +00:00
|
|
|
battery_millivolts = avgbat / BATT_AVE_SAMPLES;
|
2007-01-14 14:12:48 +00:00
|
|
|
|
2006-11-15 21:41:17 +00:00
|
|
|
/* update battery status every time an update is available */
|
|
|
|
battery_status_update();
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
2006-11-11 01:18:57 +00:00
|
|
|
else if (battery_percent < 8) {
|
|
|
|
/* If battery is low, observe voltage during disk activity.
|
|
|
|
* Shut down if voltage drops below shutoff level and we are not
|
|
|
|
* using NiMH or Alkaline batteries.
|
|
|
|
*/
|
2007-08-15 23:57:27 +00:00
|
|
|
battery_millivolts = (battery_adc_voltage() +
|
|
|
|
battery_millivolts + 1) / 2;
|
2006-11-15 21:41:17 +00:00
|
|
|
|
|
|
|
/* update battery status every time an update is available */
|
|
|
|
battery_status_update();
|
2007-01-14 14:12:48 +00:00
|
|
|
|
2007-08-17 06:45:18 +00:00
|
|
|
#ifndef NO_LOW_BATTERY_SHUTDOWN
|
2006-11-11 01:18:57 +00:00
|
|
|
if (!shutdown_timeout &&
|
2007-08-15 23:57:27 +00:00
|
|
|
(battery_millivolts < battery_level_shutoff[battery_type]))
|
2006-11-11 01:18:57 +00:00
|
|
|
sys_poweroff();
|
|
|
|
else
|
|
|
|
#endif
|
2007-08-17 06:45:18 +00:00
|
|
|
avgbat += battery_millivolts - (avgbat / BATT_AVE_SAMPLES);
|
2006-11-11 01:18:57 +00:00
|
|
|
}
|
|
|
|
|
2006-06-06 22:23:52 +00:00
|
|
|
#if CONFIG_CHARGING == CHARGING_CONTROL
|
2006-03-04 09:48:53 +00:00
|
|
|
if (ata_disk_is_active()) {
|
|
|
|
/* flag hdd use for charging calculation */
|
|
|
|
disk_activity_last_cycle = true;
|
|
|
|
}
|
|
|
|
#endif
|
2006-06-06 22:23:52 +00:00
|
|
|
#if defined(DEBUG_FILE) && (CONFIG_CHARGING == CHARGING_CONTROL)
|
2005-03-23 20:53:37 +00:00
|
|
|
/*
|
|
|
|
* If we have a lot of pending writes or if the disk is spining,
|
|
|
|
* fsync the debug log file.
|
|
|
|
*/
|
2006-11-11 01:18:57 +00:00
|
|
|
if((wrcount > 10) || ((wrcount > 0) && ata_disk_is_active())) {
|
2005-03-23 20:53:37 +00:00
|
|
|
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)
|
|
|
|
{
|
2006-06-06 22:23:52 +00:00
|
|
|
#if CONFIG_CHARGING == CHARGING_CONTROL
|
2007-08-15 23:57:27 +00:00
|
|
|
int i;
|
2006-03-04 09:48:53 +00:00
|
|
|
unsigned int target_voltage = TRICKLE_VOLTAGE; /* desired topoff/trickle
|
|
|
|
* voltage level */
|
|
|
|
int charge_max_time_idle = 0; /* max. charging duration, calculated at
|
|
|
|
* beginning of charging */
|
|
|
|
int charge_max_time_now = 0; /* max. charging duration including
|
|
|
|
* hdd activity */
|
|
|
|
int minutes_disk_activity = 0; /* count minutes of hdd use during
|
|
|
|
* charging */
|
|
|
|
int last_disk_activity = CHARGE_END_LONGD + 1; /* last hdd use x mins ago */
|
2002-08-06 10:50:50 +00:00
|
|
|
#endif
|
2005-03-03 07:25:43 +00:00
|
|
|
|
2007-11-30 10:55:09 +00:00
|
|
|
/* Delay reading the first battery level */
|
2008-03-11 09:38:12 +00:00
|
|
|
#ifdef MROBE_100
|
|
|
|
while(battery_adc_voltage()>4200) /* gives false readings initially */
|
|
|
|
#endif
|
2007-11-30 10:55:09 +00:00
|
|
|
sleep(HZ/100);
|
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
/* initialize the voltages for the exponential filter */
|
2007-08-17 06:45:18 +00:00
|
|
|
avgbat = battery_adc_voltage() + 15;
|
2006-11-11 01:18:57 +00:00
|
|
|
|
|
|
|
#ifndef HAVE_MMC /* this adjustment is only needed for HD based */
|
|
|
|
/* The battery voltage is usually a little lower directly after
|
|
|
|
turning on, because the disk was used heavily. Raise it by 5% */
|
|
|
|
#ifdef HAVE_CHARGING
|
|
|
|
if(!charger_inserted()) /* only if charger not connected */
|
|
|
|
#endif
|
|
|
|
avgbat += (percent_to_volt_discharge[battery_type][6] -
|
2007-08-17 06:45:18 +00:00
|
|
|
percent_to_volt_discharge[battery_type][5]) / 2;
|
2006-11-11 01:18:57 +00:00
|
|
|
#endif /* not HAVE_MMC */
|
|
|
|
|
|
|
|
avgbat = avgbat * BATT_AVE_SAMPLES;
|
2007-08-17 06:45:18 +00:00
|
|
|
battery_millivolts = avgbat / BATT_AVE_SAMPLES;
|
2005-03-03 07:25:43 +00:00
|
|
|
|
2007-02-18 05:32:06 +00:00
|
|
|
#if CONFIG_CHARGING
|
2006-11-11 01:18:57 +00:00
|
|
|
if(charger_inserted()) {
|
2007-08-15 23:57:27 +00:00
|
|
|
battery_percent = voltage_to_percent(battery_millivolts,
|
2006-11-11 01:18:57 +00:00
|
|
|
percent_to_volt_charge);
|
2007-08-17 06:45:18 +00:00
|
|
|
#if defined(IRIVER_H100_SERIES) || defined(IRIVER_H300_SERIES)
|
|
|
|
/* Checking for iriver is a temporary kludge. */
|
2006-11-11 01:18:57 +00:00
|
|
|
charger_input_state = CHARGER;
|
|
|
|
#endif
|
|
|
|
} else
|
|
|
|
#endif
|
2007-08-15 23:57:27 +00:00
|
|
|
{ battery_percent = voltage_to_percent(battery_millivolts,
|
2006-11-11 01:18:57 +00:00
|
|
|
percent_to_volt_discharge[battery_type]);
|
|
|
|
battery_percent += (battery_percent < 100);
|
|
|
|
}
|
|
|
|
|
2006-06-06 22:23:52 +00:00
|
|
|
#if defined(DEBUG_FILE) && (CONFIG_CHARGING == CHARGING_CONTROL)
|
2005-03-23 20:53:37 +00:00
|
|
|
fd = -1;
|
|
|
|
wrcount = 0;
|
2005-03-03 07:25:43 +00:00
|
|
|
#endif
|
2006-04-15 13:07:21 +00:00
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
/* rotate the power history */
|
2007-08-15 23:57:27 +00:00
|
|
|
memmove(power_history + 1, power_history,
|
|
|
|
sizeof(power_history) - sizeof(power_history[0]));
|
2006-04-15 13:07:21 +00:00
|
|
|
|
2007-08-15 23:57:27 +00:00
|
|
|
/* insert new value at the start, in millivolts 8-) */
|
|
|
|
power_history[0] = battery_millivolts;
|
2005-03-03 07:25:43 +00:00
|
|
|
|
2006-06-06 22:23:52 +00:00
|
|
|
#if CONFIG_CHARGING == CHARGING_CONTROL
|
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;
|
2006-03-04 09:48:53 +00:00
|
|
|
target_voltage = TRICKLE_VOLTAGE;
|
2005-03-23 20:53:37 +00:00
|
|
|
} else {
|
|
|
|
charge_state = TOPOFF;
|
2006-03-04 09:48:53 +00:00
|
|
|
target_voltage = TOPOFF_VOLTAGE;
|
2006-04-15 13:07:21 +00:00
|
|
|
}
|
2005-03-23 20:53:37 +00:00
|
|
|
} 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;
|
2006-03-04 09:48:53 +00:00
|
|
|
charge_max_time_idle =
|
2005-03-23 20:53:37 +00:00
|
|
|
i * (100 + 35 - battery_percent) / 100;
|
2006-03-04 09:48:53 +00:00
|
|
|
if (charge_max_time_idle > i) {
|
|
|
|
charge_max_time_idle = i;
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
2006-04-15 13:07:21 +00:00
|
|
|
charge_max_time_now = charge_max_time_idle;
|
|
|
|
|
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) {
|
2006-03-04 09:48:53 +00:00
|
|
|
/* alter charge time max length with extra disk use */
|
|
|
|
if (disk_activity_last_cycle) {
|
|
|
|
minutes_disk_activity++;
|
|
|
|
charge_max_time_now = charge_max_time_idle +
|
|
|
|
(minutes_disk_activity * 2 / 5);
|
|
|
|
disk_activity_last_cycle = false;
|
|
|
|
last_disk_activity = 0;
|
|
|
|
} else {
|
|
|
|
last_disk_activity++;
|
|
|
|
}
|
2005-03-23 20:53:37 +00:00
|
|
|
/*
|
|
|
|
* Check the delta voltage over the last X minutes so we can do
|
2006-03-04 09:48:53 +00:00
|
|
|
* our end-of-charge logic based on the battery level change.
|
|
|
|
*(no longer use minimum time as logic for charge end has 50
|
|
|
|
* minutes minimum charge built in)
|
2005-03-23 20:53:37 +00:00
|
|
|
*/
|
2006-03-04 09:48:53 +00:00
|
|
|
if (powermgmt_last_cycle_startstop_min > CHARGE_END_SHORTD) {
|
2005-03-23 20:53:37 +00:00
|
|
|
short_delta = power_history[0] -
|
2006-03-04 09:48:53 +00:00
|
|
|
power_history[CHARGE_END_SHORTD - 1];
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
2006-03-04 09:48:53 +00:00
|
|
|
|
|
|
|
if (powermgmt_last_cycle_startstop_min > CHARGE_END_LONGD) {
|
2005-03-23 20:53:37 +00:00
|
|
|
/*
|
2006-03-04 09:48:53 +00:00
|
|
|
* Scan the history: the points where measurement is taken need to
|
|
|
|
* be fairly static. (check prior to short delta 'area')
|
|
|
|
* (also only check first and last 10 cycles - delta in middle OK)
|
2005-03-23 20:53:37 +00:00
|
|
|
*/
|
|
|
|
long_delta = power_history[0] -
|
2006-03-04 09:48:53 +00:00
|
|
|
power_history[CHARGE_END_LONGD - 1];
|
|
|
|
|
|
|
|
for(i = CHARGE_END_SHORTD; i < CHARGE_END_SHORTD + 10; i++) {
|
2007-08-15 23:57:27 +00:00
|
|
|
if(((power_history[i] - power_history[i+1]) > 50) ||
|
|
|
|
((power_history[i] - power_history[i+1]) < -50)) {
|
2006-03-04 09:48:53 +00:00
|
|
|
long_delta = 777777;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i = CHARGE_END_LONGD - 11; i < CHARGE_END_LONGD - 1 ; i++) {
|
2007-08-15 23:57:27 +00:00
|
|
|
if(((power_history[i] - power_history[i+1]) > 50) ||
|
|
|
|
((power_history[i] - power_history[i+1]) < -50)) {
|
2005-03-23 20:53:37 +00:00
|
|
|
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
|
|
|
|
2006-03-04 09:48:53 +00:00
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
|
|
|
"Chg %dm, max %dm", powermgmt_last_cycle_startstop_min,
|
|
|
|
charge_max_time_now);
|
2005-03-23 20:53:37 +00:00
|
|
|
/*
|
|
|
|
* End of charge criteria (any qualify):
|
|
|
|
* 1) Charged a long time
|
2006-03-04 09:48:53 +00:00
|
|
|
* 2) DeltaV went negative for a short time ( & long delta static)
|
|
|
|
* 3) DeltaV was negative over a longer period (no disk use only)
|
2007-08-15 23:57:27 +00:00
|
|
|
* Note: short_delta and long_delta are millivolts
|
2005-03-23 20:53:37 +00:00
|
|
|
*/
|
2006-03-04 09:48:53 +00:00
|
|
|
if ((powermgmt_last_cycle_startstop_min >= charge_max_time_now) ||
|
2007-08-15 23:57:27 +00:00
|
|
|
(short_delta <= -50 && long_delta < 50 ) || (long_delta < -20 &&
|
2006-03-04 09:48:53 +00:00
|
|
|
last_disk_activity > CHARGE_END_LONGD)) {
|
2005-03-23 20:53:37 +00:00
|
|
|
if (powermgmt_last_cycle_startstop_min > charge_max_time_now) {
|
|
|
|
DEBUGF("power: powermgmt_last_cycle_startstop_min > charge_max_time_now, "
|
|
|
|
"enough!\n");
|
2006-03-04 09:48:53 +00:00
|
|
|
/*
|
|
|
|
*have charged too long and deltaV detection did not
|
|
|
|
*work!
|
|
|
|
*/
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN,
|
2005-03-23 20:53:37 +00:00
|
|
|
"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;
|
2006-03-04 09:48:53 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* set trickle charge target to a relative voltage instead
|
|
|
|
* of an arbitrary value - the fully charged voltage may
|
|
|
|
* vary according to ambient temp, battery condition etc
|
|
|
|
* trickle target is -0.15v from full voltage acheived
|
|
|
|
* topup target is -0.05v from full voltage
|
|
|
|
*/
|
2007-08-15 23:57:27 +00:00
|
|
|
target_voltage = power_history[0] - 150;
|
2006-03-04 09:48:53 +00:00
|
|
|
|
2005-03-23 20:53:37 +00:00
|
|
|
} 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);
|
2006-03-04 09:48:53 +00:00
|
|
|
target_voltage = power_history[CHARGE_END_SHORTD - 1]
|
2007-08-15 23:57:27 +00:00
|
|
|
- 50;
|
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);
|
2006-03-04 09:48:53 +00:00
|
|
|
target_voltage = power_history[CHARGE_END_LONGD - 1]
|
2007-08-15 23:57:27 +00:00
|
|
|
- 50;
|
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
|
|
|
}
|
2006-06-06 22:23:52 +00:00
|
|
|
else if (charge_state != DISCHARGING) /* top off or trickle */
|
2005-03-23 20:53:37 +00:00
|
|
|
{
|
2006-03-04 09:48:53 +00:00
|
|
|
/*
|
|
|
|
*Time to switch from topoff to trickle?
|
2005-03-23 20:53:37 +00:00
|
|
|
*/
|
|
|
|
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;
|
2007-08-15 23:57:27 +00:00
|
|
|
target_voltage = target_voltage - 100;
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 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].
|
|
|
|
*/
|
2006-04-15 13:07:21 +00:00
|
|
|
|
2007-08-16 17:34:55 +00:00
|
|
|
pid_p = ((signed)target_voltage - (signed)battery_millivolts) / 5;
|
2007-08-15 23:57:27 +00:00
|
|
|
if((pid_p <= PID_DEADZONE) && (pid_p >= -PID_DEADZONE))
|
2005-03-23 20:53:37 +00:00
|
|
|
pid_p = 0;
|
2007-08-15 23:57:27 +00:00
|
|
|
|
|
|
|
if((unsigned) battery_millivolts < target_voltage) {
|
2005-03-23 20:53:37 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2006-06-06 22:23:52 +00:00
|
|
|
#endif /* CONFIG_CHARGING == CHARGING_CONTROL */
|
2005-03-03 07:25:43 +00:00
|
|
|
|
|
|
|
/* sleep for a minute */
|
|
|
|
|
2006-06-06 22:23:52 +00:00
|
|
|
#if CONFIG_CHARGING == CHARGING_CONTROL
|
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
|
|
|
|
2006-06-06 22:23:52 +00:00
|
|
|
#if defined(DEBUG_FILE) && (CONFIG_CHARGING == CHARGING_CONTROL)
|
2005-03-23 20:53:37 +00:00
|
|
|
if(usb_inserted()) {
|
|
|
|
if(fd >= 0) {
|
2006-01-19 07:47:34 +00:00
|
|
|
/* It is probably too late to close the file but we can try...*/
|
2005-03-23 20:53:37 +00:00
|
|
|
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,
|
2007-08-15 23:57:27 +00:00
|
|
|
"cycle_min, bat_millivolts, bat_percent, chgr_state, charge_state, pid_p, pid_i, trickle_sec\n");
|
2005-03-23 20:53:37 +00:00
|
|
|
write(fd, debug_message, strlen(debug_message));
|
|
|
|
wrcount = 99; /* force a flush */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(fd >= 0) {
|
2006-04-15 13:07:21 +00:00
|
|
|
snprintf(debug_message, DEBUG_MESSAGE_LEN,
|
2006-01-19 07:47:34 +00:00
|
|
|
"%d, %d, %d, %d, %d, %d, %d, %d\n",
|
2007-08-15 23:57:27 +00:00
|
|
|
powermgmt_last_cycle_startstop_min, battery_millivolts,
|
2006-01-19 07:47:34 +00:00
|
|
|
battery_percent, charger_input_state, charge_state,
|
|
|
|
pid_p, pid_i, trickle_sec);
|
2005-03-23 20:53:37 +00:00
|
|
|
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
|
|
|
|
2006-06-06 22:23:52 +00:00
|
|
|
#if CONFIG_CHARGING == CHARGING_CONTROL
|
2005-03-23 20:53:37 +00:00
|
|
|
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
|
|
|
{
|
|
|
|
/* init history to 0 */
|
|
|
|
memset(power_history, 0x00, sizeof(power_history));
|
2007-10-16 01:25:17 +00:00
|
|
|
create_thread(power_thread, power_stack, sizeof(power_stack), 0,
|
2007-03-04 20:06:41 +00:00
|
|
|
power_thread_name IF_PRIO(, PRIORITY_SYSTEM)
|
2008-04-20 19:00:52 +00:00
|
|
|
IF_COP(, CPU));
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
|
|
|
|
2002-08-07 11:30:39 +00:00
|
|
|
#endif /* SIMULATOR */
|
|
|
|
|
2005-09-14 09:08:26 +00:00
|
|
|
void sys_poweroff(void)
|
2005-07-05 00:03:18 +00:00
|
|
|
{
|
2005-09-14 09:08:26 +00:00
|
|
|
logf("sys_poweroff()");
|
|
|
|
/* If the main thread fails to shut down the system, we will force a
|
2006-12-07 19:34:26 +00:00
|
|
|
power off after an 20 second timeout - 28 seconds if recording */
|
|
|
|
if (shutdown_timeout == 0)
|
2006-12-07 19:46:36 +00:00
|
|
|
{
|
2007-03-05 01:31:26 +00:00
|
|
|
#if (defined(IAUDIO_X5) || defined(IAUDIO_M5)) && !defined (SIMULATOR)
|
2006-12-07 19:34:26 +00:00
|
|
|
pcf50606_reset_timeout(); /* Reset timer on first attempt only */
|
2006-11-11 01:18:57 +00:00
|
|
|
#endif
|
2006-12-07 19:34:26 +00:00
|
|
|
#ifdef HAVE_RECORDING
|
2006-12-07 19:46:36 +00:00
|
|
|
if (audio_status() & AUDIO_STATUS_RECORD)
|
|
|
|
shutdown_timeout += HZ*8;
|
2006-12-07 19:34:26 +00:00
|
|
|
#endif
|
2006-12-07 19:46:36 +00:00
|
|
|
shutdown_timeout += HZ*20;
|
|
|
|
}
|
2006-12-07 19:34:26 +00:00
|
|
|
|
2007-08-12 11:18:52 +00:00
|
|
|
queue_broadcast(SYS_POWEROFF, 0);
|
2005-07-05 00:03:18 +00:00
|
|
|
}
|
|
|
|
|
2006-04-16 17:32:54 +00:00
|
|
|
void cancel_shutdown(void)
|
|
|
|
{
|
|
|
|
logf("sys_cancel_shutdown()");
|
2006-09-12 14:20:29 +00:00
|
|
|
|
2007-03-05 01:31:26 +00:00
|
|
|
#if (defined(IAUDIO_X5) || defined(IAUDIO_M5)) && !defined (SIMULATOR)
|
2006-09-12 14:20:29 +00:00
|
|
|
/* TODO: Move some things to target/ tree */
|
|
|
|
if (shutdown_timeout)
|
|
|
|
pcf50606_reset_timeout();
|
|
|
|
#endif
|
|
|
|
|
2006-04-16 17:32:54 +00:00
|
|
|
shutdown_timeout = 0;
|
|
|
|
}
|
|
|
|
|
2005-02-06 17:21:42 +00:00
|
|
|
/* Various hardware housekeeping tasks relating to shutting down the jukebox */
|
2006-04-15 13:07:21 +00:00
|
|
|
void shutdown_hw(void)
|
2005-02-06 17:21:42 +00:00
|
|
|
{
|
2005-03-24 07:31:59 +00:00
|
|
|
#ifndef SIMULATOR
|
2006-06-06 22:23:52 +00:00
|
|
|
#if defined(DEBUG_FILE) && (CONFIG_CHARGING == CHARGING_CONTROL)
|
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();
|
2007-06-29 19:01:24 +00:00
|
|
|
if (battery_level_safe()) { /* do not save on critical battery */
|
2005-12-06 13:27:15 +00:00
|
|
|
#ifdef HAVE_LCD_BITMAP
|
2006-11-11 01:18:57 +00:00
|
|
|
glyph_cache_save();
|
2005-12-06 13:27:15 +00:00
|
|
|
#endif
|
2006-11-11 01:18:57 +00:00
|
|
|
if(ata_disk_is_active())
|
|
|
|
ata_spindown(1);
|
|
|
|
}
|
2005-02-06 09:57:57 +00:00
|
|
|
while(ata_disk_is_active())
|
2005-03-03 07:25:43 +00:00
|
|
|
sleep(HZ/10);
|
2005-02-06 09:57:57 +00:00
|
|
|
|
2007-03-09 12:51:25 +00:00
|
|
|
#if CONFIG_CODEC != SWCODEC
|
2005-02-06 09:57:57 +00:00
|
|
|
mp3_shutdown();
|
2007-05-03 18:08:00 +00:00
|
|
|
#else
|
2006-12-06 10:24:59 +00:00
|
|
|
audiohw_close();
|
2006-04-04 19:19:28 +00:00
|
|
|
#endif
|
2007-05-03 18:08:00 +00:00
|
|
|
|
2006-11-11 01:18:57 +00:00
|
|
|
/* If HD is still active we try to wait for spindown, otherwise the
|
2007-01-14 14:12:48 +00:00
|
|
|
shutdown_timeout in power_thread_sleep will force a power off */
|
2006-11-11 01:18:57 +00:00
|
|
|
while(ata_disk_is_active())
|
|
|
|
sleep(HZ/10);
|
2006-08-08 22:03:56 +00:00
|
|
|
#ifndef IAUDIO_X5
|
2005-02-06 09:57:57 +00:00
|
|
|
lcd_set_contrast(0);
|
2006-10-25 23:24:07 +00:00
|
|
|
#endif /* IAUDIO_X5 */
|
2005-11-19 01:30:42 +00:00
|
|
|
#ifdef HAVE_REMOTE_LCD
|
2005-11-21 23:55:39 +00:00
|
|
|
lcd_remote_set_contrast(0);
|
2005-02-06 09:57:57 +00:00
|
|
|
#endif
|
2007-01-14 14:12:48 +00:00
|
|
|
|
2007-09-29 20:56:07 +00:00
|
|
|
#ifdef HAVE_LCD_SHUTDOWN
|
|
|
|
lcd_shutdown();
|
|
|
|
#endif
|
|
|
|
|
2006-12-07 10:34:25 +00:00
|
|
|
/* Small delay to make sure all HW gets time to flush. Especially
|
|
|
|
eeprom chips are quite slow and might be still writing the last
|
|
|
|
byte. */
|
|
|
|
sleep(HZ/4);
|
2005-02-06 09:57:57 +00:00
|
|
|
power_off();
|
2005-03-24 07:31:59 +00:00
|
|
|
#endif /* #ifndef SIMULATOR */
|
2005-02-06 09:57:57 +00:00
|
|
|
}
|
2007-11-03 05:00:49 +00:00
|
|
|
|
2007-11-03 13:45:12 +00:00
|
|
|
/* Send system battery level update events on reaching certain significant
|
2007-11-03 13:21:40 +00:00
|
|
|
levels. This must be called after battery_percent has been updated. */
|
2007-11-03 05:00:49 +00:00
|
|
|
static void send_battery_level_event(void)
|
|
|
|
{
|
2007-11-08 01:18:13 +00:00
|
|
|
static const int levels[] = { 5, 15, 30, 50, 0 };
|
2007-11-03 05:00:49 +00:00
|
|
|
const int *level = levels;
|
|
|
|
while (*level)
|
|
|
|
{
|
2007-11-03 13:21:40 +00:00
|
|
|
if (battery_percent <= *level && last_sent_battery_level > *level)
|
|
|
|
{
|
2007-11-03 05:00:49 +00:00
|
|
|
last_sent_battery_level = *level;
|
|
|
|
queue_broadcast(SYS_BATTERY_UPDATE, last_sent_battery_level);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
level++;
|
|
|
|
}
|
|
|
|
}
|