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
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-06 10:50:50 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
#include "config.h"
|
2008-12-24 16:58:41 +00:00
|
|
|
#include "system.h"
|
2002-08-06 10:50:50 +00:00
|
|
|
#include "kernel.h"
|
|
|
|
#include "thread.h"
|
|
|
|
#include "debug.h"
|
iBasso DX50/DX90: Major code cleanup and reorganization.
Reorganization
- Separated iBasso devices from PLATFORM_ANDROID. These are now standlone
hosted targets. Most device specific code is in the
firmware/target/hosted/ibasso directory.
- No dependency on Android SDK, only the Android NDK is needed.
32 bit Android NDK and Android API Level 16.
- Separate implementation for each device where feasible.
Code cleanup
- Rewrite of existing code, from simple reformat to complete reimplementation.
- New backlight interface, seperating backlight from touchscreen.
- Rewrite of device button handler, removing unneeded code and fixing memory
leaks.
- New Debug messages interface logging to Android adb logcat (DEBUGF, panicf,
logf).
- Rewrite of lcd device handler, removing unneeded code and fixing memory leaks.
- Rewrite of audiohw device handler/pcm interface, removing unneeded code and
fixing memory leaks, enabling 44.1/48kHz pthreaded playback.
- Rewrite of power and powermng, proper shutdown, using batterylog results
(see http://gerrit.rockbox.org/r/#/c/1047/).
- Rewrite of configure (Android NDK) and device specific config.
- Rewrite of the Android NDK specific Makefile.
Misc
- All plugins/games/demos activated.
- Update tinyalsa to latest from https://github.com/tinyalsa/tinyalsa.
Includes
- http://gerrit.rockbox.org/r/#/c/993/
- http://gerrit.rockbox.org/r/#/c/1010/
- http://gerrit.rockbox.org/r/#/c/1035/
Does not include http://gerrit.rockbox.org/r/#/c/1007/ due to new backlight
interface and new option for hold switch, touchscreen, physical button
interaction.
Rockbox needs the iBasso DX50/DX90 loader for startup, see
http://gerrit.rockbox.org/r/#/c/1099/
The loader expects Rockbox to be installed in /mnt/sdcard/.rockbox/. If
/mnt/sdcard/ is accessed as USB mass storage device, Rockbox will exit
gracefully and the loader will restart Rockbox on USB disconnect.
Tested on iBasso DX50.
Compiled (not tested) for iBasso DX90.
Compiled (not tested) for PLATFORM_ANDROID.
Change-Id: I5f5e22e68f5b4cf29c28e2b40b2c265f2beb7ab7
2015-02-02 20:44:29 +00:00
|
|
|
#if !defined(DX50) && !defined(DX90)
|
2002-08-06 10:50:50 +00:00
|
|
|
#include "adc.h"
|
iBasso DX50/DX90: Major code cleanup and reorganization.
Reorganization
- Separated iBasso devices from PLATFORM_ANDROID. These are now standlone
hosted targets. Most device specific code is in the
firmware/target/hosted/ibasso directory.
- No dependency on Android SDK, only the Android NDK is needed.
32 bit Android NDK and Android API Level 16.
- Separate implementation for each device where feasible.
Code cleanup
- Rewrite of existing code, from simple reformat to complete reimplementation.
- New backlight interface, seperating backlight from touchscreen.
- Rewrite of device button handler, removing unneeded code and fixing memory
leaks.
- New Debug messages interface logging to Android adb logcat (DEBUGF, panicf,
logf).
- Rewrite of lcd device handler, removing unneeded code and fixing memory leaks.
- Rewrite of audiohw device handler/pcm interface, removing unneeded code and
fixing memory leaks, enabling 44.1/48kHz pthreaded playback.
- Rewrite of power and powermng, proper shutdown, using batterylog results
(see http://gerrit.rockbox.org/r/#/c/1047/).
- Rewrite of configure (Android NDK) and device specific config.
- Rewrite of the Android NDK specific Makefile.
Misc
- All plugins/games/demos activated.
- Update tinyalsa to latest from https://github.com/tinyalsa/tinyalsa.
Includes
- http://gerrit.rockbox.org/r/#/c/993/
- http://gerrit.rockbox.org/r/#/c/1010/
- http://gerrit.rockbox.org/r/#/c/1035/
Does not include http://gerrit.rockbox.org/r/#/c/1007/ due to new backlight
interface and new option for hold switch, touchscreen, physical button
interaction.
Rockbox needs the iBasso DX50/DX90 loader for startup, see
http://gerrit.rockbox.org/r/#/c/1099/
The loader expects Rockbox to be installed in /mnt/sdcard/.rockbox/. If
/mnt/sdcard/ is accessed as USB mass storage device, Rockbox will exit
gracefully and the loader will restart Rockbox on USB disconnect.
Tested on iBasso DX50.
Compiled (not tested) for iBasso DX90.
Compiled (not tested) for PLATFORM_ANDROID.
Change-Id: I5f5e22e68f5b4cf29c28e2b40b2c265f2beb7ab7
2015-02-02 20:44:29 +00:00
|
|
|
#endif
|
2002-08-06 10:50:50 +00:00
|
|
|
#include "string.h"
|
2008-11-01 16:14:28 +00:00
|
|
|
#include "storage.h"
|
2002-08-06 10:50:50 +00:00
|
|
|
#include "power.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
|
2005-07-05 00:03:18 +00:00
|
|
|
#include "logf.h"
|
2012-01-07 19:49:36 +00:00
|
|
|
#ifdef HAVE_REMOTE_LCD
|
2005-11-19 01:30:42 +00:00
|
|
|
#include "lcd-remote.h"
|
2012-01-07 19:49:36 +00:00
|
|
|
#endif
|
2010-06-21 16:53:00 +00:00
|
|
|
#if (CONFIG_PLATFORM & PLATFORM_HOSTED)
|
2006-03-25 14:31:44 +00:00
|
|
|
#include <time.h>
|
|
|
|
#endif
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2009-07-26 19:41:24 +00:00
|
|
|
#if (defined(IAUDIO_X5) || defined(IAUDIO_M5) || defined(COWON_D2)) \
|
|
|
|
&& !defined (SIMULATOR)
|
|
|
|
#include "pcf50606.h"
|
|
|
|
#endif
|
2006-09-12 14:20:29 +00:00
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
/** Shared by sim **/
|
2015-01-11 17:02:43 +00:00
|
|
|
static int last_sent_battery_level = 100;
|
2008-12-24 16:58:41 +00:00
|
|
|
/* battery level (0-100%) */
|
|
|
|
int battery_percent = -1;
|
|
|
|
void send_battery_level_event(void);
|
2012-10-05 19:30:13 +00:00
|
|
|
static void set_sleep_timer(int seconds);
|
2007-11-03 05:00:49 +00:00
|
|
|
|
2011-02-28 23:14:18 +00:00
|
|
|
static bool sleeptimer_active = false;
|
|
|
|
static long sleeptimer_endtick;
|
2011-12-26 09:30:25 +00:00
|
|
|
/* Whether an active sleep timer should be restarted when a key is pressed */
|
|
|
|
static bool sleeptimer_key_restarts = false;
|
|
|
|
/* The number of seconds the sleep timer was last set to */
|
|
|
|
static unsigned int sleeptimer_duration = 0;
|
2011-02-28 23:14:18 +00:00
|
|
|
|
2007-08-12 19:49:03 +00:00
|
|
|
#if CONFIG_CHARGING
|
2008-12-24 16:58:41 +00:00
|
|
|
/* State of the charger input as seen by the power thread */
|
|
|
|
enum charger_input_state_type charger_input_state;
|
|
|
|
/* Power inputs as seen by the power thread */
|
|
|
|
unsigned int power_thread_inputs;
|
|
|
|
#if CONFIG_CHARGING >= CHARGING_MONITOR
|
|
|
|
/* Charging state (mode) as seen by the power thread */
|
|
|
|
enum charge_state_type charge_state = DISCHARGING;
|
2008-04-20 19:00:52 +00:00
|
|
|
#endif
|
2008-12-24 16:58:41 +00:00
|
|
|
#endif /* CONFIG_CHARGING */
|
2008-04-20 19:00:52 +00:00
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
static int shutdown_timeout = 0;
|
2011-03-01 20:31:20 +00:00
|
|
|
|
2011-03-08 19:33:30 +00:00
|
|
|
void handle_auto_poweroff(void);
|
2011-03-08 19:33:25 +00:00
|
|
|
static int poweroff_timeout = 0;
|
2011-03-08 19:33:30 +00:00
|
|
|
static long last_event_tick = 0;
|
2011-03-08 19:33:25 +00:00
|
|
|
|
2012-01-03 23:44:38 +00:00
|
|
|
#if (CONFIG_BATTERY_MEASURE & PERCENTAGE_MEASURE) == PERCENTAGE_MEASURE
|
|
|
|
int _battery_voltage(void) { return -1; }
|
|
|
|
|
|
|
|
const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11];
|
|
|
|
const unsigned short percent_to_volt_charge[11];
|
|
|
|
|
|
|
|
#elif (CONFIG_BATTERY_MEASURE & VOLTAGE_MEASURE) == VOLTAGE_MEASURE
|
|
|
|
int _battery_level(void) { return -1; }
|
2005-03-03 07:25:43 +00:00
|
|
|
/*
|
|
|
|
* Average battery voltage and charger voltage, filtered via a digital
|
2008-05-19 03:59:52 +00:00
|
|
|
* exponential filter (aka. exponential moving average, scaled):
|
|
|
|
* avgbat = y[n] = (N-1)/N*y[n-1] + x[n]. battery_millivolts = y[n] / N.
|
2005-03-03 07:25:43 +00:00
|
|
|
*/
|
2008-12-24 16:58:41 +00:00
|
|
|
static unsigned int avgbat;
|
|
|
|
/* filtered battery voltage, millivolts */
|
|
|
|
static unsigned int battery_millivolts;
|
2012-01-03 23:44:38 +00:00
|
|
|
#elif (CONFIG_BATTERY_MEASURE == 0)
|
|
|
|
int _battery_voltage(void) { return -1; }
|
|
|
|
int _battery_level(void) { return -1; }
|
|
|
|
|
|
|
|
const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11];
|
|
|
|
const unsigned short percent_to_volt_charge[11];
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !(CONFIG_BATTERY_MEASURE & TIME_MEASURE)
|
|
|
|
static int powermgmt_est_runningtime_min;
|
|
|
|
int _battery_time(void) { return powermgmt_est_runningtime_min; }
|
|
|
|
#endif
|
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
/* default value, mAh */
|
|
|
|
static int battery_capacity = BATTERY_CAPACITY_DEFAULT;
|
|
|
|
|
2007-11-08 15:37:39 +00:00
|
|
|
#if BATTERY_TYPES_COUNT > 1
|
2008-12-24 16:58:41 +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 */
|
2010-03-09 03:23:05 +00:00
|
|
|
unsigned short power_history[POWER_HISTORY_LEN] = {0};
|
2005-03-03 07:25:43 +00:00
|
|
|
|
2018-06-28 10:24:26 +00:00
|
|
|
#if (CONFIG_CPU == JZ4732) || (CONFIG_CPU == JZ4760B) || (CONFIG_PLATFORM & PLATFORM_HOSTED)
|
2009-05-07 14:05:42 +00:00
|
|
|
static char power_stack[DEFAULT_STACK_SIZE + POWERMGMT_DEBUG_STACK];
|
|
|
|
#else
|
2008-12-24 16:58:41 +00:00
|
|
|
static char power_stack[DEFAULT_STACK_SIZE/2 + POWERMGMT_DEBUG_STACK];
|
2009-05-07 14:05:42 +00:00
|
|
|
#endif
|
2004-08-03 19:22:56 +00:00
|
|
|
static const char power_thread_name[] = "power";
|
2002-08-06 10:50:50 +00:00
|
|
|
|
2002-09-23 11:42:48 +00:00
|
|
|
|
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);
|
2010-03-16 17:13:25 +00:00
|
|
|
|
|
|
|
#ifdef CURRENT_NORMAL /*only used if we have run current*/
|
2005-04-10 15:10:44 +00:00
|
|
|
static int runcurrent(void);
|
2010-03-16 17:13:25 +00:00
|
|
|
#endif
|
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
|
|
|
{
|
2012-01-03 23:44:38 +00:00
|
|
|
int millivolts = _battery_voltage();
|
|
|
|
int percent;
|
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
|
|
|
|
2012-01-03 23:44:38 +00:00
|
|
|
if (level) {
|
|
|
|
percent = voltage_to_battery_level(millivolts);
|
|
|
|
if (percent < 0)
|
|
|
|
percent = _battery_level();
|
|
|
|
*level = percent;
|
|
|
|
}
|
2006-10-30 11:33:38 +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) {
|
2008-12-24 16:58:41 +00:00
|
|
|
if ((unsigned)type >= BATTERY_TYPES_COUNT)
|
|
|
|
type = 0;
|
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
battery_type = type;
|
2008-12-24 16:58:41 +00:00
|
|
|
battery_status_update(); /* recalculate the battery status */
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-01-03 23:44:38 +00:00
|
|
|
#ifdef BATTERY_CAPACITY_MIN
|
2005-03-03 07:25:43 +00:00
|
|
|
void set_battery_capacity(int capacity)
|
|
|
|
{
|
2008-12-24 16:58:41 +00:00
|
|
|
if (capacity > BATTERY_CAPACITY_MAX)
|
|
|
|
capacity = BATTERY_CAPACITY_MAX;
|
|
|
|
if (capacity < BATTERY_CAPACITY_MIN)
|
|
|
|
capacity = BATTERY_CAPACITY_MIN;
|
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
battery_capacity = capacity;
|
2008-12-24 16:58:41 +00:00
|
|
|
|
|
|
|
battery_status_update(); /* recalculate the battery status */
|
|
|
|
}
|
2012-01-03 23:44:38 +00:00
|
|
|
#endif
|
2008-12-24 16:58:41 +00:00
|
|
|
|
|
|
|
int get_battery_capacity(void)
|
|
|
|
{
|
|
|
|
return battery_capacity;
|
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)
|
|
|
|
{
|
2012-01-03 23:44:38 +00:00
|
|
|
#if ((CONFIG_BATTERY_MEASURE & TIME_MEASURE) == 0)
|
|
|
|
|
|
|
|
#ifndef CURRENT_NORMAL /* no estimation without current */
|
|
|
|
return -1;
|
|
|
|
#endif
|
|
|
|
if (battery_capacity <= 0) /* nor without capacity */
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
return _battery_time();
|
2002-12-03 14:18:51 +00:00
|
|
|
}
|
|
|
|
|
2002-12-18 18:55:50 +00:00
|
|
|
/* Returns battery level in percent */
|
|
|
|
int battery_level(void)
|
|
|
|
{
|
2008-12-21 18:10:36 +00:00
|
|
|
#ifdef HAVE_BATTERY_SWITCH
|
|
|
|
if ((power_input_status() & POWER_INPUT_BATTERY) == 0)
|
|
|
|
return -1;
|
|
|
|
#endif
|
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)
|
|
|
|
{
|
2008-12-24 16:58:41 +00:00
|
|
|
#if defined(NO_LOW_BATTERY_SHUTDOWN)
|
|
|
|
return true;
|
2012-01-03 23:44:38 +00:00
|
|
|
#elif (CONFIG_BATTERY_MEASURE & PERCENTAGE_MEASURE)
|
|
|
|
return (battery_percent > 0);
|
2008-12-24 16:58:41 +00:00
|
|
|
#elif defined(HAVE_BATTERY_SWITCH)
|
|
|
|
/* Cannot rely upon the battery reading to be valid and the
|
|
|
|
* device could be powered externally. */
|
|
|
|
return input_millivolts() > battery_level_dangerous[battery_type];
|
|
|
|
#else
|
2007-08-15 23:57:27 +00:00
|
|
|
return battery_millivolts > battery_level_dangerous[battery_type];
|
2008-12-21 18:10:36 +00:00
|
|
|
#endif
|
2008-12-24 16:58:41 +00:00
|
|
|
}
|
2002-08-07 11:30:39 +00:00
|
|
|
|
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)
|
|
|
|
{
|
2008-12-24 16:58:41 +00:00
|
|
|
if (voltage <= table[0]) {
|
2005-03-23 20:53:37 +00:00
|
|
|
return 0;
|
2008-12-24 16:58:41 +00:00
|
|
|
}
|
|
|
|
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 */
|
|
|
|
/* Tens digit, 10% per entry, ones digit: interpolated */
|
|
|
|
return i*10 + (voltage - table[i])*10 / (table[i+1] - table[i]);
|
|
|
|
}
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
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;
|
|
|
|
|
2012-01-03 23:44:38 +00:00
|
|
|
if (battery_millivolts < 0)
|
|
|
|
return -1;
|
|
|
|
|
2008-12-21 18:10:36 +00:00
|
|
|
#if CONFIG_CHARGING >= CHARGING_MONITOR
|
2008-12-24 16:58:41 +00:00
|
|
|
if (charging_state()) {
|
2005-04-10 15:10:44 +00:00
|
|
|
/* battery level is defined to be < 100% until charging is finished */
|
2008-12-24 16:58:41 +00:00
|
|
|
level = voltage_to_percent(battery_millivolts,
|
|
|
|
percent_to_volt_charge);
|
|
|
|
if (level > 99)
|
|
|
|
level = 99;
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
2008-12-24 16:58:41 +00:00
|
|
|
else
|
|
|
|
#endif /* CONFIG_CHARGING >= CHARGING_MONITOR */
|
|
|
|
{
|
|
|
|
/* DISCHARGING or error state */
|
|
|
|
level = voltage_to_percent(battery_millivolts,
|
|
|
|
percent_to_volt_discharge[battery_type]);
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
|
|
|
|
2006-10-30 11:33:38 +00:00
|
|
|
return level;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void battery_status_update(void)
|
|
|
|
{
|
2012-01-21 17:10:31 +00:00
|
|
|
int millivolt = battery_voltage();
|
|
|
|
int level = _battery_level();
|
|
|
|
|
|
|
|
if (level < 0)
|
|
|
|
level = voltage_to_battery_level(millivolt);
|
2006-10-30 11:33:38 +00:00
|
|
|
|
2010-03-16 17:13:25 +00:00
|
|
|
#ifdef CURRENT_NORMAL /*don't try to estimate run or charge
|
|
|
|
time without normal current defined*/
|
2005-04-10 15:10:44 +00:00
|
|
|
/* calculate estimated remaining running time */
|
2006-06-06 22:23:52 +00:00
|
|
|
#if CONFIG_CHARGING >= CHARGING_MONITOR
|
2008-12-24 16:58:41 +00:00
|
|
|
if (charging_state()) {
|
|
|
|
/* charging: remaining charging time */
|
|
|
|
powermgmt_est_runningtime_min = (100 - level)*battery_capacity*60
|
|
|
|
/ 100 / (CURRENT_MAX_CHG - runcurrent());
|
2005-04-10 15:10:44 +00:00
|
|
|
}
|
|
|
|
else
|
2006-11-11 01:18:57 +00:00
|
|
|
#endif
|
2010-03-16 17:13:25 +00:00
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
/* discharging: remaining running time */
|
2012-01-03 23:44:38 +00:00
|
|
|
if (level > 0 && (millivolt > percent_to_volt_discharge[battery_type][0]
|
|
|
|
|| millivolt < 0)) {
|
2010-01-02 20:24:25 +00:00
|
|
|
/* linear extrapolation */
|
2008-12-24 16:58:41 +00:00
|
|
|
powermgmt_est_runningtime_min = (level + battery_percent)*60
|
|
|
|
* battery_capacity / 200 / runcurrent();
|
|
|
|
}
|
2010-01-02 20:24:25 +00:00
|
|
|
if (0 > powermgmt_est_runningtime_min) {
|
2008-12-24 16:58:41 +00:00
|
|
|
powermgmt_est_runningtime_min = 0;
|
|
|
|
}
|
2010-03-16 17:13:25 +00:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2010-03-16 17:13:25 +00:00
|
|
|
#ifdef CURRENT_NORMAL /*check that we have a current defined in a config file*/
|
|
|
|
|
2005-03-03 07:25:43 +00:00
|
|
|
/*
|
|
|
|
* Estimate how much current we are drawing just to run.
|
|
|
|
*/
|
|
|
|
static int runcurrent(void)
|
|
|
|
{
|
2010-03-16 17:13:25 +00:00
|
|
|
int current = CURRENT_NORMAL;
|
2005-04-10 15:10:44 +00:00
|
|
|
|
2008-12-24 17:28:01 +00:00
|
|
|
#ifndef BOOTLOADER
|
2008-12-24 16:58:41 +00:00
|
|
|
if (usb_inserted()
|
|
|
|
#ifdef HAVE_USB_POWER
|
|
|
|
#if (CURRENT_USB < CURRENT_NORMAL)
|
2015-01-02 21:15:31 +00:00
|
|
|
|| usb_powered_only()
|
2008-12-24 16:58:41 +00:00
|
|
|
#else
|
2015-01-02 21:15:31 +00:00
|
|
|
&& !usb_powered_only()
|
2008-12-24 16:58:41 +00:00
|
|
|
#endif
|
2005-06-23 02:18:29 +00:00
|
|
|
#endif
|
2008-12-24 16:58:41 +00:00
|
|
|
) {
|
2005-03-03 07:25:43 +00:00
|
|
|
current = CURRENT_USB;
|
|
|
|
}
|
2005-04-10 15:10:44 +00:00
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
#if defined(HAVE_BACKLIGHT)
|
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
|
2010-07-27 21:27:51 +00:00
|
|
|
|
|
|
|
#if defined(HAVE_ATA_POWER_OFF) && defined(CURRENT_ATA)
|
|
|
|
if (ide_powered())
|
|
|
|
current += CURRENT_ATA;
|
|
|
|
#endif
|
|
|
|
|
2008-12-24 17:28:01 +00:00
|
|
|
#endif /* BOOTLOADER */
|
2010-03-16 17:13:25 +00:00
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
return current;
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
|
|
|
|
2010-03-16 17:13:25 +00:00
|
|
|
#endif /* CURRENT_NORMAL */
|
2005-03-03 07:25:43 +00:00
|
|
|
|
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
|
|
|
{
|
2008-12-24 16:58:41 +00:00
|
|
|
if (rtc_check_alarm_flag())
|
2005-03-03 07:25:43 +00:00
|
|
|
rtc_enable_alarm(false);
|
2005-02-06 17:21:42 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
/* switch off unit if battery level is too low for reliable operation */
|
2008-12-21 18:10:36 +00:00
|
|
|
bool query_force_shutdown(void)
|
|
|
|
{
|
2008-12-24 16:58:41 +00:00
|
|
|
#if defined(NO_LOW_BATTERY_SHUTDOWN)
|
2008-12-21 18:10:36 +00:00
|
|
|
return false;
|
2012-01-03 23:44:38 +00:00
|
|
|
#elif CONFIG_BATTERY_MEASURE & PERCENTAGE_MEASURE
|
|
|
|
return battery_percent == 0;
|
2008-12-24 16:58:41 +00:00
|
|
|
#elif defined(HAVE_BATTERY_SWITCH)
|
|
|
|
/* Cannot rely upon the battery reading to be valid and the
|
|
|
|
* device could be powered externally. */
|
|
|
|
return input_millivolts() < battery_level_shutoff[battery_type];
|
|
|
|
#else
|
|
|
|
return battery_millivolts < battery_level_shutoff[battery_type];
|
2008-12-21 18:10:36 +00:00
|
|
|
#endif
|
|
|
|
}
|
2008-05-19 03:59:52 +00:00
|
|
|
|
2009-01-11 10:07:22 +00:00
|
|
|
#if defined(HAVE_BATTERY_SWITCH) || defined(HAVE_RESET_BATTERY_FILTER)
|
2008-05-19 03:59:52 +00:00
|
|
|
/*
|
2008-12-24 16:58:41 +00:00
|
|
|
* Reset the battery voltage filter to a new value and update the
|
|
|
|
* status.
|
2008-05-19 03:59:52 +00:00
|
|
|
*/
|
2008-12-24 16:58:41 +00:00
|
|
|
void reset_battery_filter(int millivolts)
|
2008-05-19 03:59:52 +00:00
|
|
|
{
|
2008-12-24 16:58:41 +00:00
|
|
|
avgbat = millivolts * BATT_AVE_SAMPLES;
|
|
|
|
battery_millivolts = millivolts;
|
|
|
|
battery_status_update();
|
2008-05-19 03:59:52 +00:00
|
|
|
}
|
2008-12-24 16:58:41 +00:00
|
|
|
#endif /* HAVE_BATTERY_SWITCH */
|
2008-05-19 03:59:52 +00:00
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
/** Generic charging algorithms for common charging types **/
|
2008-12-24 17:28:01 +00:00
|
|
|
#if CONFIG_CHARGING == 0 || CONFIG_CHARGING == CHARGING_SIMPLE
|
|
|
|
static inline void powermgmt_init_target(void)
|
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
}
|
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
static inline void charging_algorithm_step(void)
|
2008-05-19 03:59:52 +00:00
|
|
|
{
|
2008-12-24 16:58:41 +00:00
|
|
|
/* Nothing to do */
|
2008-05-19 03:59:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline void charging_algorithm_close(void)
|
|
|
|
{
|
2008-12-24 16:58:41 +00:00
|
|
|
/* Nothing to do */
|
2008-12-21 18:10:36 +00:00
|
|
|
}
|
2008-12-24 16:58:41 +00:00
|
|
|
#elif CONFIG_CHARGING == CHARGING_MONITOR
|
|
|
|
/*
|
|
|
|
* Monitor CHARGING/DISCHARGING state.
|
|
|
|
*/
|
2008-12-24 17:28:01 +00:00
|
|
|
static inline void powermgmt_init_target(void)
|
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
}
|
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
static inline void charging_algorithm_step(void)
|
2008-05-19 03:59:52 +00:00
|
|
|
{
|
|
|
|
switch (charger_input_state)
|
|
|
|
{
|
2008-12-24 16:58:41 +00:00
|
|
|
case CHARGER_PLUGGED:
|
|
|
|
case CHARGER:
|
|
|
|
if (charging_state()) {
|
|
|
|
charge_state = CHARGING;
|
2008-05-19 03:59:52 +00:00
|
|
|
break;
|
2008-12-24 16:58:41 +00:00
|
|
|
}
|
|
|
|
/* Fallthrough */
|
|
|
|
case CHARGER_UNPLUGGED:
|
|
|
|
case NO_CHARGER:
|
|
|
|
charge_state = DISCHARGING;
|
|
|
|
break;
|
2008-05-19 03:59:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void charging_algorithm_close(void)
|
|
|
|
{
|
|
|
|
/* Nothing to do */
|
|
|
|
}
|
2008-12-24 16:58:41 +00:00
|
|
|
#endif /* CONFIG_CHARGING == * */
|
2008-05-19 03:59:52 +00:00
|
|
|
|
2008-12-03 19:54:25 +00:00
|
|
|
#if CONFIG_CHARGING
|
|
|
|
/* Shortcut function calls - compatibility, simplicity. */
|
|
|
|
|
|
|
|
/* Returns true if any power input is capable of charging. */
|
|
|
|
bool charger_inserted(void)
|
|
|
|
{
|
2008-12-24 16:58:41 +00:00
|
|
|
return power_thread_inputs & POWER_INPUT_CHARGER;
|
2008-12-03 19:54:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns true if any power input is connected - charging-capable
|
|
|
|
* or not. */
|
|
|
|
bool power_input_present(void)
|
|
|
|
{
|
2008-12-24 16:58:41 +00:00
|
|
|
return power_thread_inputs & POWER_INPUT;
|
2008-12-03 19:54:25 +00:00
|
|
|
}
|
|
|
|
|
2003-10-17 14:49:00 +00:00
|
|
|
/*
|
2008-12-24 16:58:41 +00:00
|
|
|
* Detect charger inserted. Return true if the state is transistional.
|
2003-10-17 14:49:00 +00:00
|
|
|
*/
|
2008-12-24 16:58:41 +00:00
|
|
|
static inline bool detect_charger(unsigned int pwr)
|
2003-10-17 14:49:00 +00:00
|
|
|
{
|
2008-12-24 16:58:41 +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 (pwr & POWER_INPUT_CHARGER) {
|
|
|
|
switch (charger_input_state)
|
|
|
|
{
|
|
|
|
case NO_CHARGER:
|
|
|
|
case CHARGER_UNPLUGGED:
|
|
|
|
charger_input_state = CHARGER_PLUGGED;
|
|
|
|
break;
|
2005-03-03 07:25:43 +00:00
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
case CHARGER_PLUGGED:
|
|
|
|
queue_broadcast(SYS_CHARGER_CONNECTED, 0);
|
|
|
|
last_sent_battery_level = 0;
|
|
|
|
charger_input_state = CHARGER;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case CHARGER:
|
|
|
|
/* Steady state */
|
|
|
|
return false;
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
2008-12-24 16:58:41 +00:00
|
|
|
}
|
|
|
|
else { /* charger not inserted */
|
|
|
|
switch (charger_input_state)
|
|
|
|
{
|
|
|
|
case NO_CHARGER:
|
|
|
|
/* Steady state */
|
|
|
|
return false;
|
2005-03-23 20:53:37 +00:00
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
case CHARGER_UNPLUGGED:
|
|
|
|
queue_broadcast(SYS_CHARGER_DISCONNECTED, 0);
|
|
|
|
last_sent_battery_level = 100;
|
|
|
|
charger_input_state = NO_CHARGER;
|
|
|
|
break;
|
2008-12-21 18:10:36 +00:00
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
case CHARGER_PLUGGED:
|
|
|
|
case CHARGER:
|
|
|
|
charger_input_state = CHARGER_UNPLUGGED;
|
|
|
|
break;
|
2008-12-21 18:10:36 +00:00
|
|
|
}
|
2008-12-24 16:58:41 +00:00
|
|
|
}
|
2003-10-17 14:49:00 +00:00
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
/* Transitional state */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif /* CONFIG_CHARGING */
|
|
|
|
|
2012-01-03 23:44:38 +00:00
|
|
|
|
|
|
|
#if CONFIG_BATTERY_MEASURE & VOLTAGE_MEASURE
|
|
|
|
/* Returns filtered battery voltage [millivolts] */
|
|
|
|
int battery_voltage(void)
|
|
|
|
{
|
|
|
|
return battery_millivolts;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void average_init(void)
|
|
|
|
{
|
|
|
|
/* initialize the voltages for the exponential filter */
|
|
|
|
avgbat = _battery_voltage() + 15;
|
|
|
|
|
|
|
|
#ifdef HAVE_DISK_STORAGE /* 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% */
|
|
|
|
#if CONFIG_CHARGING
|
|
|
|
if (!charger_inserted()) /* only if charger not connected */
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
avgbat += (percent_to_volt_discharge[battery_type][6] -
|
|
|
|
percent_to_volt_discharge[battery_type][5]) / 2;
|
|
|
|
}
|
|
|
|
#endif /* HAVE_DISK_STORAGE */
|
|
|
|
|
|
|
|
avgbat = avgbat * BATT_AVE_SAMPLES;
|
|
|
|
battery_millivolts = power_history[0] = avgbat / BATT_AVE_SAMPLES;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void average_step(void)
|
|
|
|
{
|
|
|
|
avgbat += _battery_voltage() - avgbat / BATT_AVE_SAMPLES;
|
|
|
|
/*
|
|
|
|
* battery_millivolts is the millivolt-scaled filtered battery value.
|
|
|
|
*/
|
|
|
|
battery_millivolts = avgbat / BATT_AVE_SAMPLES;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void average_step_low(void)
|
|
|
|
{
|
|
|
|
battery_millivolts = (_battery_voltage() + battery_millivolts + 1) / 2;
|
|
|
|
avgbat += battery_millivolts - avgbat / BATT_AVE_SAMPLES;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void init_battery_percent(void)
|
|
|
|
{
|
|
|
|
#if CONFIG_CHARGING
|
|
|
|
if (charger_inserted()) {
|
|
|
|
battery_percent = voltage_to_percent(battery_millivolts,
|
|
|
|
percent_to_volt_charge);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
battery_percent = voltage_to_percent(battery_millivolts,
|
|
|
|
percent_to_volt_discharge[battery_type]);
|
|
|
|
battery_percent += battery_percent < 100;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int power_hist_item(void)
|
|
|
|
{
|
|
|
|
return battery_millivolts;
|
|
|
|
}
|
|
|
|
#define power_history_unit() battery_millivolts
|
|
|
|
|
|
|
|
#else
|
|
|
|
int battery_voltage(void)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void average_init(void) {}
|
|
|
|
static void average_step(void) {}
|
|
|
|
static void average_step_low(void) {}
|
|
|
|
static void init_battery_percent(void)
|
|
|
|
{
|
|
|
|
battery_percent = _battery_level();
|
|
|
|
}
|
|
|
|
|
|
|
|
static int power_hist_item(void)
|
|
|
|
{
|
|
|
|
return battery_percent;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static void collect_power_history(void)
|
|
|
|
{
|
|
|
|
/* rotate the power history */
|
|
|
|
memmove(&power_history[1], &power_history[0],
|
|
|
|
sizeof(power_history) - sizeof(power_history[0]));
|
|
|
|
power_history[0] = power_hist_item();
|
|
|
|
}
|
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
/*
|
|
|
|
* Monitor the presence of a charger and perform critical frequent steps
|
|
|
|
* such as running the battery voltage filter.
|
|
|
|
*/
|
|
|
|
static inline void power_thread_step(void)
|
|
|
|
{
|
|
|
|
/* 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 -= POWER_THREAD_STEP_TICKS;
|
|
|
|
|
|
|
|
if (shutdown_timeout <= 0)
|
|
|
|
power_off();
|
|
|
|
}
|
2005-09-14 09:08:26 +00:00
|
|
|
|
2007-02-28 21:55:11 +00:00
|
|
|
#ifdef HAVE_RTC_ALARM
|
2008-12-24 16:58:41 +00:00
|
|
|
power_thread_rtc_process();
|
2005-02-06 17:21:42 +00:00
|
|
|
#endif
|
2005-03-03 07:25:43 +00:00
|
|
|
|
2008-12-24 16:58:41 +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) or charging.
|
|
|
|
*/
|
|
|
|
if (!storage_disk_is_active() || usb_inserted()
|
2008-12-21 18:10:36 +00:00
|
|
|
#if CONFIG_CHARGING >= CHARGING_MONITOR
|
2008-12-24 16:58:41 +00:00
|
|
|
|| charger_input_state == CHARGER
|
2008-12-21 18:10:36 +00:00
|
|
|
#endif
|
2008-12-24 16:58:41 +00:00
|
|
|
) {
|
2012-01-03 23:44:38 +00:00
|
|
|
average_step();
|
2008-12-24 16:58:41 +00:00
|
|
|
/* update battery status every time an update is available */
|
|
|
|
battery_status_update();
|
|
|
|
}
|
|
|
|
else if (battery_percent < 8) {
|
2012-01-03 23:44:38 +00:00
|
|
|
average_step_low();
|
|
|
|
/* update battery status every time an update is available */
|
|
|
|
battery_status_update();
|
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
if (!shutdown_timeout && query_force_shutdown()) {
|
|
|
|
sys_poweroff();
|
2005-03-23 20:53:37 +00:00
|
|
|
}
|
2005-03-03 07:25:43 +00:00
|
|
|
}
|
2008-12-24 16:58:41 +00:00
|
|
|
} /* power_thread_step */
|
2003-10-17 14:49:00 +00:00
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
static void power_thread(void)
|
|
|
|
{
|
2008-12-24 16:58:41 +00:00
|
|
|
long next_power_hist;
|
|
|
|
|
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
|
2012-01-03 23:44:38 +00:00
|
|
|
while (_battery_voltage() > 4200) /* gives false readings initially */
|
2014-08-30 11:15:53 +00:00
|
|
|
#elif defined(DX50) || defined(DX90)
|
|
|
|
while (_battery_voltage() < 1) /* can give false readings initially */
|
2008-12-24 16:58:41 +00:00
|
|
|
#endif
|
|
|
|
{
|
|
|
|
sleep(HZ/100);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if CONFIG_CHARGING
|
|
|
|
/* Initialize power input status before calling other routines. */
|
|
|
|
power_thread_inputs = power_input_status();
|
2008-03-11 09:38:12 +00:00
|
|
|
#endif
|
2007-11-30 10:55:09 +00:00
|
|
|
|
2012-01-03 23:44:38 +00:00
|
|
|
/* initialize voltage averaging (if available) */
|
|
|
|
average_init();
|
|
|
|
/* get initial battery level value (in %) */
|
|
|
|
init_battery_percent();
|
|
|
|
/* get some initial data for the power curve */
|
|
|
|
collect_power_history();
|
|
|
|
/* call target specific init now */
|
2008-12-21 18:10:36 +00:00
|
|
|
powermgmt_init_target();
|
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
next_power_hist = current_tick + HZ*60;
|
|
|
|
|
2002-08-06 10:50:50 +00:00
|
|
|
while (1)
|
|
|
|
{
|
2008-12-24 17:42:07 +00:00
|
|
|
#if CONFIG_CHARGING
|
2008-12-24 16:58:41 +00:00
|
|
|
unsigned int pwr = power_input_status();
|
|
|
|
#ifdef HAVE_BATTERY_SWITCH
|
|
|
|
if ((pwr ^ power_thread_inputs) & POWER_INPUT_BATTERY) {
|
|
|
|
sleep(HZ/10);
|
2012-01-03 23:44:38 +00:00
|
|
|
reset_battery_filter(_battery_voltage());
|
2008-12-24 16:58:41 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
power_thread_inputs = pwr;
|
|
|
|
|
|
|
|
if (!detect_charger(pwr))
|
|
|
|
#endif /* CONFIG_CHARGING */
|
|
|
|
{
|
|
|
|
/* Steady state */
|
|
|
|
sleep(POWER_THREAD_STEP_TICKS);
|
|
|
|
|
|
|
|
/* Do common power tasks */
|
|
|
|
power_thread_step();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform target tasks */
|
|
|
|
charging_algorithm_step();
|
|
|
|
|
2012-01-03 23:44:38 +00:00
|
|
|
/* check if some idle or sleep timer wears off */
|
2002-09-23 11:42:48 +00:00
|
|
|
handle_auto_poweroff();
|
2012-01-03 23:44:38 +00:00
|
|
|
|
|
|
|
if (TIME_AFTER(current_tick, next_power_hist)) {
|
|
|
|
/* increment to ensure there is a record for every minute
|
|
|
|
* rather than go forward from the current tick */
|
|
|
|
next_power_hist += HZ*60;
|
|
|
|
collect_power_history();
|
|
|
|
}
|
2002-08-06 10:50:50 +00:00
|
|
|
}
|
2008-12-24 16:58:41 +00:00
|
|
|
} /* power_thread */
|
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
|
|
|
{
|
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
|
|
|
}
|
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
/* Various hardware housekeeping tasks relating to shutting down the player */
|
2006-04-15 13:07:21 +00:00
|
|
|
void shutdown_hw(void)
|
2005-02-06 17:21:42 +00:00
|
|
|
{
|
2008-05-19 03:59:52 +00:00
|
|
|
charging_algorithm_close();
|
2005-04-04 12:06:29 +00:00
|
|
|
audio_stop();
|
2008-12-24 16:58:41 +00:00
|
|
|
|
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
|
2011-10-16 13:18:46 +00:00
|
|
|
font_unload_all();
|
2005-12-06 13:27:15 +00:00
|
|
|
#endif
|
2009-10-09 20:36:09 +00:00
|
|
|
|
|
|
|
/* Commit pending writes if needed. Even though we don't do write caching,
|
|
|
|
things like flash translation layers may need this to commit scattered
|
2018-04-21 07:32:30 +00:00
|
|
|
pages to their final locations. So far only used for iPod Nano 2G. */
|
2009-10-09 20:36:09 +00:00
|
|
|
#ifdef HAVE_STORAGE_FLUSH
|
|
|
|
storage_flush();
|
|
|
|
#endif
|
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
if (storage_disk_is_active())
|
2008-11-01 16:14:28 +00:00
|
|
|
storage_spindown(1);
|
2006-11-11 01:18:57 +00:00
|
|
|
}
|
2008-12-24 16:58:41 +00:00
|
|
|
|
|
|
|
#if CONFIG_CODEC == SWCODEC
|
2006-12-06 10:24:59 +00:00
|
|
|
audiohw_close();
|
2008-12-24 16:58:41 +00:00
|
|
|
#else
|
|
|
|
mp3_shutdown();
|
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
|
2008-12-24 16:58:41 +00:00
|
|
|
shutdown_timeout in power_thread_step will force a power off */
|
|
|
|
while (storage_disk_is_active())
|
2006-11-11 01:18:57 +00:00
|
|
|
sleep(HZ/10);
|
2008-12-24 16:58:41 +00:00
|
|
|
|
2008-12-09 01:33:01 +00:00
|
|
|
#ifndef HAVE_LCD_COLOR
|
2005-02-06 09:57:57 +00:00
|
|
|
lcd_set_contrast(0);
|
2008-12-09 01:33:01 +00:00
|
|
|
#endif
|
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-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();
|
|
|
|
}
|
2007-11-03 05:00:49 +00:00
|
|
|
|
2011-03-08 19:33:30 +00:00
|
|
|
void set_poweroff_timeout(int timeout)
|
|
|
|
{
|
|
|
|
poweroff_timeout = timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
void reset_poweroff_timer(void)
|
|
|
|
{
|
|
|
|
last_event_tick = current_tick;
|
2011-12-26 09:30:25 +00:00
|
|
|
if (sleeptimer_active && sleeptimer_key_restarts)
|
|
|
|
set_sleep_timer(sleeptimer_duration);
|
2011-03-08 19:33:30 +00:00
|
|
|
}
|
|
|
|
|
2008-12-24 16:58:41 +00:00
|
|
|
void sys_poweroff(void)
|
|
|
|
{
|
|
|
|
#ifndef BOOTLOADER
|
|
|
|
logf("sys_poweroff()");
|
|
|
|
/* If the main thread fails to shut down the system, we will force a
|
|
|
|
power off after an 20 second timeout - 28 seconds if recording */
|
|
|
|
if (shutdown_timeout == 0) {
|
2011-03-01 21:19:30 +00:00
|
|
|
#if (defined(IAUDIO_X5) || defined(IAUDIO_M5) || defined(COWON_D2)) && !defined(SIMULATOR)
|
2008-12-24 16:58:41 +00:00
|
|
|
pcf50606_reset_timeout(); /* Reset timer on first attempt only */
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_RECORDING
|
|
|
|
if (audio_status() & AUDIO_STATUS_RECORD)
|
|
|
|
shutdown_timeout += HZ*8;
|
|
|
|
#endif
|
2009-10-21 15:28:11 +00:00
|
|
|
#ifdef IPOD_NANO2G
|
|
|
|
/* The FTL alone may take half a minute to shut down cleanly. */
|
|
|
|
shutdown_timeout += HZ*60;
|
|
|
|
#else
|
2008-12-24 16:58:41 +00:00
|
|
|
shutdown_timeout += HZ*20;
|
2009-10-21 15:28:11 +00:00
|
|
|
#endif
|
2008-12-24 16:58:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
queue_broadcast(SYS_POWEROFF, 0);
|
|
|
|
#endif /* BOOTLOADER */
|
|
|
|
}
|
|
|
|
|
|
|
|
void cancel_shutdown(void)
|
|
|
|
{
|
|
|
|
logf("cancel_shutdown()");
|
|
|
|
|
2011-03-01 21:19:30 +00:00
|
|
|
#if (defined(IAUDIO_X5) || defined(IAUDIO_M5) || defined(COWON_D2)) && !defined(SIMULATOR)
|
2008-12-24 16:58:41 +00:00
|
|
|
/* TODO: Move some things to target/ tree */
|
|
|
|
if (shutdown_timeout)
|
|
|
|
pcf50606_reset_timeout();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
shutdown_timeout = 0;
|
|
|
|
}
|
|
|
|
|
2007-11-03 13:45:12 +00:00
|
|
|
/* Send system battery level update events on reaching certain significant
|
2008-12-24 16:58:41 +00:00
|
|
|
levels. This must be called after battery_percent has been updated. */
|
|
|
|
void send_battery_level_event(void)
|
2007-11-03 05:00:49 +00:00
|
|
|
{
|
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;
|
2008-12-24 16:58:41 +00:00
|
|
|
|
2007-11-03 05:00:49 +00:00
|
|
|
while (*level)
|
|
|
|
{
|
2008-12-24 16:58:41 +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;
|
|
|
|
}
|
2008-12-24 16:58:41 +00:00
|
|
|
|
2007-11-03 05:00:49 +00:00
|
|
|
level++;
|
|
|
|
}
|
|
|
|
}
|
2011-02-28 23:05:00 +00:00
|
|
|
|
2012-10-05 19:30:13 +00:00
|
|
|
void set_sleeptimer_duration(int minutes)
|
|
|
|
{
|
|
|
|
set_sleep_timer(minutes * 60);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void set_sleep_timer(int seconds)
|
2011-02-28 23:05:00 +00:00
|
|
|
{
|
|
|
|
if (seconds) {
|
|
|
|
sleeptimer_active = true;
|
|
|
|
sleeptimer_endtick = current_tick + seconds * HZ;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
sleeptimer_active = false;
|
|
|
|
sleeptimer_endtick = 0;
|
|
|
|
}
|
2011-12-26 09:30:25 +00:00
|
|
|
sleeptimer_duration = seconds;
|
2011-02-28 23:05:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int get_sleep_timer(void)
|
|
|
|
{
|
|
|
|
if (sleeptimer_active && (sleeptimer_endtick >= current_tick))
|
|
|
|
return (sleeptimer_endtick - current_tick) / HZ;
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-26 09:30:25 +00:00
|
|
|
void set_keypress_restarts_sleep_timer(bool enable)
|
|
|
|
{
|
|
|
|
sleeptimer_key_restarts = enable;
|
|
|
|
}
|
|
|
|
|
2012-03-15 13:58:15 +00:00
|
|
|
#ifndef BOOTLOADER
|
2011-03-08 19:33:30 +00:00
|
|
|
static void handle_sleep_timer(void)
|
2011-02-28 23:05:00 +00:00
|
|
|
{
|
|
|
|
if (!sleeptimer_active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Handle sleeptimer */
|
|
|
|
if (TIME_AFTER(current_tick, sleeptimer_endtick)) {
|
|
|
|
if (usb_inserted()
|
|
|
|
#if CONFIG_CHARGING && !defined(HAVE_POWEROFF_WHILE_CHARGING)
|
|
|
|
|| charger_input_state != NO_CHARGER
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
DEBUGF("Sleep timer timeout. Stopping...\n");
|
2011-03-10 15:50:15 +00:00
|
|
|
audio_pause();
|
2011-02-28 23:05:00 +00:00
|
|
|
set_sleep_timer(0);
|
|
|
|
backlight_off(); /* Nighty, nighty... */
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DEBUGF("Sleep timer timeout. Shutting off...\n");
|
|
|
|
sys_poweroff();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-15 13:58:15 +00:00
|
|
|
#endif /* BOOTLOADER */
|
2011-03-08 19:33:25 +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
|
|
|
|
* 3) The battery level has reached shutdown limit
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* 4) The radio is playing
|
|
|
|
*/
|
2011-03-08 19:33:30 +00:00
|
|
|
void handle_auto_poweroff(void)
|
2011-03-08 19:33:25 +00:00
|
|
|
{
|
2012-03-15 13:58:15 +00:00
|
|
|
#ifndef BOOTLOADER
|
2011-03-08 19:33:25 +00:00
|
|
|
long timeout = poweroff_timeout*60*HZ;
|
|
|
|
int audio_stat = audio_status();
|
|
|
|
long tick = current_tick;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inhibit shutdown as long as the charger is plugged in. If it is
|
|
|
|
* unplugged, wait for a timeout period and then shut down.
|
|
|
|
*/
|
2011-04-16 16:57:35 +00:00
|
|
|
if (audio_stat == AUDIO_STATUS_PLAY
|
|
|
|
#if CONFIG_CHARGING
|
|
|
|
|| charger_input_state == CHARGER
|
|
|
|
#endif
|
|
|
|
) {
|
2011-03-08 19:33:25 +00:00
|
|
|
last_event_tick = current_tick;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!shutdown_timeout && query_force_shutdown()) {
|
|
|
|
backlight_on();
|
|
|
|
sys_poweroff();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (timeout &&
|
|
|
|
#if CONFIG_TUNER
|
|
|
|
!(get_radio_status() & FMRADIO_PLAYING) &&
|
|
|
|
#endif
|
|
|
|
!usb_inserted() &&
|
|
|
|
(audio_stat == 0 ||
|
|
|
|
(audio_stat == (AUDIO_STATUS_PLAY | AUDIO_STATUS_PAUSE) &&
|
|
|
|
!sleeptimer_active))) {
|
|
|
|
|
|
|
|
if (TIME_AFTER(tick, last_event_tick + timeout)
|
2011-03-08 19:33:30 +00:00
|
|
|
#if !(CONFIG_PLATFORM & PLATFORM_HOSTED)
|
2011-03-08 19:33:25 +00:00
|
|
|
&& TIME_AFTER(tick, storage_last_disk_activity() + timeout)
|
|
|
|
#endif
|
|
|
|
) {
|
|
|
|
sys_poweroff();
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
handle_sleep_timer();
|
2012-03-15 13:58:15 +00:00
|
|
|
#endif
|
2011-03-08 19:33:25 +00:00
|
|
|
}
|