summaryrefslogtreecommitdiff
path: root/firmware/export/powermgmt.h
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/export/powermgmt.h')
-rw-r--r--firmware/export/powermgmt.h195
1 files changed, 73 insertions, 122 deletions
diff --git a/firmware/export/powermgmt.h b/firmware/export/powermgmt.h
index c333795ad7..5be3a39c1d 100644
--- a/firmware/export/powermgmt.h
+++ b/firmware/export/powermgmt.h
@@ -25,147 +25,101 @@
25 25
26#define POWER_HISTORY_LEN 2*60 /* 2 hours of samples, one per minute */ 26#define POWER_HISTORY_LEN 2*60 /* 2 hours of samples, one per minute */
27 27
28#define CHARGE_END_SHORTD 6 /* stop when N minutes have passed with 28enum charge_state_type
29 * avg delta being < -0.05 V */ 29{
30#define CHARGE_END_LONGD 50 /* stop when N minutes have passed with 30 /* sorted by increasing charging current */
31 * avg delta being < -0.02 V */
32
33typedef enum { /* sorted by increasing charging current */
34#if CONFIG_CHARGING >= CHARGING_MONITOR 31#if CONFIG_CHARGING >= CHARGING_MONITOR
35 CHARGE_STATE_DISABLED = -2, /* Disable charger use */ 32 CHARGE_STATE_DISABLED = -2, /* Disable charger use (safety measure) */
36 CHARGE_STATE_ERROR = -1, /* Some error occurred that should not allow 33 CHARGE_STATE_ERROR = -1, /* Some error occurred that should not allow
37 further attempts without user intervention */ 34 turning on the charger again by software
35 without user intervention (ie. replug) */
38#endif 36#endif
39 DISCHARGING = 0, 37 DISCHARGING = 0,
40#if CONFIG_CHARGING >= CHARGING_MONITOR 38#if CONFIG_CHARGING >= CHARGING_MONITOR
41 TRICKLE, /* Can occur for CONFIG_CHARGING >= CHARGING_MONITOR */ 39 TRICKLE, /* For NiCd, battery maintenence phase */
42 /* For LiIon, the low-current precharge mode if battery 40 /* For LiIon, low-current precharge phase */
43 was very low */ 41 TOPOFF, /* For NiCd, waiting for dead zone */
44 TOPOFF, /* Can occur for CONFIG_CHARGING == CHARGING_CONTROL */
45 /* For LiIon, constant voltage phase */ 42 /* For LiIon, constant voltage phase */
46 CHARGING, /* Can occur for all CONFIG_CHARGING options */ 43 CHARGING, /* For NiCd, main charge phase */
47 /* For LiIon, the constant current phase */ 44 /* For LiIon, constant current phase */
48#endif 45#endif
49} charge_state_type; 46};
50 47
51/* tells what the charger is doing */ 48/* tells what the charger is doing */
52extern charge_state_type charge_state; 49extern enum charge_state_type charge_state;
53 50
54#ifdef CONFIG_CHARGING 51#ifdef CONFIG_CHARGING
55/* 52/*
56 * Flag that the charger has been plugged in/removed: this is set for exactly 53 * Flag that the charger has been plugged in/removed: this is set for exactly
57 * one time through the power loop when the charger has been plugged in. 54 * one time through the power loop when the charger has been plugged in.
58 */ 55 */
59typedef enum { 56enum charger_input_state_type
57{
60 NO_CHARGER = 0, /* No charger is present */ 58 NO_CHARGER = 0, /* No charger is present */
61 CHARGER_UNPLUGGED, /* Transitional state during CHARGER=>NO_CHARGER */ 59 CHARGER_UNPLUGGED, /* Transitional state during CHARGER=>NO_CHARGER */
62 CHARGER_PLUGGED, /* Transitional state during NO_CHARGER=>CHARGER */ 60 CHARGER_PLUGGED, /* Transitional state during NO_CHARGER=>CHARGER */
63 CHARGER /* Charger is present */ 61 CHARGER /* Charger is present */
64} charger_input_state_type; 62};
65 63
66/* tells the state of the charge input */ 64/* tells the state of the charge input */
67extern charger_input_state_type charger_input_state; 65extern enum charger_input_state_type charger_input_state;
66
67/* Power input status saved on the power thread each loop */
68extern unsigned int power_thread_inputs;
69
70#endif /* CONFIG_CHARGING */
71
72#if CONFIG_CHARGING == CHARGING_TARGET
73/* Include target-specific definitions */
74#include "powermgmt-target.h"
68#endif 75#endif
69 76
70#ifndef SIMULATOR 77#ifndef SIMULATOR
71 78
72#if CONFIG_CHARGING == CHARGING_CONTROL 79/* Generic current values that are really rather meaningless - config header
73#define START_TOPOFF_CHG 85 /* Battery % to start at top-off */ 80 * should define proper numbers. */
74#define START_TRICKLE_CHG 95 /* Battery % to start at trickle */ 81#ifndef CURRENT_NORMAL
75 82#define CURRENT_NORMAL 145 /* usual current in mA */
76#define POWER_MESSAGE_LEN 32 /* power thread status message */
77#define CHARGE_MAX_TIME_1500 450 /* minutes: maximum charging time for 1500 mAh batteries */
78 /* actual max time depends also on BATTERY_CAPACITY! */
79#define CHARGE_MIN_TIME 10 /* minutes: minimum charging time */
80#define TOPOFF_MAX_TIME 90 /* After charging, go to top off charge. How long should top off charge be? */
81#define TOPOFF_VOLTAGE 5650 /* which voltage is best? (millivolts) */
82#define TRICKLE_MAX_TIME 12*60 /* After top off charge, go to trickle charge. How long should trickle charge be? */
83#define TRICKLE_VOLTAGE 5450 /* which voltage is best? (millivolts) */
84
85#define START_TOPOFF_SEC 25 /* initial trickle_sec for topoff */
86#define START_TRICKLE_SEC 15 /* initial trickle_sec for trickle */
87
88#define PID_DEADZONE 4 /* PID proportional deadzone */
89
90extern char power_message[POWER_MESSAGE_LEN];
91
92extern int long_delta; /* long term delta battery voltage */
93extern int short_delta; /* short term delta battery voltage */
94
95extern int powermgmt_last_cycle_startstop_min; /* how many minutes ago was the charging started or stopped? */
96extern int powermgmt_last_cycle_level; /* which level had the batteries at this time? */
97
98extern int pid_p; /* PID proportional term */
99extern int pid_i; /* PID integral term */
100extern int trickle_sec; /* trickle charge: How many seconds per minute are we charging actually? */
101
102#endif /* CONFIG_CHARGING == CHARGING_CONTROL */
103
104#if defined(ARCHOS_ONDIOSP) || defined(ARCHOS_ONDIOFM) /* Values for Ondio */
105# define CURRENT_NORMAL 95 /* average, nearly proportional to 1/U */
106# define CURRENT_USB 1 /* host powered in USB mode; avoid zero-div */
107# define CURRENT_BACKLIGHT 0 /* no backlight */
108#else /* Values for HD based jukeboxes */
109#ifdef IRIVER_H100_SERIES
110# define CURRENT_NORMAL 80 /* 16h playback on 1300mAh battery */
111# define CURRENT_BACKLIGHT 23 /* from IriverBattery twiki page */
112# define CURRENT_SPDIF_OUT 10 /* optical SPDIF output on */
113# define CURRENT_RECORD 105 /* additional current while recording */
114#elif defined(IRIVER_H300_SERIES)
115# define CURRENT_NORMAL 80 /* 16h playback on 1300mAh battery from IriverRuntime wiki page */
116# define CURRENT_BACKLIGHT 23 /* FIXME: This needs to be measured, copied from H100 */
117# define CURRENT_RECORD 110 /* additional current while recording */
118#elif defined(IPOD_NANO) /* iPOD Nano */
119# define CURRENT_NORMAL 32 /* MP3: ~9h playback out of 300mAh battery */
120# define CURRENT_BACKLIGHT 20 /* FIXME: this needs adjusting */
121#if defined(HAVE_RECORDING)
122# define CURRENT_RECORD 35 /* FIXME: this needs adjusting */
123#endif 83#endif
124#elif defined(IPOD_VIDEO) /* iPOD Video */ 84
125# define CURRENT_NORMAL 35 /* MP3: ~11h out of 400mAh battery (30GB) or ~17h out of 600mAh (60GB) */ 85#ifndef CURRENT_BACKLIGHT
126# define CURRENT_BACKLIGHT 20 /* FIXME: this needs adjusting */ 86#define CURRENT_BACKLIGHT 30 /* additional current when backlight always on */
127#if defined(HAVE_RECORDING)
128# define CURRENT_RECORD 35 /* FIXME: this needs adjusting */
129#endif 87#endif
130#elif defined(SANSA_E200) /* Sandisk E200v1 */ 88
131# define CURRENT_NORMAL 45 /* Mike's measurements in Jan 2008 */ 89#ifdef HAVE_RECORDING
132# define CURRENT_BACKLIGHT 40 /* Screen is about 20, blue LEDs are another 20, so 40 if both */ 90#ifndef CURRENT_RECORD
133# define CURRENT_RECORD 40 /* flash player, so this is just unboosted current*/ 91#define CURRENT_RECORD 35 /* additional recording current */
134#elif defined(SANSA_C200) /* Sandisk C200v1 */
135# define CURRENT_NORMAL 45 /* Should be nearly identical to E200 */
136# define CURRENT_BACKLIGHT 40 /* Screen is about 20, blue LEDs are another 20, so 40 if both */
137# define CURRENT_RECORD 40 /* flash player, so this is just unboosted current*/
138#elif defined(IPOD_4G) /* iPOD 4G */
139# define CURRENT_NORMAL 100 /* MP3: ~10.5h out of 1100mAh battery */
140# define CURRENT_BACKLIGHT 20 /* FIXME: this needs adjusting */
141#if defined(HAVE_RECORDING)
142# define CURRENT_RECORD 35 /* FIXME: this needs adjusting */
143#endif 92#endif
144#else /* Not iriver H1x0, H3x0, nor Archos Ondio, nor iPod nano/Video/4G, nor Sansas */ 93#endif /* HAVE_RECORDING */
145# define CURRENT_NORMAL 145 /* usual current in mA when using the AJB including some disk/backlight/... activity */ 94
146# define CURRENT_BACKLIGHT 30 /* additional current when backlight always on */ 95#ifndef CURRENT_USB
147#if defined(HAVE_RECORDING) 96#define CURRENT_USB 500 /* usual current in mA in USB mode */
148# define CURRENT_RECORD 35 /* FIXME: this needs adjusting */
149#endif 97#endif
150#endif /* Not Archos Ondio */ 98
151#define CURRENT_USB 500 /* usual current in mA in USB mode */
152#ifdef HAVE_REMOTE_LCD 99#ifdef HAVE_REMOTE_LCD
153# define CURRENT_REMOTE 8 /* add. current when H100-remote connected */ 100#define CURRENT_REMOTE 8 /* additional current when remote connected */
154#endif /* HAVE_REMOTE_LCD */ 101#endif /* HAVE_REMOTE_LCD */
155 102
156# define CURRENT_MIN_CHG 70 /* minimum charge current */ 103#if CONFIG_CHARGING
157# define MIN_CHG_V 8500 /* at 8.5v charger voltage get CURRENT_MIN_CHG */ 104#ifndef CURRENT_MAX_CHG
158# ifdef IRIVER_H300_SERIES 105#define CURRENT_MAX_CHG 350 /* maximum charging current */
159# define CURRENT_MAX_CHG 650 /* maximum charging current */ 106#endif
160# else 107#endif /* CONFIG_CHARGING */
161# define CURRENT_MAX_CHG 350 /* maximum charging current */ 108
162# endif 109#ifdef CHARGING_DEBUG_FILE
163# define MAX_CHG_V 10250 /* anything over 10.25v gives CURRENT_MAX_CHG */ 110#define POWERMGMT_DEBUG_STACK ((0x1000)/sizeof(long))
164#endif /* not ONDIO */ 111#else
112#define POWERMGMT_DEBUG_STACK 0
113#endif
165 114
166#if CONFIG_CHARGING == CHARGING_TARGET 115#ifndef BATT_AVE_SAMPLES
167/* Include target-specific definitions */ 116/* slw filter constant unless otherwise specified */
168#include "powermgmt-target.h" 117#define BATT_AVE_SAMPLES 128
118#endif
119
120#ifndef POWER_THREAD_STEP_TICKS
121/* 2HZ sample rate unless otherwise specified */
122#define POWER_THREAD_STEP_TICKS (HZ/2)
169#endif 123#endif
170 124
171extern unsigned short power_history[POWER_HISTORY_LEN]; 125extern unsigned short power_history[POWER_HISTORY_LEN];
@@ -179,12 +133,6 @@ extern const unsigned short percent_to_volt_charge[11];
179/* Start up power management thread */ 133/* Start up power management thread */
180void powermgmt_init(void); 134void powermgmt_init(void);
181 135
182/* Do target portion of init (for CHARGING_TARGET) - called on power thread */
183void powermgmt_init_target(void);
184
185/* Handle frequent tasks and call charging_algorithm_small_step */
186void power_thread_sleep(int ticks);
187
188#endif /* SIMULATOR */ 136#endif /* SIMULATOR */
189 137
190/* Returns battery statust */ 138/* Returns battery statust */
@@ -193,9 +141,15 @@ int battery_time(void); /* minutes */
193unsigned int battery_adc_voltage(void); /* voltage from ADC in millivolts */ 141unsigned int battery_adc_voltage(void); /* voltage from ADC in millivolts */
194unsigned int battery_voltage(void); /* filtered batt. voltage in millivolts */ 142unsigned int battery_voltage(void); /* filtered batt. voltage in millivolts */
195 143
144#ifdef HAVE_BATTERY_SWITCH
145unsigned int input_millivolts(void); /* voltage that device is running from */
146
196/* Set the filtered battery voltage (to adjust it before beginning a charge 147/* Set the filtered battery voltage (to adjust it before beginning a charge
197 cycle for instance where old, loaded readings will likely be invalid). */ 148 * cycle for instance where old, loaded readings will likely be invalid).
198void set_filtered_battery_voltage(int millivolts); 149 * Also readjust when battery switch is opened or closed.
150 */
151void reset_battery_filter(int millivolts);
152#endif /* HAVE_BATTERY_SWITCH */
199 153
200/* read unfiltered battery info */ 154/* read unfiltered battery info */
201void battery_read_info(int *voltage, int *level); 155void battery_read_info(int *voltage, int *level);
@@ -203,13 +157,10 @@ void battery_read_info(int *voltage, int *level);
203/* Tells if the battery level is safe for disk writes */ 157/* Tells if the battery level is safe for disk writes */
204bool battery_level_safe(void); 158bool battery_level_safe(void);
205 159
206#ifdef TARGET_POWERMGMT_FILTER_CHARGE_STATE
207int powermgmt_filter_charge_state(void);
208#endif
209
210void set_poweroff_timeout(int timeout); 160void set_poweroff_timeout(int timeout);
211void set_battery_capacity(int capacity); /* set local battery capacity value */ 161void set_battery_capacity(int capacity); /* set local battery capacity value */
212void set_battery_type(int type); /* set local battery type */ 162int get_battery_capacity(void); /* get local battery capacity value */
163void set_battery_type(int type); /* set local battery type */
213 164
214void set_sleep_timer(int seconds); 165void set_sleep_timer(int seconds);
215int get_sleep_timer(void); 166int get_sleep_timer(void);