summaryrefslogtreecommitdiff
path: root/firmware/powermgmt.c
diff options
context:
space:
mode:
authorBrandon Low <lostlogic@rockbox.org>2006-01-19 07:47:34 +0000
committerBrandon Low <lostlogic@rockbox.org>2006-01-19 07:47:34 +0000
commitcdacab3d89cdbbd8fd7c20b3b340e27352e6737e (patch)
treead1746cf51bdb56a1cdcabf0c38eb6a53244c431 /firmware/powermgmt.c
parent13414ae77a77d96ef2cb210d644880b847c9ca6f (diff)
downloadrockbox-cdacab3d89cdbbd8fd7c20b3b340e27352e6737e.tar.gz
rockbox-cdacab3d89cdbbd8fd7c20b3b340e27352e6737e.zip
Charging state reporting on H3x0
Don't read the GPIO every refresh of statusbar or gwps for plugged state, use a variable exported from powermgmt instead. Allow Custom WPS to access %bc for battery charge state on targets that support this. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@8383 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/powermgmt.c')
-rw-r--r--firmware/powermgmt.c53
1 files changed, 34 insertions, 19 deletions
diff --git a/firmware/powermgmt.c b/firmware/powermgmt.c
index 4b3999751c..7bf2d58284 100644
--- a/firmware/powermgmt.c
+++ b/firmware/powermgmt.c
@@ -145,33 +145,28 @@ static const short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] =
145}; 145};
146 146
147#ifdef HAVE_CHARGING 147#ifdef HAVE_CHARGING
148charger_input_state_type charger_input_state IDATA_ATTR;
149
148/* voltages (centivolt) of 0%, 10%, ... 100% when charging enabled */ 150/* voltages (centivolt) of 0%, 10%, ... 100% when charging enabled */
149static const short percent_to_volt_charge[11] = 151static const short percent_to_volt_charge[11] =
150{ 152{
153#if CONFIG_BATTERY == BATT_LIPOL1300
154 340, 349, 358, 367, 376, 385, 394, 403, 408, 413, 418 /* Estimated */
155#else
151 /* values guessed, see 156 /* values guessed, see
152 http://www.seattlerobotics.org/encoder/200210/LiIon2.pdf until someone 157 http://www.seattlerobotics.org/encoder/200210/LiIon2.pdf until someone
153 measures voltages over a charging cycle */ 158 measures voltages over a charging cycle */
154 476, 544, 551, 556, 561, 564, 566, 576, 582, 584, 585 /* NiMH */ 159 476, 544, 551, 556, 561, 564, 566, 576, 582, 584, 585 /* NiMH */
160#endif
155}; 161};
156#endif /* HAVE_CHARGING */ 162#endif /* HAVE_CHARGING */
157 163
158#if defined(HAVE_CHARGE_CTRL) || CONFIG_BATTERY == BATT_LIION2200 164#if defined(HAVE_CHARGE_CTRL) || \
165 CONFIG_BATTERY == BATT_LIION2200 || \
166 defined(HAVE_CHARGE_STATE)
159charge_state_type charge_state; /* charging mode */ 167charge_state_type charge_state; /* charging mode */
160#endif 168#endif
161 169
162#ifdef HAVE_CHARGING
163/*
164 * Flag that the charger has been plugged in/removed: this is set for exactly
165 * one time through the power loop when the charger has been plugged in.
166 */
167static enum {
168 NO_CHARGER,
169 CHARGER_UNPLUGGED, /* transient state */
170 CHARGER_PLUGGED, /* transient state */
171 CHARGER
172} charger_input_state;
173#endif
174
175#ifdef HAVE_CHARGE_CTRL 170#ifdef HAVE_CHARGE_CTRL
176int long_delta; /* long term delta battery voltage */ 171int long_delta; /* long term delta battery voltage */
177int short_delta; /* short term delta battery voltage */ 172int short_delta; /* short term delta battery voltage */
@@ -317,7 +312,7 @@ static void battery_status_update(void)
317{ 312{
318 int level; 313 int level;
319 314
320#ifdef HAVE_CHARGE_CTRL 315#if defined(HAVE_CHARGE_CTRL) || defined(HAVE_CHARGE_STATE)
321 if (charge_state == DISCHARGING) { 316 if (charge_state == DISCHARGING) {
322 level = voltage_to_percent(battery_centivolts, 317 level = voltage_to_percent(battery_centivolts,
323 percent_to_volt_discharge[battery_type]); 318 percent_to_volt_discharge[battery_type]);
@@ -520,6 +515,23 @@ static void power_thread_sleep(int ticks)
520 } 515 }
521 } 516 }
522#endif 517#endif
518#ifdef HAVE_CHARGE_STATE
519 switch (charger_input_state) {
520 case CHARGER_UNPLUGGED:
521 charge_state = DISCHARGING;
522 case NO_CHARGER:
523 break;
524 case CHARGER_PLUGGED:
525 case CHARGER:
526 if (charging_state()) {
527 charge_state = CHARGING;
528 } else {
529 charge_state = DISCHARGING;
530 }
531 break;
532 }
533
534#endif /* HAVE_CHARGE_STATE */
523 535
524 small_ticks = MIN(HZ/2, ticks); 536 small_ticks = MIN(HZ/2, ticks);
525 sleep(small_ticks); 537 sleep(small_ticks);
@@ -551,6 +563,7 @@ static void power_thread_sleep(int ticks)
551 battery_centivolts = avgbat / BATT_AVE_SAMPLES / 10000; 563 battery_centivolts = avgbat / BATT_AVE_SAMPLES / 10000;
552 564
553 } 565 }
566
554#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL) 567#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL)
555 /* 568 /*
556 * If we have a lot of pending writes or if the disk is spining, 569 * If we have a lot of pending writes or if the disk is spining,
@@ -821,7 +834,7 @@ static void power_thread(void)
821 snprintf(power_message, POWER_MESSAGE_LEN, "Charger: discharge"); 834 snprintf(power_message, POWER_MESSAGE_LEN, "Charger: discharge");
822 } 835 }
823 836
824#endif /* HAVE_CHARGE_CTRL*/ 837#endif /* end HAVE_CHARGE_CTRL */
825 838
826 /* sleep for a minute */ 839 /* sleep for a minute */
827 840
@@ -840,7 +853,7 @@ static void power_thread(void)
840#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL) 853#if defined(DEBUG_FILE) && defined(HAVE_CHARGE_CTRL)
841 if(usb_inserted()) { 854 if(usb_inserted()) {
842 if(fd >= 0) { 855 if(fd >= 0) {
843 /* It is probably too late to close the file but we can try... */ 856 /* It is probably too late to close the file but we can try...*/
844 close(fd); 857 close(fd);
845 fd = -1; 858 fd = -1;
846 } 859 }
@@ -855,9 +868,11 @@ static void power_thread(void)
855 } 868 }
856 } 869 }
857 if(fd >= 0) { 870 if(fd >= 0) {
858 snprintf(debug_message, DEBUG_MESSAGE_LEN, "%d, %d, %d, %d, %d, %d, %d, %d\n", 871 snprintf(debug_message, DEBUG_MESSAGE_LEN,
872 "%d, %d, %d, %d, %d, %d, %d, %d\n",
859 powermgmt_last_cycle_startstop_min, battery_centivolts, 873 powermgmt_last_cycle_startstop_min, battery_centivolts,
860 battery_percent, charger_input_state, charge_state, pid_p, pid_i, trickle_sec); 874 battery_percent, charger_input_state, charge_state,
875 pid_p, pid_i, trickle_sec);
861 write(fd, debug_message, strlen(debug_message)); 876 write(fd, debug_message, strlen(debug_message));
862 wrcount++; 877 wrcount++;
863 } 878 }