diff options
-rw-r--r-- | firmware/config-recorder.h | 4 | ||||
-rw-r--r-- | firmware/drivers/adc.h | 2 | ||||
-rw-r--r-- | firmware/drivers/power.c | 15 | ||||
-rw-r--r-- | firmware/drivers/power.h | 4 | ||||
-rw-r--r-- | firmware/powermgmt.c | 177 | ||||
-rw-r--r-- | firmware/powermgmt.h | 49 |
6 files changed, 247 insertions, 4 deletions
diff --git a/firmware/config-recorder.h b/firmware/config-recorder.h index a365214451..9bcfc7bc60 100644 --- a/firmware/config-recorder.h +++ b/firmware/config-recorder.h | |||
@@ -12,3 +12,7 @@ | |||
12 | 12 | ||
13 | /* Define this if you have a MAS3587F */ | 13 | /* Define this if you have a MAS3587F */ |
14 | #define HAVE_MAS3587F | 14 | #define HAVE_MAS3587F |
15 | |||
16 | /* Define this if you have charging control */ | ||
17 | #define HAVE_CHARGE_CTRL | ||
18 | |||
diff --git a/firmware/drivers/adc.h b/firmware/drivers/adc.h index 1ff5118238..0c05f4d0f8 100644 --- a/firmware/drivers/adc.h +++ b/firmware/drivers/adc.h | |||
@@ -40,6 +40,8 @@ | |||
40 | #define BATTERY_SCALE_FACTOR 6546 | 40 | #define BATTERY_SCALE_FACTOR 6546 |
41 | #endif | 41 | #endif |
42 | 42 | ||
43 | #define EXT_SCALE_FACTOR 14800 | ||
44 | |||
43 | unsigned short adc_read(int channel); | 45 | unsigned short adc_read(int channel); |
44 | void adc_init(void); | 46 | void adc_init(void); |
45 | 47 | ||
diff --git a/firmware/drivers/power.c b/firmware/drivers/power.c index d3ce939ce2..559509fa67 100644 --- a/firmware/drivers/power.c +++ b/firmware/drivers/power.c | |||
@@ -22,12 +22,16 @@ | |||
22 | #include "adc.h" | 22 | #include "adc.h" |
23 | #include "power.h" | 23 | #include "power.h" |
24 | 24 | ||
25 | #ifdef HAVE_CHARGE_CTRL | ||
26 | bool charger_enabled = 0; | ||
27 | #endif | ||
28 | |||
25 | #ifndef SIMULATOR | 29 | #ifndef SIMULATOR |
26 | 30 | ||
27 | bool charger_inserted(void) | 31 | bool charger_inserted(void) |
28 | { | 32 | { |
29 | #ifdef ARCHOS_RECORDER | 33 | #ifdef ARCHOS_RECORDER |
30 | return adc_read(ADC_EXT_POWER) > 0x200; | 34 | return adc_read(ADC_EXT_POWER) > 0x100; |
31 | #else | 35 | #else |
32 | return (PADR & 1) == 0; | 36 | return (PADR & 1) == 0; |
33 | #endif | 37 | #endif |
@@ -58,11 +62,14 @@ bool battery_level_safe(void) | |||
58 | 62 | ||
59 | void charger_enable(bool on) | 63 | void charger_enable(bool on) |
60 | { | 64 | { |
61 | #ifdef ARCHOS_RECORDER | 65 | #ifdef HAVE_CHARGE_CTRL |
62 | if(on) | 66 | if(on) { |
63 | PBDR &= ~0x20; | 67 | PBDR &= ~0x20; |
64 | else | 68 | charger_enabled = 1; |
69 | } else { | ||
65 | PBDR |= 0x20; | 70 | PBDR |= 0x20; |
71 | charger_enabled = 0; | ||
72 | } | ||
66 | #else | 73 | #else |
67 | on = on; | 74 | on = on; |
68 | #endif | 75 | #endif |
diff --git a/firmware/drivers/power.h b/firmware/drivers/power.h index f921b67d5e..fe7d443bef 100644 --- a/firmware/drivers/power.h +++ b/firmware/drivers/power.h | |||
@@ -26,6 +26,10 @@ | |||
26 | 26 | ||
27 | #define BATTERY_RANGE (BATTERY_LEVEL_FULL - BATTERY_LEVEL_EMPTY) | 27 | #define BATTERY_RANGE (BATTERY_LEVEL_FULL - BATTERY_LEVEL_EMPTY) |
28 | 28 | ||
29 | #ifdef HAVE_CHARGE_CTRL | ||
30 | extern bool charger_enabled; | ||
31 | #endif | ||
32 | |||
29 | bool charger_inserted(void); | 33 | bool charger_inserted(void); |
30 | void charger_enable(bool on); | 34 | void charger_enable(bool on); |
31 | void ide_power_enable(bool on); | 35 | void ide_power_enable(bool on); |
diff --git a/firmware/powermgmt.c b/firmware/powermgmt.c new file mode 100644 index 0000000000..940d37ea16 --- /dev/null +++ b/firmware/powermgmt.c | |||
@@ -0,0 +1,177 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 by Heikki Hannikainen | ||
11 | * | ||
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. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
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 "power.h" | ||
29 | #include "powermgmt.h" | ||
30 | |||
31 | #ifndef SIMULATOR | ||
32 | |||
33 | static char power_stack[DEFAULT_STACK_SIZE]; | ||
34 | static char power_thread_name[] = "power"; | ||
35 | |||
36 | unsigned short power_history[POWER_HISTORY_LEN]; | ||
37 | #ifdef HAVE_CHARGE_CTRL | ||
38 | char power_message[POWER_MESSAGE_LEN] = ""; | ||
39 | char charge_restart_level = CHARGE_RESTART_HI; | ||
40 | #endif | ||
41 | |||
42 | /* | ||
43 | * This power thread maintains a history of battery voltage | ||
44 | * and should, in the future, enable charging when it's needed | ||
45 | * and power is available, and disable it when the battery is full. | ||
46 | * Battery 'fullness' can be determined by the voltage drop, see: | ||
47 | * | ||
48 | * http://www.nimhbattery.com/nimhbattery-faq.htm questions 3 & 4 | ||
49 | * http://www.powerpacks-uk.com/Charging%20NiMh%20Batteries.htm | ||
50 | * http://www.angelfire.com/electronic/hayles/charge1.html (soft start idea) | ||
51 | * http://www.powerstream.com/NiMH.htm (discouraging) | ||
52 | * http://www.panasonic.com/industrial/battery/oem/images/pdf/nimhchar.pdf | ||
53 | * http://www.duracell.com/oem/Pdf/others/nimh_5.pdf (discharging) | ||
54 | * http://www.duracell.com/oem/Pdf/others/nimh_6.pdf (charging) | ||
55 | * | ||
56 | * Charging logic which we're starting with (by Linus, Hes, Bagder): | ||
57 | * | ||
58 | * 1) max 16 hrs charge time (just in negative delta detection fails) | ||
59 | * 2) Stop at negative delta of 5 mins | ||
60 | * 3) Stop at 15 mins of zero-delta or below | ||
61 | * 4) minimum of 15 mins charge time before 2) is applied | ||
62 | * 5) after end of charging, wait for charge go down 80% | ||
63 | * before charging again if in 'no-use overnight charging mode' | ||
64 | * and down to 10% if in 'fixed-location mains-powered usage mode' | ||
65 | */ | ||
66 | |||
67 | static void power_thread(void) | ||
68 | { | ||
69 | int i; | ||
70 | int avg; | ||
71 | #ifdef HAVE_CHARGE_CTRL | ||
72 | int delta; | ||
73 | int charged_time = 0; | ||
74 | #endif | ||
75 | |||
76 | while (1) | ||
77 | { | ||
78 | DEBUGF("power_thread woke up\n"); | ||
79 | /* make POWER_AVG measurements and calculate an average of that to | ||
80 | * reduce the effect of backlights/disk spinning/other variation | ||
81 | */ | ||
82 | avg = 0; | ||
83 | for (i = 0; i < POWER_AVG; i++) { | ||
84 | avg += adc_read(ADC_UNREG_POWER); | ||
85 | sleep(15); | ||
86 | } | ||
87 | avg = avg / POWER_AVG; | ||
88 | |||
89 | /* rotate the power history */ | ||
90 | for (i = 0; i < POWER_HISTORY_LEN-1; i++) | ||
91 | power_history[i] = power_history[i+1]; | ||
92 | |||
93 | /* insert new value in the end, in decivolts 8-) */ | ||
94 | power_history[POWER_HISTORY_LEN-1] = (avg * BATTERY_SCALE_FACTOR) / 10000; | ||
95 | |||
96 | #ifdef HAVE_CHARGE_CTRL | ||
97 | if (charger_inserted()) { | ||
98 | if (charger_enabled) { | ||
99 | /* charger inserted and enabled */ | ||
100 | charged_time++; | ||
101 | if (charged_time > CHARGE_MAX_TIME) { | ||
102 | DEBUGF("power: charged_time > CHARGE_MAX_TIME, enough!\n"); | ||
103 | /* have charged too long and deltaV detection did not work! */ | ||
104 | charger_enable(false); | ||
105 | snprintf(power_message, POWER_MESSAGE_LEN, "Chg tmout %d min", CHARGE_MAX_TIME); | ||
106 | /* Perhaps we should disable charging for several | ||
107 | hours from this point, just to be sure. */ | ||
108 | } else { | ||
109 | if (charged_time > CHARGE_MIN_TIME) { | ||
110 | /* have charged continuously over the minimum charging time, | ||
111 | * so we monitor for deltaV going negative. Multiply things | ||
112 | * by 100 to get more accuracy without floating point arithmetic. | ||
113 | * power_history[] contains decivolts. | ||
114 | */ | ||
115 | delta = 0; | ||
116 | for (i = 0; i < CHARGE_END_NEGD; i++) | ||
117 | delta += power_history[POWER_HISTORY_LEN-1-i]*100 - power_history[POWER_HISTORY_LEN-1-i-1]*100; | ||
118 | delta = delta / CHARGE_END_NEGD; | ||
119 | |||
120 | if (delta < -50) { /* delta < -0.3 V */ | ||
121 | DEBUGF("power: short-term negative delta, enough!\n"); | ||
122 | charger_enable(false); | ||
123 | snprintf(power_message, POWER_MESSAGE_LEN, "end negd %d %dmin", delta, charged_time); | ||
124 | } else { | ||
125 | /* if we didn't disable the charger in the previous test, check for low positive delta */ | ||
126 | delta = 0; | ||
127 | for (i = 0; i < CHARGE_END_ZEROD; i++) | ||
128 | delta += power_history[POWER_HISTORY_LEN-1-i]*100 - power_history[POWER_HISTORY_LEN-1-i-1]*100; | ||
129 | delta = delta / CHARGE_END_ZEROD; | ||
130 | |||
131 | if (delta <= 5) { /* delta of <= 0.005 V */ | ||
132 | DEBUGF("power: long-term small positive delta, enough!\n"); | ||
133 | charger_enable(false); | ||
134 | snprintf(power_message, POWER_MESSAGE_LEN, "end lowd %d %dmin", delta, charged_time); | ||
135 | } | ||
136 | } | ||
137 | } | ||
138 | } | ||
139 | } else { | ||
140 | /* charged inserted but not enabled */ | ||
141 | /* if battery is not full, enable charging */ | ||
142 | if (battery_level() < charge_restart_level) { | ||
143 | DEBUGF("power: charger inserted and battery not full, enabling\n"); | ||
144 | charger_enable(true); | ||
145 | charged_time = 0; | ||
146 | snprintf(power_message, POWER_MESSAGE_LEN, "Chg started at %d%%", battery_level()); | ||
147 | } | ||
148 | } | ||
149 | } else { | ||
150 | /* charger not inserted */ | ||
151 | if (charger_enabled) { | ||
152 | /* charger not inserted but was enabled */ | ||
153 | DEBUGF("power: charger disconnected, disabling\n"); | ||
154 | charger_enable(false); | ||
155 | snprintf(power_message, POWER_MESSAGE_LEN, "Charger disc"); | ||
156 | } | ||
157 | /* charger not inserted and disabled, so we're discharging */ | ||
158 | } | ||
159 | #endif /* HAVE_CHARGE_CTRL*/ | ||
160 | |||
161 | /* sleep for roughly a minute */ | ||
162 | sleep(HZ*(60-POWER_AVG*15)); | ||
163 | } | ||
164 | } | ||
165 | |||
166 | void power_init(void) | ||
167 | { | ||
168 | /* init history to 0 */ | ||
169 | memset(power_history, 0x00, sizeof(power_history)); | ||
170 | |||
171 | #ifdef HAVE_CHARGE_CTRL | ||
172 | snprintf(power_message, POWER_MESSAGE_LEN, "Powermgmt started"); | ||
173 | #endif | ||
174 | create_thread(power_thread, power_stack, sizeof(power_stack), power_thread_name); | ||
175 | } | ||
176 | |||
177 | #endif | ||
diff --git a/firmware/powermgmt.h b/firmware/powermgmt.h new file mode 100644 index 0000000000..068fd5d4b5 --- /dev/null +++ b/firmware/powermgmt.h | |||
@@ -0,0 +1,49 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 by Heikki Hannikainen | ||
11 | * | ||
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. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | #ifndef _POWERMGMT_H_ | ||
20 | #define _POWERMGMT_H_ | ||
21 | |||
22 | #ifndef SIMULATOR | ||
23 | |||
24 | #define POWER_HISTORY_LEN 2*60 /* 2 hours of samples, one per minute */ | ||
25 | #define POWER_AVG 3 /* how many samples to take for each measurement */ | ||
26 | |||
27 | #define CHARGE_END_NEGD 6 /* stop when N minutes have passed with | ||
28 | * avg delta being < -0.3 V */ | ||
29 | #define CHARGE_END_ZEROD 30 /* stop when N minutes have passed with | ||
30 | * avg delta being < 0.005 V */ | ||
31 | |||
32 | #ifdef HAVE_CHARGE_CTRL | ||
33 | #define POWER_MESSAGE_LEN 32 /* power thread status message */ | ||
34 | #define CHARGE_MAX_TIME 16*60 /* minutes: maximum charging time */ | ||
35 | #define CHARGE_MIN_TIME 10 /* minutes: minimum charging time */ | ||
36 | #define CHARGE_RESTART_HI 90 /* %: when to restart charging in 'charge' mode */ | ||
37 | #define CHARGE_RESTART_LO 10 /* %: when to restart charging in 'discharge' mode */ | ||
38 | |||
39 | extern char power_message[POWER_MESSAGE_LEN]; | ||
40 | extern char charge_restart_level; | ||
41 | #endif /* HAVE_CHARGE_CTRL */ | ||
42 | |||
43 | extern unsigned short power_history[POWER_HISTORY_LEN]; | ||
44 | |||
45 | void power_init(void); | ||
46 | |||
47 | #endif | ||
48 | |||
49 | #endif | ||