2002-08-06 10:50:50 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
2002-12-14 15:48:08 +00:00
|
|
|
* Copyright (C) 2002 by Heikki Hannikainen, Uwe Freese
|
2002-08-06 10:50:50 +00:00
|
|
|
*
|
|
|
|
* All files in this archive are subject to the GNU General Public License.
|
|
|
|
* See the file COPYING in the source tree root for full license agreement.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "config.h"
|
|
|
|
#include "sh7034.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "system.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "panic.h"
|
|
|
|
#include "adc.h"
|
|
|
|
#include "string.h"
|
2002-08-06 13:06:41 +00:00
|
|
|
#include "sprintf.h"
|
2002-08-12 12:19:25 +00:00
|
|
|
#include "ata.h"
|
2002-08-06 10:50:50 +00:00
|
|
|
#include "power.h"
|
2002-09-23 11:42:48 +00:00
|
|
|
#include "button.h"
|
|
|
|
#include "ata.h"
|
|
|
|
#include "mpeg.h"
|
2002-09-25 19:25:10 +00:00
|
|
|
#include "usb.h"
|
2002-08-06 10:50:50 +00:00
|
|
|
#include "powermgmt.h"
|
2002-12-14 12:10:29 +00:00
|
|
|
#include "../apps/settings.h"
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2002-08-07 11:30:39 +00:00
|
|
|
#ifdef SIMULATOR
|
|
|
|
|
|
|
|
int battery_level(void)
|
|
|
|
{
|
|
|
|
return 100;
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2002-09-23 12:21:51 +00:00
|
|
|
void set_poweroff_timeout(int timeout)
|
|
|
|
{
|
|
|
|
(void)timeout;
|
|
|
|
}
|
|
|
|
|
2002-08-07 11:30:39 +00:00
|
|
|
#else /* not SIMULATOR */
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2002-09-23 11:42:48 +00:00
|
|
|
static int poweroff_idle_timeout_value[15] =
|
|
|
|
{
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 30, 45, 60
|
|
|
|
};
|
|
|
|
|
2002-12-03 14:18:51 +00:00
|
|
|
static int percent_to_volt_nocharge[11] = /* voltages (centivolt) of 0%, 10%, ... 100% when charging disabled */
|
|
|
|
{
|
|
|
|
450, 481, 491, 497, 503, 507, 512, 514, 517, 528, 560
|
|
|
|
};
|
|
|
|
|
2002-12-03 14:33:20 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
2002-12-03 14:18:51 +00:00
|
|
|
static int percent_to_volt_charge[11] = /* voltages (centivolt) of 0%, 10%, ... 100% when charging enabled */
|
|
|
|
{
|
|
|
|
476, 544, 551, 556, 561, 564, 566, 576, 582, 584, 585
|
|
|
|
};
|
2002-12-03 14:33:20 +00:00
|
|
|
#endif
|
2002-12-03 14:18:51 +00:00
|
|
|
|
|
|
|
int battery_lazyness[20] = /* how does the battery react when plugging in/out the charger */
|
|
|
|
{
|
|
|
|
0, 17, 31, 42, 52, 60, 67, 72, 77, 81, 84, 87, 89, 91, 92, 94, 95, 95, 96, 97
|
|
|
|
};
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
static char power_stack[DEFAULT_STACK_SIZE];
|
|
|
|
static char power_thread_name[] = "power";
|
|
|
|
|
2002-09-23 11:42:48 +00:00
|
|
|
static int poweroff_timeout = 0;
|
2002-09-24 07:50:17 +00:00
|
|
|
static long last_charge_time = 0;
|
2002-12-03 14:18:51 +00:00
|
|
|
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;
|
|
|
|
static unsigned long sleeptimer_endtick;
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
unsigned short power_history[POWER_HISTORY_LEN];
|
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
char power_message[POWER_MESSAGE_LEN] = "";
|
|
|
|
char charge_restart_level = CHARGE_RESTART_HI;
|
2002-12-03 14:18:51 +00:00
|
|
|
|
|
|
|
int powermgmt_last_cycle_startstop_min = 20; /* how many minutes ago was the charging started or stopped? */
|
2002-12-14 12:10:29 +00:00
|
|
|
int powermgmt_last_cycle_level = 0; /* which level had the batteries at this time? */
|
|
|
|
int trickle_sec = 0; /* how many seconds should the charger be enabled per minute for trickle charging? */
|
2002-12-14 15:48:08 +00:00
|
|
|
int charge_state = 0; /* at the beginning, the charger does nothing */
|
2002-08-06 10:50:50 +00:00
|
|
|
#endif
|
|
|
|
|
2002-12-03 14:18:51 +00:00
|
|
|
|
|
|
|
int battery_time(void)
|
|
|
|
{
|
|
|
|
return powermgmt_est_runningtime_min;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look into the percent_to_volt_* table and get a realistic battery level percentage */
|
|
|
|
int voltage_to_percent(int voltage, int* 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 /* 10th */
|
|
|
|
+ (voltage - table[i]) * 10 / (table[i+1] - table[i]); /* 1th */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-08-07 11:30:39 +00:00
|
|
|
/* Returns battery level in percent */
|
|
|
|
int battery_level(void)
|
|
|
|
{
|
|
|
|
int level = 0;
|
|
|
|
int c = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* calculate average over last 3 minutes (skip empty samples) */
|
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
if (power_history[POWER_HISTORY_LEN-1-i]) {
|
|
|
|
level += power_history[POWER_HISTORY_LEN-1-i];
|
|
|
|
c++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c)
|
|
|
|
level = level / c; /* avg */
|
|
|
|
else /* history was empty, get a fresh sample */
|
|
|
|
level = (adc_read(ADC_UNREG_POWER) * BATTERY_SCALE_FACTOR) / 10000;
|
|
|
|
|
|
|
|
if(level > BATTERY_LEVEL_FULL)
|
|
|
|
level = BATTERY_LEVEL_FULL;
|
|
|
|
|
|
|
|
if(level < BATTERY_LEVEL_EMPTY)
|
|
|
|
level = BATTERY_LEVEL_EMPTY;
|
|
|
|
|
2002-12-03 14:18:51 +00:00
|
|
|
/* level now stores the voltage in centivolts */
|
|
|
|
/* let's calculate a percentage now with using the voltage arrays */
|
|
|
|
|
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
if (powermgmt_last_cycle_startstop_min < 20) {
|
|
|
|
/* the batteries are lazy, so take a value between the result of the two table lookups */
|
|
|
|
if (charger_enabled)
|
|
|
|
level = (voltage_to_percent(level, percent_to_volt_charge)
|
|
|
|
* battery_lazyness[powermgmt_last_cycle_startstop_min]
|
|
|
|
+ voltage_to_percent(level, percent_to_volt_nocharge)
|
|
|
|
* (100 - battery_lazyness[powermgmt_last_cycle_startstop_min])) / 100;
|
|
|
|
else
|
|
|
|
level = (voltage_to_percent(level, percent_to_volt_nocharge)
|
|
|
|
* battery_lazyness[powermgmt_last_cycle_startstop_min]
|
|
|
|
+ voltage_to_percent(level, percent_to_volt_charge)
|
|
|
|
* (100 - battery_lazyness[powermgmt_last_cycle_startstop_min])) / 100;
|
|
|
|
} else {
|
|
|
|
if (charger_enabled)
|
|
|
|
level = voltage_to_percent(level, percent_to_volt_charge);
|
|
|
|
else
|
|
|
|
level = voltage_to_percent(level, percent_to_volt_nocharge);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
level = voltage_to_percent(level, percent_to_volt_nocharge); /* always use the nocharge table */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return level;
|
2002-08-07 11:30:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Tells if the battery level is safe for disk writes */
|
|
|
|
bool battery_level_safe(void)
|
|
|
|
{
|
|
|
|
/* I'm pretty sure we don't want an average over a long time here */
|
2002-08-12 12:19:25 +00:00
|
|
|
if (power_history[POWER_HISTORY_LEN-1])
|
|
|
|
return power_history[POWER_HISTORY_LEN-1] > BATTERY_LEVEL_DANGEROUS;
|
|
|
|
else
|
|
|
|
return adc_read(ADC_UNREG_POWER) > (BATTERY_LEVEL_DANGEROUS * 10000) / BATTERY_SCALE_FACTOR;
|
2002-08-07 11:30:39 +00:00
|
|
|
}
|
|
|
|
|
2002-09-23 11:42:48 +00:00
|
|
|
void set_poweroff_timeout(int timeout)
|
|
|
|
{
|
|
|
|
poweroff_timeout = timeout;
|
|
|
|
}
|
|
|
|
|
2002-12-03 22:41:50 +00:00
|
|
|
void set_sleep_timer(int seconds)
|
|
|
|
{
|
|
|
|
if(seconds)
|
|
|
|
{
|
|
|
|
sleeptimer_active = true;
|
|
|
|
sleeptimer_endtick = current_tick + seconds * HZ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sleeptimer_active = false;
|
|
|
|
sleeptimer_endtick = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int get_sleep_timer(void)
|
|
|
|
{
|
|
|
|
if(sleeptimer_active)
|
|
|
|
return (sleeptimer_endtick - current_tick) / HZ;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-09-24 07:50:17 +00:00
|
|
|
/* We shut off in the following cases:
|
2002-09-26 22:34:35 +00:00
|
|
|
1) The unit is idle, not playing music
|
2002-09-24 07:50:17 +00:00
|
|
|
2) The unit is playing music, but is paused
|
|
|
|
|
2002-09-26 22:34:35 +00:00
|
|
|
We do not shut off in the following cases:
|
|
|
|
1) The USB is connected
|
|
|
|
2) The charger is connected
|
|
|
|
3) We are recording, or recording with pause
|
2002-09-24 07:50:17 +00:00
|
|
|
*/
|
2002-09-23 11:42:48 +00:00
|
|
|
static void handle_auto_poweroff(void)
|
|
|
|
{
|
|
|
|
long timeout = poweroff_idle_timeout_value[poweroff_timeout]*60*HZ;
|
2002-09-24 07:50:17 +00:00
|
|
|
int mpeg_stat = mpeg_status();
|
2002-09-26 22:34:35 +00:00
|
|
|
bool charger_is_inserted = charger_inserted();
|
|
|
|
static bool charger_was_inserted = false;
|
2002-09-24 07:50:17 +00:00
|
|
|
|
2002-09-26 22:34:35 +00:00
|
|
|
/* The was_inserted thing prevents the unit to shut down immediately
|
|
|
|
when the charger is extracted */
|
|
|
|
if(charger_is_inserted || charger_was_inserted)
|
|
|
|
{
|
2002-09-24 07:50:17 +00:00
|
|
|
last_charge_time = current_tick;
|
2002-09-26 22:34:35 +00:00
|
|
|
}
|
|
|
|
charger_was_inserted = charger_is_inserted;
|
2002-09-23 11:42:48 +00:00
|
|
|
|
2002-09-24 07:50:17 +00:00
|
|
|
if(timeout &&
|
2002-09-25 19:25:10 +00:00
|
|
|
!usb_inserted() &&
|
2002-09-24 07:50:17 +00:00
|
|
|
(mpeg_stat == 0 ||
|
|
|
|
mpeg_stat == (MPEG_STATUS_PLAY | MPEG_STATUS_PAUSE)))
|
2002-09-23 11:42:48 +00:00
|
|
|
{
|
|
|
|
if(TIME_AFTER(current_tick, last_keypress + timeout) &&
|
2002-09-24 07:50:17 +00:00
|
|
|
TIME_AFTER(current_tick, last_disk_activity + timeout) &&
|
2002-09-26 22:34:35 +00:00
|
|
|
TIME_AFTER(current_tick, last_charge_time + timeout))
|
2002-09-23 11:42:48 +00:00
|
|
|
power_off();
|
|
|
|
}
|
2002-12-03 22:41:50 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Handle sleeptimer */
|
|
|
|
if(sleeptimer_endtick &&
|
|
|
|
!usb_inserted())
|
|
|
|
{
|
|
|
|
if(TIME_AFTER(current_tick, sleeptimer_endtick))
|
|
|
|
{
|
2002-12-04 12:53:10 +00:00
|
|
|
if(charger_is_inserted)
|
|
|
|
{
|
|
|
|
DEBUGF("Sleep timer timeout. Rebooting...\n");
|
|
|
|
system_reboot();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DEBUGF("Sleep timer timeout. Shutting off...\n");
|
|
|
|
power_off();
|
|
|
|
}
|
2002-12-03 22:41:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-09-23 11:42:48 +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
|
|
|
*/
|
|
|
|
|
2002-12-14 12:10:29 +00:00
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
static void power_thread(void)
|
|
|
|
{
|
|
|
|
int i;
|
2002-08-12 12:19:25 +00:00
|
|
|
int avg, ok_samples, spin_samples;
|
2002-12-14 12:10:29 +00:00
|
|
|
int current = 0;
|
2002-08-06 10:50:50 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
int delta;
|
|
|
|
int charged_time = 0;
|
2002-12-03 14:18:51 +00:00
|
|
|
int charge_max_time_now = 0;
|
2002-12-14 12:10:29 +00:00
|
|
|
int charge_pause = 0; /* no charging pause at the beginning */
|
|
|
|
int trickle_time = 0; /* how many minutes trickle charging already? */
|
2002-08-06 10:50:50 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
2002-08-12 12:19:25 +00:00
|
|
|
/* Make POWER_AVG measurements and calculate an average of that to
|
|
|
|
* reduce the effect of backlights/disk spinning/other variation.
|
2002-08-06 10:50:50 +00:00
|
|
|
*/
|
2002-08-12 12:19:25 +00:00
|
|
|
ok_samples = spin_samples = avg = 0;
|
|
|
|
for (i = 0; i < POWER_AVG_N; i++) {
|
|
|
|
if (ata_disk_is_active()) {
|
|
|
|
if (!ok_samples) {
|
|
|
|
/* if we don't have any good non-disk-spinning samples,
|
|
|
|
* we take a sample anyway in case the disk is going
|
|
|
|
* to spin all the time.
|
|
|
|
*/
|
|
|
|
avg += adc_read(ADC_UNREG_POWER);
|
|
|
|
spin_samples++;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (spin_samples) /* throw away disk-spinning samples */
|
|
|
|
spin_samples = avg = 0;
|
|
|
|
avg += adc_read(ADC_UNREG_POWER);
|
|
|
|
ok_samples++;
|
|
|
|
}
|
|
|
|
sleep(HZ*POWER_AVG_SLEEP);
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
2002-08-12 12:19:25 +00:00
|
|
|
avg = avg / ((ok_samples) ? ok_samples : spin_samples);
|
2002-08-06 10:50:50 +00:00
|
|
|
|
|
|
|
/* rotate the power history */
|
|
|
|
for (i = 0; i < POWER_HISTORY_LEN-1; i++)
|
|
|
|
power_history[i] = power_history[i+1];
|
|
|
|
|
2002-08-12 12:19:25 +00:00
|
|
|
/* insert new value in the end, in centivolts 8-) */
|
2002-08-06 10:50:50 +00:00
|
|
|
power_history[POWER_HISTORY_LEN-1] = (avg * BATTERY_SCALE_FACTOR) / 10000;
|
|
|
|
|
2002-12-03 14:18:51 +00:00
|
|
|
/* calculate estimated remaining running time */
|
|
|
|
/* not charging: remaining running time */
|
|
|
|
/* charging: remaining charging time */
|
2002-12-14 12:10:29 +00:00
|
|
|
|
2002-12-03 14:18:51 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
if (charger_enabled)
|
|
|
|
/* if taking the nocharge battery level, charging lasts 30% longer than the value says */
|
|
|
|
/* so consider it because there's the battery lazyness inside the the battery_level */
|
|
|
|
if (powermgmt_last_cycle_startstop_min < 20) {
|
|
|
|
i = (100 - battery_lazyness[powermgmt_last_cycle_startstop_min]) * 30 / 100 ; /* 0..30 */
|
|
|
|
powermgmt_est_runningtime_min = (100 - battery_level()) * BATTERY_CAPACITY / 100 * (100 + i) / 100 * 60 / CURRENT_CHARGING;
|
|
|
|
} else {
|
|
|
|
powermgmt_est_runningtime_min = (100 - battery_level()) * BATTERY_CAPACITY / 100 * 60 / CURRENT_CHARGING;
|
|
|
|
}
|
2002-12-14 12:10:29 +00:00
|
|
|
else {
|
2002-12-03 14:18:51 +00:00
|
|
|
#endif
|
2002-12-14 12:10:29 +00:00
|
|
|
current = CURRENT_NORMAL;
|
|
|
|
if (global_settings.backlight_timeout == 1) /* LED always on */
|
|
|
|
current += CURRENT_BACKLIGHT;
|
|
|
|
powermgmt_est_runningtime_min = battery_level() * BATTERY_CAPACITY / 100 * 60 / current;
|
2002-12-03 14:18:51 +00:00
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
2002-12-14 12:10:29 +00:00
|
|
|
}
|
2002-12-03 14:18:51 +00:00
|
|
|
|
|
|
|
if (charge_pause > 0)
|
|
|
|
charge_pause--;
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
if (charger_inserted()) {
|
|
|
|
if (charger_enabled) {
|
|
|
|
/* charger inserted and enabled */
|
|
|
|
charged_time++;
|
2002-12-03 14:18:51 +00:00
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN, "Chg %dm max %dm", charged_time, charge_max_time_now);
|
|
|
|
|
|
|
|
if (charged_time > charge_max_time_now) {
|
|
|
|
DEBUGF("power: charged_time > charge_max_time_now, enough!\n");
|
2002-08-06 10:50:50 +00:00
|
|
|
/* have charged too long and deltaV detection did not work! */
|
2002-12-03 14:18:51 +00:00
|
|
|
powermgmt_last_cycle_level = battery_level();
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
2002-08-06 10:50:50 +00:00
|
|
|
charger_enable(false);
|
2002-12-03 14:18:51 +00:00
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN, "Chg tmout %d min", charge_max_time_now);
|
|
|
|
/* disable charging for several hours from this point, just to be sure */
|
|
|
|
charge_pause = CHARGE_PAUSE_LEN;
|
2002-12-14 12:10:29 +00:00
|
|
|
/* no trickle charge here, because the charging cycle didn't end the right way */
|
2002-12-14 15:48:08 +00:00
|
|
|
charge_state = 0; /* 0: decharging/charger off, 1: charge, 2: top-off, 3: trickle */
|
2002-08-06 10:50:50 +00:00
|
|
|
} else {
|
|
|
|
if (charged_time > CHARGE_MIN_TIME) {
|
|
|
|
/* have charged continuously over the minimum charging time,
|
|
|
|
* so we monitor for deltaV going negative. Multiply things
|
|
|
|
* by 100 to get more accuracy without floating point arithmetic.
|
2002-08-12 12:19:25 +00:00
|
|
|
* power_history[] contains centivolts so after multiplying by 100
|
|
|
|
* the deltas are in tenths of millivolts (delta of 5 is
|
|
|
|
* 0.0005 V).
|
2002-08-06 10:50:50 +00:00
|
|
|
*/
|
2002-08-12 12:19:25 +00:00
|
|
|
delta = ( power_history[POWER_HISTORY_LEN-1] * 100
|
2002-08-13 16:06:29 +00:00
|
|
|
+ power_history[POWER_HISTORY_LEN-2] * 100
|
|
|
|
- power_history[POWER_HISTORY_LEN-1-CHARGE_END_NEGD+1] * 100
|
2002-08-12 12:19:25 +00:00
|
|
|
- power_history[POWER_HISTORY_LEN-1-CHARGE_END_NEGD] * 100 )
|
2002-08-13 16:06:29 +00:00
|
|
|
/ CHARGE_END_NEGD / 2;
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2002-08-12 12:19:25 +00:00
|
|
|
if (delta < -100) { /* delta < -10 mV */
|
2002-08-06 10:50:50 +00:00
|
|
|
DEBUGF("power: short-term negative delta, enough!\n");
|
2002-12-03 14:18:51 +00:00
|
|
|
powermgmt_last_cycle_level = battery_level();
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
2002-08-06 10:50:50 +00:00
|
|
|
charger_enable(false);
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN, "end negd %d %dmin", delta, charged_time);
|
2002-12-03 14:18:51 +00:00
|
|
|
/* disable charging for several hours from this point, just to be sure */
|
|
|
|
charge_pause = CHARGE_PAUSE_LEN;
|
2002-12-14 12:10:29 +00:00
|
|
|
/* enable trickle charging */
|
2002-12-14 15:48:08 +00:00
|
|
|
if (global_settings.trickle_charge) {
|
|
|
|
trickle_sec = CURRENT_NORMAL * 60 / CURRENT_CHARGING; /* first guess, maybe consider if LED backlight is on, disk is active,... */
|
|
|
|
trickle_time = 0;
|
|
|
|
charge_state = 2; /* 0: decharging/charger off, 1: charge, 2: top-off, 3: trickle */
|
|
|
|
}
|
2002-08-06 10:50:50 +00:00
|
|
|
} else {
|
|
|
|
/* if we didn't disable the charger in the previous test, check for low positive delta */
|
2002-08-12 12:19:25 +00:00
|
|
|
delta = ( power_history[POWER_HISTORY_LEN-1] * 100
|
2002-08-13 16:06:29 +00:00
|
|
|
+ power_history[POWER_HISTORY_LEN-2] * 100
|
|
|
|
- power_history[POWER_HISTORY_LEN-1-CHARGE_END_ZEROD+1] * 100
|
2002-08-12 12:19:25 +00:00
|
|
|
- power_history[POWER_HISTORY_LEN-1-CHARGE_END_ZEROD] * 100 )
|
2002-08-13 16:06:29 +00:00
|
|
|
/ CHARGE_END_ZEROD / 2;
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2002-08-12 12:19:25 +00:00
|
|
|
if (delta < 1) { /* delta < 0.1 mV */
|
2002-08-06 10:50:50 +00:00
|
|
|
DEBUGF("power: long-term small positive delta, enough!\n");
|
2002-12-03 14:18:51 +00:00
|
|
|
powermgmt_last_cycle_level = battery_level();
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
2002-08-06 10:50:50 +00:00
|
|
|
charger_enable(false);
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN, "end lowd %d %dmin", delta, charged_time);
|
2002-12-03 14:18:51 +00:00
|
|
|
/* disable charging for several hours from this point, just to be sure */
|
|
|
|
charge_pause = CHARGE_PAUSE_LEN;
|
2002-12-14 12:10:29 +00:00
|
|
|
/* enable trickle charging */
|
2002-12-14 15:48:08 +00:00
|
|
|
if (global_settings.trickle_charge) {
|
|
|
|
trickle_sec = CURRENT_NORMAL * 60 / CURRENT_CHARGING; /* first guess, maybe consider if LED backlight is on, disk is active,... */
|
|
|
|
trickle_time = 0;
|
|
|
|
charge_state = 2; /* 0: decharging/charger off, 1: charge, 2: top-off, 3: trickle */
|
|
|
|
}
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-12-14 12:10:29 +00:00
|
|
|
} else { /* charged inserted but not enabled */
|
|
|
|
|
|
|
|
/* trickle charging */
|
2002-12-14 15:48:08 +00:00
|
|
|
if (charge_state > 1) { /* top off or trickle? */
|
2002-12-14 12:10:29 +00:00
|
|
|
/* adjust trickle charge time */
|
2002-12-14 15:48:08 +00:00
|
|
|
if ( ((charge_state == 2) && (power_history[POWER_HISTORY_LEN-1] > TOPOFF_VOLTAGE))
|
|
|
|
|| ((charge_state == 3) && (power_history[POWER_HISTORY_LEN-1] > TRICKLE_VOLTAGE)) ) { /* charging too much */
|
2002-12-14 12:10:29 +00:00
|
|
|
trickle_sec--;
|
|
|
|
} else { /* charging too less */
|
|
|
|
trickle_sec++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trickle_sec > 24) trickle_sec = 24;
|
|
|
|
if (trickle_sec < 1) trickle_sec = 1;
|
|
|
|
|
|
|
|
/* charge the calculated amount of seconds */
|
|
|
|
charger_enable(true);
|
|
|
|
sleep(HZ * trickle_sec);
|
|
|
|
charger_enable(false);
|
|
|
|
|
|
|
|
/* trickle charging long enough? */
|
|
|
|
|
|
|
|
if (trickle_time++ > TRICKLE_MAX_TIME + TOPOFF_MAX_TIME) {
|
|
|
|
trickle_sec = 0; /* show in debug menu that trickle is off */
|
2002-12-14 15:48:08 +00:00
|
|
|
charge_state = 0; /* 0: decharging/charger off, 1: charge, 2: top-off, 3: trickle */
|
2002-12-14 12:10:29 +00:00
|
|
|
}
|
2002-12-14 15:48:08 +00:00
|
|
|
|
|
|
|
if ((charge_state == 2) && (trickle_time > TOPOFF_MAX_TIME)) /* change state? */
|
|
|
|
charge_state = 3; /* 0: decharging/charger off, 1: charge, 2: top-off, 3: trickle */
|
2002-12-14 12:10:29 +00:00
|
|
|
}
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
/* if battery is not full, enable charging */
|
|
|
|
if (battery_level() < charge_restart_level) {
|
2002-12-03 14:18:51 +00:00
|
|
|
if (charge_pause) {
|
|
|
|
DEBUGF("power: batt level < restart level, but charge pause, not enabling\n");
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN, "chg pause %d min", charge_pause);
|
|
|
|
} else {
|
|
|
|
/* calculate max charge time depending on current battery level */
|
2002-12-14 21:01:32 +00:00
|
|
|
/* take 35% more because battery level is not linear */
|
|
|
|
i = CHARGE_MAX_TIME_1500 * BATTERY_CAPACITY / 1500;
|
|
|
|
charge_max_time_now = i * (100 + 35 - battery_level()) / 100;
|
|
|
|
if (charge_max_time_now > i) {
|
|
|
|
charge_max_time_now = i;
|
2002-12-03 14:18:51 +00:00
|
|
|
}
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN, "ChgAt %d%% max %dm", battery_level(), charge_max_time_now);
|
|
|
|
|
|
|
|
/* enable the charger after the max time calc is done, because battery_level */
|
|
|
|
/* depends on if the charger is on */
|
2002-12-14 12:10:29 +00:00
|
|
|
DEBUGF("power: charger inserted and battery not full, enabling\n");
|
2002-12-03 14:18:51 +00:00
|
|
|
powermgmt_last_cycle_level = battery_level();
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
2002-12-14 12:10:29 +00:00
|
|
|
charged_time = 0;
|
2002-12-03 14:18:51 +00:00
|
|
|
|
|
|
|
charger_enable(true);
|
2002-12-14 15:48:08 +00:00
|
|
|
charge_state = 1; /* 0: decharging/charger off, 1: charge, 2: top-off, 3: trickle */
|
|
|
|
/* clear the power history so that we don't use values before
|
|
|
|
* discharge for the long-term delta
|
|
|
|
*/
|
|
|
|
for (i = 0; i < POWER_HISTORY_LEN-1; i++)
|
|
|
|
power_history[i] = power_history[POWER_HISTORY_LEN-1];
|
2002-12-03 14:18:51 +00:00
|
|
|
}
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* charger not inserted */
|
|
|
|
if (charger_enabled) {
|
|
|
|
/* charger not inserted but was enabled */
|
|
|
|
DEBUGF("power: charger disconnected, disabling\n");
|
2002-12-03 14:18:51 +00:00
|
|
|
powermgmt_last_cycle_level = battery_level();
|
|
|
|
powermgmt_last_cycle_startstop_min = 0;
|
2002-12-14 12:10:29 +00:00
|
|
|
trickle_sec = 0; /* show in debug menu that trickle is off */
|
2002-08-06 10:50:50 +00:00
|
|
|
charger_enable(false);
|
2002-12-14 15:48:08 +00:00
|
|
|
charge_state = 0; /* 0: decharging/charger off, 1: charge, 2: top-off, 3: trickle */
|
2002-08-06 10:50:50 +00:00
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN, "Charger disc");
|
|
|
|
}
|
|
|
|
/* charger not inserted and disabled, so we're discharging */
|
|
|
|
}
|
2002-12-03 14:18:51 +00:00
|
|
|
powermgmt_last_cycle_startstop_min++;
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
#endif /* HAVE_CHARGE_CTRL*/
|
|
|
|
|
|
|
|
/* sleep for roughly a minute */
|
2002-12-14 12:10:29 +00:00
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
i = 60 - trickle_sec - POWER_AVG_N * POWER_AVG_SLEEP;
|
|
|
|
#else
|
|
|
|
i = 60 - POWER_AVG_N * POWER_AVG_SLEEP;
|
|
|
|
#endif
|
|
|
|
if (i > 0)
|
|
|
|
sleep(HZ*(i));
|
2002-09-23 11:42:48 +00:00
|
|
|
|
|
|
|
handle_auto_poweroff();
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void power_init(void)
|
|
|
|
{
|
|
|
|
/* init history to 0 */
|
|
|
|
memset(power_history, 0x00, sizeof(power_history));
|
2002-08-15 11:32:01 +00:00
|
|
|
/* initialize the history with a single sample to prevent level
|
|
|
|
flickering during the first minute of execution */
|
|
|
|
power_history[POWER_HISTORY_LEN-1] = (adc_read(ADC_UNREG_POWER) * BATTERY_SCALE_FACTOR) / 10000;
|
2002-12-03 14:18:51 +00:00
|
|
|
/* calculate the remaining time to that the info screen displays something useful */
|
|
|
|
powermgmt_est_runningtime_min = battery_level() * BATTERY_CAPACITY / 100 * 60 / CURRENT_NORMAL;
|
2002-08-06 10:50:50 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_CHARGE_CTRL
|
|
|
|
snprintf(power_message, POWER_MESSAGE_LEN, "Powermgmt started");
|
|
|
|
#endif
|
|
|
|
create_thread(power_thread, power_stack, sizeof(power_stack), power_thread_name);
|
|
|
|
}
|
|
|
|
|
2002-08-07 11:30:39 +00:00
|
|
|
#endif /* SIMULATOR */
|
|
|
|
|