removed entry for status bar
[kugel-rb.git] / firmware / powermgmt.c
blob8791338ab43d25c50d4e3d9580015dcf84b761ec
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 "powermgmt.h"
33 #ifdef SIMULATOR
35 int battery_level(void)
37 return 100;
40 bool battery_level_safe(void)
42 return true;
45 #else /* not SIMULATOR */
47 static char power_stack[DEFAULT_STACK_SIZE];
48 static char power_thread_name[] = "power";
50 unsigned short power_history[POWER_HISTORY_LEN];
51 #ifdef HAVE_CHARGE_CTRL
52 char power_message[POWER_MESSAGE_LEN] = "";
53 char charge_restart_level = CHARGE_RESTART_HI;
54 #endif
56 /* Returns battery level in percent */
57 int battery_level(void)
59 int level = 0;
60 int c = 0;
61 int i;
63 /* calculate average over last 3 minutes (skip empty samples) */
64 for (i = 0; i < 3; i++)
65 if (power_history[POWER_HISTORY_LEN-1-i]) {
66 level += power_history[POWER_HISTORY_LEN-1-i];
67 c++;
70 if (c)
71 level = level / c; /* avg */
72 else /* history was empty, get a fresh sample */
73 level = (adc_read(ADC_UNREG_POWER) * BATTERY_SCALE_FACTOR) / 10000;
75 if(level > BATTERY_LEVEL_FULL)
76 level = BATTERY_LEVEL_FULL;
78 if(level < BATTERY_LEVEL_EMPTY)
79 level = BATTERY_LEVEL_EMPTY;
81 return ((level-BATTERY_LEVEL_EMPTY) * 100) / BATTERY_RANGE;
84 /* Tells if the battery level is safe for disk writes */
85 bool battery_level_safe(void)
87 /* I'm pretty sure we don't want an average over a long time here */
88 if (power_history[POWER_HISTORY_LEN-1])
89 return power_history[POWER_HISTORY_LEN-1] > BATTERY_LEVEL_DANGEROUS;
90 else
91 return adc_read(ADC_UNREG_POWER) > (BATTERY_LEVEL_DANGEROUS * 10000) / BATTERY_SCALE_FACTOR;
95 * This power thread maintains a history of battery voltage
96 * and implements a charging algorithm.
97 * Battery 'fullness' can be determined by the voltage drop, see:
99 * http://www.nimhbattery.com/nimhbattery-faq.htm questions 3 & 4
100 * http://www.powerpacks-uk.com/Charging%20NiMh%20Batteries.htm
101 * http://www.angelfire.com/electronic/hayles/charge1.html (soft start idea)
102 * http://www.powerstream.com/NiMH.htm (discouraging)
103 * http://www.panasonic.com/industrial/battery/oem/images/pdf/nimhchar.pdf
104 * http://www.duracell.com/oem/Pdf/others/nimh_5.pdf (discharging)
105 * http://www.duracell.com/oem/Pdf/others/nimh_6.pdf (charging)
107 * Charging logic which we're starting with (by Linus, Hes, Bagder):
109 * 1) max 16 hrs charge time (just in negative delta detection fails)
110 * 2) Stop at negative delta of 5 mins
111 * 3) Stop at 15 mins of zero-delta or below
112 * 4) minimum of 15 mins charge time before 2) is applied
113 * 5) after end of charging, wait for charge go down 80%
114 * before charging again if in 'no-use overnight charging mode'
115 * and down to 10% if in 'fixed-location mains-powered usage mode'
118 static void power_thread(void)
120 int i;
121 int avg, ok_samples, spin_samples;
122 #ifdef HAVE_CHARGE_CTRL
123 int delta;
124 int charged_time = 0;
125 #endif
127 while (1)
129 /* Make POWER_AVG measurements and calculate an average of that to
130 * reduce the effect of backlights/disk spinning/other variation.
132 ok_samples = spin_samples = avg = 0;
133 for (i = 0; i < POWER_AVG_N; i++) {
134 if (ata_disk_is_active()) {
135 if (!ok_samples) {
136 /* if we don't have any good non-disk-spinning samples,
137 * we take a sample anyway in case the disk is going
138 * to spin all the time.
140 avg += adc_read(ADC_UNREG_POWER);
141 spin_samples++;
143 } else {
144 if (spin_samples) /* throw away disk-spinning samples */
145 spin_samples = avg = 0;
146 avg += adc_read(ADC_UNREG_POWER);
147 ok_samples++;
149 sleep(HZ*POWER_AVG_SLEEP);
151 avg = avg / ((ok_samples) ? ok_samples : spin_samples);
153 /* rotate the power history */
154 for (i = 0; i < POWER_HISTORY_LEN-1; i++)
155 power_history[i] = power_history[i+1];
157 /* insert new value in the end, in centivolts 8-) */
158 power_history[POWER_HISTORY_LEN-1] = (avg * BATTERY_SCALE_FACTOR) / 10000;
160 #ifdef HAVE_CHARGE_CTRL
161 if (charger_inserted()) {
162 if (charger_enabled) {
163 /* charger inserted and enabled */
164 charged_time++;
165 if (charged_time > CHARGE_MAX_TIME) {
166 DEBUGF("power: charged_time > CHARGE_MAX_TIME, enough!\n");
167 /* have charged too long and deltaV detection did not work! */
168 charger_enable(false);
169 snprintf(power_message, POWER_MESSAGE_LEN, "Chg tmout %d min", CHARGE_MAX_TIME);
170 /* Perhaps we should disable charging for several
171 hours from this point, just to be sure. */
172 } else {
173 if (charged_time > CHARGE_MIN_TIME) {
174 /* have charged continuously over the minimum charging time,
175 * so we monitor for deltaV going negative. Multiply things
176 * by 100 to get more accuracy without floating point arithmetic.
177 * power_history[] contains centivolts so after multiplying by 100
178 * the deltas are in tenths of millivolts (delta of 5 is
179 * 0.0005 V).
181 delta = ( power_history[POWER_HISTORY_LEN-1] * 100
182 + power_history[POWER_HISTORY_LEN-2] * 100
183 - power_history[POWER_HISTORY_LEN-1-CHARGE_END_NEGD+1] * 100
184 - power_history[POWER_HISTORY_LEN-1-CHARGE_END_NEGD] * 100 )
185 / CHARGE_END_NEGD / 2;
187 if (delta < -100) { /* delta < -10 mV */
188 DEBUGF("power: short-term negative delta, enough!\n");
189 charger_enable(false);
190 snprintf(power_message, POWER_MESSAGE_LEN, "end negd %d %dmin", delta, charged_time);
191 } else {
192 /* if we didn't disable the charger in the previous test, check for low positive delta */
193 delta = ( power_history[POWER_HISTORY_LEN-1] * 100
194 + power_history[POWER_HISTORY_LEN-2] * 100
195 - power_history[POWER_HISTORY_LEN-1-CHARGE_END_ZEROD+1] * 100
196 - power_history[POWER_HISTORY_LEN-1-CHARGE_END_ZEROD] * 100 )
197 / CHARGE_END_ZEROD / 2;
199 if (delta < 1) { /* delta < 0.1 mV */
200 DEBUGF("power: long-term small positive delta, enough!\n");
201 charger_enable(false);
202 snprintf(power_message, POWER_MESSAGE_LEN, "end lowd %d %dmin", delta, charged_time);
207 } else {
208 /* charged inserted but not enabled */
209 /* if battery is not full, enable charging */
210 if (battery_level() < charge_restart_level) {
211 DEBUGF("power: charger inserted and battery not full, enabling\n");
212 charger_enable(true);
213 charged_time = 0;
214 snprintf(power_message, POWER_MESSAGE_LEN, "Chg started at %d%%", battery_level());
217 } else {
218 /* charger not inserted */
219 if (charger_enabled) {
220 /* charger not inserted but was enabled */
221 DEBUGF("power: charger disconnected, disabling\n");
222 charger_enable(false);
223 snprintf(power_message, POWER_MESSAGE_LEN, "Charger disc");
225 /* charger not inserted and disabled, so we're discharging */
227 #endif /* HAVE_CHARGE_CTRL*/
229 /* sleep for roughly a minute */
230 sleep(HZ*(60 - POWER_AVG_N * POWER_AVG_SLEEP));
234 void power_init(void)
236 /* init history to 0 */
237 memset(power_history, 0x00, sizeof(power_history));
238 /* initialize the history with a single sample to prevent level
239 flickering during the first minute of execution */
240 power_history[POWER_HISTORY_LEN-1] = (adc_read(ADC_UNREG_POWER) * BATTERY_SCALE_FACTOR) / 10000;
242 #ifdef HAVE_CHARGE_CTRL
243 snprintf(power_message, POWER_MESSAGE_LEN, "Powermgmt started");
244 #endif
245 create_thread(power_thread, power_stack, sizeof(power_stack), power_thread_name);
248 #endif /* SIMULATOR */