1 /***************************************************************************
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
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 ****************************************************************************/
30 #include "powermgmt.h"
34 int battery_level(void)
39 bool battery_level_safe(void)
44 #else /* not SIMULATOR */
46 static char power_stack
[DEFAULT_STACK_SIZE
];
47 static char power_thread_name
[] = "power";
49 unsigned short power_history
[POWER_HISTORY_LEN
];
50 #ifdef HAVE_CHARGE_CTRL
51 char power_message
[POWER_MESSAGE_LEN
] = "";
52 char charge_restart_level
= CHARGE_RESTART_HI
;
55 /* Returns battery level in percent */
56 int battery_level(void)
62 /* calculate average over last 3 minutes (skip empty samples) */
63 for (i
= 0; i
< 3; i
++)
64 if (power_history
[POWER_HISTORY_LEN
-1-i
]) {
65 level
+= power_history
[POWER_HISTORY_LEN
-1-i
];
70 level
= level
/ c
; /* avg */
71 else /* history was empty, get a fresh sample */
72 level
= (adc_read(ADC_UNREG_POWER
) * BATTERY_SCALE_FACTOR
) / 10000;
74 #ifdef HAVE_CHARGE_CTRL
76 level
-= 10; /* the charger raises the voltage 0.05-0.2v */
79 if(level
> BATTERY_LEVEL_FULL
)
80 level
= BATTERY_LEVEL_FULL
;
82 if(level
< BATTERY_LEVEL_EMPTY
)
83 level
= BATTERY_LEVEL_EMPTY
;
85 return ((level
-BATTERY_LEVEL_EMPTY
) * 100) / BATTERY_RANGE
;
88 /* Tells if the battery level is safe for disk writes */
89 bool battery_level_safe(void)
91 /* I'm pretty sure we don't want an average over a long time here */
92 return adc_read(ADC_UNREG_POWER
) > (BATTERY_LEVEL_DANGEROUS
* 10000) / BATTERY_SCALE_FACTOR
;
96 * This power thread maintains a history of battery voltage
97 * and should, in the future, enable charging when it's needed
98 * and power is available, and disable it when the battery is full.
99 * Battery 'fullness' can be determined by the voltage drop, see:
101 * http://www.nimhbattery.com/nimhbattery-faq.htm questions 3 & 4
102 * http://www.powerpacks-uk.com/Charging%20NiMh%20Batteries.htm
103 * http://www.angelfire.com/electronic/hayles/charge1.html (soft start idea)
104 * http://www.powerstream.com/NiMH.htm (discouraging)
105 * http://www.panasonic.com/industrial/battery/oem/images/pdf/nimhchar.pdf
106 * http://www.duracell.com/oem/Pdf/others/nimh_5.pdf (discharging)
107 * http://www.duracell.com/oem/Pdf/others/nimh_6.pdf (charging)
109 * Charging logic which we're starting with (by Linus, Hes, Bagder):
111 * 1) max 16 hrs charge time (just in negative delta detection fails)
112 * 2) Stop at negative delta of 5 mins
113 * 3) Stop at 15 mins of zero-delta or below
114 * 4) minimum of 15 mins charge time before 2) is applied
115 * 5) after end of charging, wait for charge go down 80%
116 * before charging again if in 'no-use overnight charging mode'
117 * and down to 10% if in 'fixed-location mains-powered usage mode'
120 static void power_thread(void)
124 #ifdef HAVE_CHARGE_CTRL
126 int charged_time
= 0;
131 DEBUGF("power_thread woke up\n");
132 /* make POWER_AVG measurements and calculate an average of that to
133 * reduce the effect of backlights/disk spinning/other variation
136 for (i
= 0; i
< POWER_AVG
; i
++) {
137 avg
+= adc_read(ADC_UNREG_POWER
);
140 avg
= avg
/ POWER_AVG
;
142 /* rotate the power history */
143 for (i
= 0; i
< POWER_HISTORY_LEN
-1; i
++)
144 power_history
[i
] = power_history
[i
+1];
146 /* insert new value in the end, in decivolts 8-) */
147 power_history
[POWER_HISTORY_LEN
-1] = (avg
* BATTERY_SCALE_FACTOR
) / 10000;
149 #ifdef HAVE_CHARGE_CTRL
150 if (charger_inserted()) {
151 if (charger_enabled
) {
152 /* charger inserted and enabled */
154 if (charged_time
> CHARGE_MAX_TIME
) {
155 DEBUGF("power: charged_time > CHARGE_MAX_TIME, enough!\n");
156 /* have charged too long and deltaV detection did not work! */
157 charger_enable(false);
158 snprintf(power_message
, POWER_MESSAGE_LEN
, "Chg tmout %d min", CHARGE_MAX_TIME
);
159 /* Perhaps we should disable charging for several
160 hours from this point, just to be sure. */
162 if (charged_time
> CHARGE_MIN_TIME
) {
163 /* have charged continuously over the minimum charging time,
164 * so we monitor for deltaV going negative. Multiply things
165 * by 100 to get more accuracy without floating point arithmetic.
166 * power_history[] contains decivolts.
169 for (i
= 0; i
< CHARGE_END_NEGD
; i
++)
170 delta
+= power_history
[POWER_HISTORY_LEN
-1-i
]*100 - power_history
[POWER_HISTORY_LEN
-1-i
-1]*100;
171 delta
= delta
/ CHARGE_END_NEGD
;
173 if (delta
< -50) { /* delta < -0.3 V */
174 DEBUGF("power: short-term negative delta, enough!\n");
175 charger_enable(false);
176 snprintf(power_message
, POWER_MESSAGE_LEN
, "end negd %d %dmin", delta
, charged_time
);
178 /* if we didn't disable the charger in the previous test, check for low positive delta */
180 for (i
= 0; i
< CHARGE_END_ZEROD
; i
++)
181 delta
+= power_history
[POWER_HISTORY_LEN
-1-i
]*100 - power_history
[POWER_HISTORY_LEN
-1-i
-1]*100;
182 delta
= delta
/ CHARGE_END_ZEROD
;
184 if (delta
<= 5) { /* delta of <= 0.005 V */
185 DEBUGF("power: long-term small positive delta, enough!\n");
186 charger_enable(false);
187 snprintf(power_message
, POWER_MESSAGE_LEN
, "end lowd %d %dmin", delta
, charged_time
);
193 /* charged inserted but not enabled */
194 /* if battery is not full, enable charging */
195 if (battery_level() < charge_restart_level
) {
196 DEBUGF("power: charger inserted and battery not full, enabling\n");
197 charger_enable(true);
199 snprintf(power_message
, POWER_MESSAGE_LEN
, "Chg started at %d%%", battery_level());
203 /* charger not inserted */
204 if (charger_enabled
) {
205 /* charger not inserted but was enabled */
206 DEBUGF("power: charger disconnected, disabling\n");
207 charger_enable(false);
208 snprintf(power_message
, POWER_MESSAGE_LEN
, "Charger disc");
210 /* charger not inserted and disabled, so we're discharging */
212 #endif /* HAVE_CHARGE_CTRL*/
214 /* sleep for roughly a minute */
215 sleep(HZ
*(60-POWER_AVG
*15));
219 void power_init(void)
221 /* init history to 0 */
222 memset(power_history
, 0x00, sizeof(power_history
));
224 #ifdef HAVE_CHARGE_CTRL
225 snprintf(power_message
, POWER_MESSAGE_LEN
, "Powermgmt started");
227 create_thread(power_thread
, power_stack
, sizeof(power_stack
), power_thread_name
);
230 #endif /* SIMULATOR */