Fixed red build
[kugel-rb.git] / firmware / powermgmt.c
blob93d3192904db29c7e5f1b97cef23139b77ae2fe6
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 by Heikki Hannikainen
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
19 #include "config.h"
20 #include "sh7034.h"
21 #include "kernel.h"
22 #include "thread.h"
23 #include "system.h"
24 #include "debug.h"
25 #include "panic.h"
26 #include "adc.h"
27 #include "string.h"
28 #include "sprintf.h"
29 #include "ata.h"
30 #include "power.h"
31 #include "button.h"
32 #include "ata.h"
33 #include "mpeg.h"
34 #include "usb.h"
35 #include "powermgmt.h"
37 #ifdef SIMULATOR
39 int battery_level(void)
41 return 100;
44 bool battery_level_safe(void)
46 return true;
49 void set_poweroff_timeout(int timeout)
51 (void)timeout;
54 #else /* not SIMULATOR */
56 static int poweroff_idle_timeout_value[15] =
58 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 15, 30, 45, 60
61 static char power_stack[DEFAULT_STACK_SIZE];
62 static char power_thread_name[] = "power";
64 static int poweroff_timeout = 0;
65 static long last_charge_time = 0;
67 unsigned short power_history[POWER_HISTORY_LEN];
68 #ifdef HAVE_CHARGE_CTRL
69 char power_message[POWER_MESSAGE_LEN] = "";
70 char charge_restart_level = CHARGE_RESTART_HI;
71 #endif
73 /* Returns battery level in percent */
74 int battery_level(void)
76 int level = 0;
77 int c = 0;
78 int i;
80 /* calculate average over last 3 minutes (skip empty samples) */
81 for (i = 0; i < 3; i++)
82 if (power_history[POWER_HISTORY_LEN-1-i]) {
83 level += power_history[POWER_HISTORY_LEN-1-i];
84 c++;
87 if (c)
88 level = level / c; /* avg */
89 else /* history was empty, get a fresh sample */
90 level = (adc_read(ADC_UNREG_POWER) * BATTERY_SCALE_FACTOR) / 10000;
92 if(level > BATTERY_LEVEL_FULL)
93 level = BATTERY_LEVEL_FULL;
95 if(level < BATTERY_LEVEL_EMPTY)
96 level = BATTERY_LEVEL_EMPTY;
98 return ((level-BATTERY_LEVEL_EMPTY) * 100) / BATTERY_RANGE;
101 /* Tells if the battery level is safe for disk writes */
102 bool battery_level_safe(void)
104 /* I'm pretty sure we don't want an average over a long time here */
105 if (power_history[POWER_HISTORY_LEN-1])
106 return power_history[POWER_HISTORY_LEN-1] > BATTERY_LEVEL_DANGEROUS;
107 else
108 return adc_read(ADC_UNREG_POWER) > (BATTERY_LEVEL_DANGEROUS * 10000) / BATTERY_SCALE_FACTOR;
111 void set_poweroff_timeout(int timeout)
113 poweroff_timeout = timeout;
116 /* We shut off in the following cases:
117 1) The unit is idle, not playing music
118 2) The unit is playing music, but is paused
120 We do not shut off in the following cases:
121 1) The USB is connected
122 2) The charger is connected
123 3) We are recording, or recording with pause
125 static void handle_auto_poweroff(void)
127 long timeout = poweroff_idle_timeout_value[poweroff_timeout]*60*HZ;
128 int mpeg_stat = mpeg_status();
129 bool charger_is_inserted = charger_inserted();
130 static bool charger_was_inserted = false;
132 /* The was_inserted thing prevents the unit to shut down immediately
133 when the charger is extracted */
134 if(charger_is_inserted || charger_was_inserted)
136 last_charge_time = current_tick;
138 charger_was_inserted = charger_is_inserted;
140 if(timeout &&
141 !usb_inserted() &&
142 (mpeg_stat == 0 ||
143 mpeg_stat == (MPEG_STATUS_PLAY | MPEG_STATUS_PAUSE)))
145 if(TIME_AFTER(current_tick, last_keypress + timeout) &&
146 TIME_AFTER(current_tick, last_disk_activity + timeout) &&
147 TIME_AFTER(current_tick, last_charge_time + timeout))
148 power_off();
153 * This power thread maintains a history of battery voltage
154 * and implements a charging algorithm.
155 * Battery 'fullness' can be determined by the voltage drop, see:
157 * http://www.nimhbattery.com/nimhbattery-faq.htm questions 3 & 4
158 * http://www.powerpacks-uk.com/Charging%20NiMh%20Batteries.htm
159 * http://www.angelfire.com/electronic/hayles/charge1.html (soft start idea)
160 * http://www.powerstream.com/NiMH.htm (discouraging)
161 * http://www.panasonic.com/industrial/battery/oem/images/pdf/nimhchar.pdf
162 * http://www.duracell.com/oem/Pdf/others/nimh_5.pdf (discharging)
163 * http://www.duracell.com/oem/Pdf/others/nimh_6.pdf (charging)
165 * Charging logic which we're starting with (by Linus, Hes, Bagder):
167 * 1) max 16 hrs charge time (just in negative delta detection fails)
168 * 2) Stop at negative delta of 5 mins
169 * 3) Stop at 15 mins of zero-delta or below
170 * 4) minimum of 15 mins charge time before 2) is applied
171 * 5) after end of charging, wait for charge go down 80%
172 * before charging again if in 'no-use overnight charging mode'
173 * and down to 10% if in 'fixed-location mains-powered usage mode'
176 static void power_thread(void)
178 int i;
179 int avg, ok_samples, spin_samples;
180 #ifdef HAVE_CHARGE_CTRL
181 int delta;
182 int charged_time = 0;
183 #endif
185 while (1)
187 /* Make POWER_AVG measurements and calculate an average of that to
188 * reduce the effect of backlights/disk spinning/other variation.
190 ok_samples = spin_samples = avg = 0;
191 for (i = 0; i < POWER_AVG_N; i++) {
192 if (ata_disk_is_active()) {
193 if (!ok_samples) {
194 /* if we don't have any good non-disk-spinning samples,
195 * we take a sample anyway in case the disk is going
196 * to spin all the time.
198 avg += adc_read(ADC_UNREG_POWER);
199 spin_samples++;
201 } else {
202 if (spin_samples) /* throw away disk-spinning samples */
203 spin_samples = avg = 0;
204 avg += adc_read(ADC_UNREG_POWER);
205 ok_samples++;
207 sleep(HZ*POWER_AVG_SLEEP);
209 avg = avg / ((ok_samples) ? ok_samples : spin_samples);
211 /* rotate the power history */
212 for (i = 0; i < POWER_HISTORY_LEN-1; i++)
213 power_history[i] = power_history[i+1];
215 /* insert new value in the end, in centivolts 8-) */
216 power_history[POWER_HISTORY_LEN-1] = (avg * BATTERY_SCALE_FACTOR) / 10000;
218 #ifdef HAVE_CHARGE_CTRL
219 if (charger_inserted()) {
220 if (charger_enabled) {
221 /* charger inserted and enabled */
222 charged_time++;
223 if (charged_time > CHARGE_MAX_TIME) {
224 DEBUGF("power: charged_time > CHARGE_MAX_TIME, enough!\n");
225 /* have charged too long and deltaV detection did not work! */
226 charger_enable(false);
227 snprintf(power_message, POWER_MESSAGE_LEN, "Chg tmout %d min", CHARGE_MAX_TIME);
228 /* Perhaps we should disable charging for several
229 hours from this point, just to be sure. */
230 } else {
231 if (charged_time > CHARGE_MIN_TIME) {
232 /* have charged continuously over the minimum charging time,
233 * so we monitor for deltaV going negative. Multiply things
234 * by 100 to get more accuracy without floating point arithmetic.
235 * power_history[] contains centivolts so after multiplying by 100
236 * the deltas are in tenths of millivolts (delta of 5 is
237 * 0.0005 V).
239 delta = ( power_history[POWER_HISTORY_LEN-1] * 100
240 + power_history[POWER_HISTORY_LEN-2] * 100
241 - power_history[POWER_HISTORY_LEN-1-CHARGE_END_NEGD+1] * 100
242 - power_history[POWER_HISTORY_LEN-1-CHARGE_END_NEGD] * 100 )
243 / CHARGE_END_NEGD / 2;
245 if (delta < -100) { /* delta < -10 mV */
246 DEBUGF("power: short-term negative delta, enough!\n");
247 charger_enable(false);
248 snprintf(power_message, POWER_MESSAGE_LEN, "end negd %d %dmin", delta, charged_time);
249 } else {
250 /* if we didn't disable the charger in the previous test, check for low positive delta */
251 delta = ( power_history[POWER_HISTORY_LEN-1] * 100
252 + power_history[POWER_HISTORY_LEN-2] * 100
253 - power_history[POWER_HISTORY_LEN-1-CHARGE_END_ZEROD+1] * 100
254 - power_history[POWER_HISTORY_LEN-1-CHARGE_END_ZEROD] * 100 )
255 / CHARGE_END_ZEROD / 2;
257 if (delta < 1) { /* delta < 0.1 mV */
258 DEBUGF("power: long-term small positive delta, enough!\n");
259 charger_enable(false);
260 snprintf(power_message, POWER_MESSAGE_LEN, "end lowd %d %dmin", delta, charged_time);
265 } else {
266 /* charged inserted but not enabled */
267 /* if battery is not full, enable charging */
268 if (battery_level() < charge_restart_level) {
269 DEBUGF("power: charger inserted and battery not full, enabling\n");
270 charger_enable(true);
271 charged_time = 0;
272 /* clear the power history so that we don't use values before
273 * discharge for the long-term delta
275 for (i = 0; i < POWER_HISTORY_LEN-1; i++)
276 power_history[i] = power_history[POWER_HISTORY_LEN-1];
277 snprintf(power_message, POWER_MESSAGE_LEN, "Chg started at %d%%", battery_level());
280 } else {
281 /* charger not inserted */
282 if (charger_enabled) {
283 /* charger not inserted but was enabled */
284 DEBUGF("power: charger disconnected, disabling\n");
285 charger_enable(false);
286 snprintf(power_message, POWER_MESSAGE_LEN, "Charger disc");
288 /* charger not inserted and disabled, so we're discharging */
290 #endif /* HAVE_CHARGE_CTRL*/
292 /* sleep for roughly a minute */
293 sleep(HZ*(60 - POWER_AVG_N * POWER_AVG_SLEEP));
295 handle_auto_poweroff();
299 void power_init(void)
301 /* init history to 0 */
302 memset(power_history, 0x00, sizeof(power_history));
303 /* initialize the history with a single sample to prevent level
304 flickering during the first minute of execution */
305 power_history[POWER_HISTORY_LEN-1] = (adc_read(ADC_UNREG_POWER) * BATTERY_SCALE_FACTOR) / 10000;
307 #ifdef HAVE_CHARGE_CTRL
308 snprintf(power_message, POWER_MESSAGE_LEN, "Powermgmt started");
309 #endif
310 create_thread(power_thread, power_stack, sizeof(power_stack), power_thread_name);
313 #endif /* SIMULATOR */