summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--apps/gui/gwps-common.c18
-rw-r--r--apps/gui/statusbar.c18
-rw-r--r--firmware/drivers/power.c6
-rw-r--r--firmware/export/config-h300.h3
-rw-r--r--firmware/export/power.h1
-rw-r--r--firmware/export/powermgmt.h30
-rw-r--r--firmware/powermgmt.c53
7 files changed, 100 insertions, 29 deletions
diff --git a/apps/gui/gwps-common.c b/apps/gui/gwps-common.c
index 3af52b5396..37f4fea310 100644
--- a/apps/gui/gwps-common.c
+++ b/apps/gui/gwps-common.c
@@ -655,11 +655,27 @@ static char* get_tag(struct wps_data* wps_data,
655 655
656 case 'p': /* External power plugged in? */ 656 case 'p': /* External power plugged in? */
657 { 657 {
658 if(charger_inserted()) 658 if(charger_input_state==CHARGER)
659 return "p"; 659 return "p";
660 else 660 else
661 return NULL; 661 return NULL;
662 } 662 }
663#if defined(HAVE_CHARGE_CTRL) || \
664 defined (HAVE_CHARGE_STATE) || \
665 CONFIG_BATTERY == BATT_LIION2200
666 case 'c': /* Charging */
667 {
668 if (charge_state == CHARGING
669#ifdef HAVE_CHARGE_CTRL
670 || charge_state == TOPOFF
671#endif
672 ) {
673 return "c";
674 } else {
675 return NULL;
676 }
677 }
678#endif
663 } 679 }
664 break; 680 break;
665 681
diff --git a/apps/gui/statusbar.c b/apps/gui/statusbar.c
index 80de707a72..b4070394f9 100644
--- a/apps/gui/statusbar.c
+++ b/apps/gui/statusbar.c
@@ -127,7 +127,7 @@ void gui_statusbar_draw(struct gui_statusbar * bar, bool force_redraw)
127#endif /* HAVE_LCD_CHARCELLS */ 127#endif /* HAVE_LCD_CHARCELLS */
128 128
129 bar->info.volume = sound_val2phys(SOUND_VOLUME, global_settings.volume); 129 bar->info.volume = sound_val2phys(SOUND_VOLUME, global_settings.volume);
130 bar->info.inserted = charger_inserted(); 130 bar->info.inserted = (charger_input_state == CHARGER);
131 bar->info.battlevel = battery_level(); 131 bar->info.battlevel = battery_level();
132 bar->info.battery_safe = battery_level_safe(); 132 bar->info.battery_safe = battery_level_safe();
133 133
@@ -178,16 +178,21 @@ void gui_statusbar_draw(struct gui_statusbar * bar, bool force_redraw)
178#ifdef HAVE_CHARGING 178#ifdef HAVE_CHARGING
179 if (bar->info.inserted) { 179 if (bar->info.inserted) {
180 battery_state = true; 180 battery_state = true;
181#if defined(HAVE_CHARGE_CTRL) || CONFIG_BATTERY == BATT_LIION2200 181#if defined(HAVE_CHARGE_CTRL) || \
182 defined(HAVE_CHARGE_STATE) || \
183 CONFIG_BATTERY == BATT_LIION2200
182 /* zero battery run time if charging */ 184 /* zero battery run time if charging */
183 if (charge_state > 0) { 185 if (charge_state > DISCHARGING) {
184 global_settings.runtime = 0; 186 global_settings.runtime = 0;
185 lasttime = current_tick; 187 lasttime = current_tick;
186 } 188 }
187 189
188 /* animate battery if charging */ 190 /* animate battery if charging */
189 if ((charge_state == 1) || 191 if ((charge_state == CHARGING)
190 (charge_state == 2)) { 192#ifdef HAVE_CHARGE_CTRL
193 || (charge_state == TOPOFF)
194#endif
195 ) {
191#else 196#else
192 global_settings.runtime = 0; 197 global_settings.runtime = 0;
193 lasttime = current_tick; 198 lasttime = current_tick;
@@ -323,7 +328,8 @@ void gui_statusbar_icon_battery(struct screen * display, int percent)
323 if (fill > 100) 328 if (fill > 100)
324 fill = 100; 329 fill = 100;
325 330
326#if defined(HAVE_CHARGE_CTRL) && !defined(SIMULATOR) /* Rec v1 target only */ 331#if (defined(HAVE_CHARGE_CTRL) || defined(HAVE_CHARGE_STATE)) && \
332 !defined(SIMULATOR) /* Certain charge controlled targets */
327 /* show graphical animation when charging instead of numbers */ 333 /* show graphical animation when charging instead of numbers */
328 if ((global_settings.battery_display) && 334 if ((global_settings.battery_display) &&
329 (charge_state != 1) && 335 (charge_state != 1) &&
diff --git a/firmware/drivers/power.c b/firmware/drivers/power.c
index da97233e57..8a20890228 100644
--- a/firmware/drivers/power.c
+++ b/firmware/drivers/power.c
@@ -148,6 +148,12 @@ void charger_enable(bool on)
148} 148}
149#endif 149#endif
150 150
151#ifdef HAVE_CHARGE_STATE
152bool charging_state(void) {
153 return (GPIO_READ & 0x00800000)?true:false;
154}
155#endif
156
151#ifdef HAVE_SPDIF_POWER 157#ifdef HAVE_SPDIF_POWER
152void spdif_power_enable(bool on) 158void spdif_power_enable(bool on)
153{ 159{
diff --git a/firmware/export/config-h300.h b/firmware/export/config-h300.h
index 806ae0a079..ded20c9129 100644
--- a/firmware/export/config-h300.h
+++ b/firmware/export/config-h300.h
@@ -72,6 +72,9 @@
72/* Define this if the platform can charge batteries */ 72/* Define this if the platform can charge batteries */
73#define HAVE_CHARGING 1 73#define HAVE_CHARGING 1
74 74
75/* For units with a hardware charger that reports charge state */
76#define HAVE_CHARGE_STATE 1
77
75/* define this if the hardware can be powered off while charging */ 78/* define this if the hardware can be powered off while charging */
76#define HAVE_POWEROFF_WHILE_CHARGING 79#define HAVE_POWEROFF_WHILE_CHARGING
77 80
diff --git a/firmware/export/power.h b/firmware/export/power.h
index bf645522ef..9f91e25f15 100644
--- a/firmware/export/power.h
+++ b/firmware/export/power.h
@@ -25,6 +25,7 @@ extern bool charger_enabled;
25 25
26void power_init(void); 26void power_init(void);
27bool charger_inserted(void); 27bool charger_inserted(void);
28bool charging_state(void);
28void charger_enable(bool on); 29void charger_enable(bool on);
29void ide_power_enable(bool on); 30void ide_power_enable(bool on);
30bool ide_powered(void); 31bool ide_powered(void);
diff --git a/firmware/export/powermgmt.h b/firmware/export/powermgmt.h
index 3c4a61fb87..25c18eabad 100644
--- a/firmware/export/powermgmt.h
+++ b/firmware/export/powermgmt.h
@@ -79,18 +79,38 @@ extern int trickle_sec; /* trickle charge: How many seconds per minute
79 79
80#endif /* HAVE_CHARGE_CTRL */ 80#endif /* HAVE_CHARGE_CTRL */
81 81
82#if defined(HAVE_CHARGE_CTRL) || (CONFIG_BATTERY == BATT_LIION2200) 82#if defined(HAVE_CHARGE_CTRL) || \
83 (CONFIG_BATTERY == BATT_LIION2200) || \
84 defined(HAVE_CHARGE_STATE)
83typedef enum { 85typedef enum {
84 DISCHARGING, 86 DISCHARGING,
85 CHARGING, 87 CHARGING,
88#ifdef HAVE_CHARGE_CTRL
86 TOPOFF, 89 TOPOFF,
87 TRICKLE 90 TRICKLE
91#endif
88} charge_state_type; 92} charge_state_type;
89 93
90/* tells what the charger is doing */ 94/* tells what the charger is doing */
91extern charge_state_type charge_state; 95extern charge_state_type charge_state;
92#endif /* defined(HAVE_CHARGE_CTRL) || (CONFIG_BATTERY == BATT_LIION2200) */ 96#endif /* defined(HAVE_CHARGE_CTRL) || (CONFIG_BATTERY == BATT_LIION2200) */
93 97
98#ifdef HAVE_CHARGING
99/*
100 * Flag that the charger has been plugged in/removed: this is set for exactly
101 * one time through the power loop when the charger has been plugged in.
102 */
103typedef enum {
104 NO_CHARGER,
105 CHARGER_UNPLUGGED, /* transient state */
106 CHARGER_PLUGGED, /* transient state */
107 CHARGER
108} charger_input_state_type;
109
110/* tells the state of the charge input */
111extern charger_input_state_type charger_input_state;
112#endif
113
94#ifdef HAVE_MMC /* Values for Ondio */ 114#ifdef HAVE_MMC /* Values for Ondio */
95# define CURRENT_NORMAL 95 /* average, nearly proportional to 1/U */ 115# define CURRENT_NORMAL 95 /* average, nearly proportional to 1/U */
96# define CURRENT_USB 1 /* host powered in USB mode; avoid zero-div */ 116# define CURRENT_USB 1 /* host powered in USB mode; avoid zero-div */
@@ -106,9 +126,13 @@ extern charge_state_type charge_state;
106 126
107# define CURRENT_MIN_CHG 70 /* minimum charge current */ 127# define CURRENT_MIN_CHG 70 /* minimum charge current */
108# define MIN_CHG_V 8500 /* at 8.5v charger voltage get CURRENT_MIN_CHG */ 128# define MIN_CHG_V 8500 /* at 8.5v charger voltage get CURRENT_MIN_CHG */
109# define CURRENT_MAX_CHG 350 /* maximum charging current */ 129# ifdef IRIVER_H300_SERIES
130# define CURRENT_MAX_CHG 650 /* maximum charging current */
131# else
132# define CURRENT_MAX_CHG 350 /* maximum charging current */
133# endif
110# define MAX_CHG_V 10250 /* anything over 10.25v gives CURRENT_MAX_CHG */ 134# define MAX_CHG_V 10250 /* anything over 10.25v gives CURRENT_MAX_CHG */
111#endif /* HAVE_MMC */ 135#endif /* not HAVE_MMC */
112 136
113extern unsigned int bat; /* filtered battery voltage, centivolts */ 137extern unsigned int bat; /* filtered battery voltage, centivolts */
114extern unsigned short power_history[POWER_HISTORY_LEN]; 138extern unsigned short power_history[POWER_HISTORY_LEN];
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 }