diff options
author | Thomas Martitz <kugel@rockbox.org> | 2011-12-31 13:34:56 +0000 |
---|---|---|
committer | Thomas Martitz <kugel@rockbox.org> | 2011-12-31 13:34:56 +0000 |
commit | 05f12e08772d1ca03101b176e329bfd313daf673 (patch) | |
tree | 56ce125d3bd3c2c1d3e5588b2a3ddab1b8a460ab /firmware/target/hosted/ypr0/powermgmt-ypr0.c | |
parent | 07605a659e06efaedb325e9a91214be503188f06 (diff) | |
download | rockbox-05f12e08772d1ca03101b176e329bfd313daf673.tar.gz rockbox-05f12e08772d1ca03101b176e329bfd313daf673.zip |
ypr0: Enable battery voltage read-out, charging monitoring and charger detection.
Voltage can be read using as3543 adc (i.e. ascodec api, on this target implemented
via ioctl()). TODO: Look into possibly controlling charging more by re-using
powermgmt-ascodec.c. However, charging seems to be controlled by the kernel,
so may not be needed.
Charger state can be read using /dev/minivet. It allows to differentiate between
wall charger and usb charging, but that's not implemented (is it even worthwhile?)
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@31470 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/target/hosted/ypr0/powermgmt-ypr0.c')
-rw-r--r-- | firmware/target/hosted/ypr0/powermgmt-ypr0.c | 115 |
1 files changed, 35 insertions, 80 deletions
diff --git a/firmware/target/hosted/ypr0/powermgmt-ypr0.c b/firmware/target/hosted/ypr0/powermgmt-ypr0.c index 5701e9f02f..3a97331083 100644 --- a/firmware/target/hosted/ypr0/powermgmt-ypr0.c +++ b/firmware/target/hosted/ypr0/powermgmt-ypr0.c | |||
@@ -17,117 +17,72 @@ | |||
17 | * | 17 | * |
18 | ****************************************************************************/ | 18 | ****************************************************************************/ |
19 | #include "config.h" | 19 | #include "config.h" |
20 | #include "system.h" | 20 | #include <sys/ioctl.h> |
21 | #include <time.h> | ||
22 | #include "kernel.h" | 21 | #include "kernel.h" |
23 | #include "powermgmt.h" | 22 | #include "powermgmt.h" |
23 | #include "power.h" | ||
24 | #include "file.h" | ||
24 | #include "ascodec-target.h" | 25 | #include "ascodec-target.h" |
25 | #include "stdio.h" | 26 | #include "as3514.h" |
27 | #include "sc900776.h" | ||
26 | 28 | ||
27 | #if 0 /*still unused*/ | ||
28 | /* The battery manufacturer's website shows discharge curves down to 3.0V, | ||
29 | so 'dangerous' and 'shutoff' levels of 3.4V and 3.3V should be safe. | ||
30 | */ | ||
31 | const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] = | 29 | const unsigned short battery_level_dangerous[BATTERY_TYPES_COUNT] = |
32 | { | 30 | { |
33 | 3550 | 31 | 3500 |
34 | }; | 32 | }; |
35 | 33 | ||
34 | /* the OF shuts down at this voltage */ | ||
36 | const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT] = | 35 | const unsigned short battery_level_shutoff[BATTERY_TYPES_COUNT] = |
37 | { | 36 | { |
38 | 3450 | 37 | 3450 |
39 | }; | 38 | }; |
40 | 39 | ||
41 | /* voltages (millivolt) of 0%, 10%, ... 100% when charging disabled */ | 40 | /* voltages (millivolt) of 0%, 10%, ... 100% when charging disabled */ |
41 | /* FIXME: This is guessed. Make proper curve using battery_bench */ | ||
42 | const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] = | 42 | const unsigned short percent_to_volt_discharge[BATTERY_TYPES_COUNT][11] = |
43 | { | 43 | { |
44 | { 3300, 3692, 3740, 3772, 3798, 3828, 3876, 3943, 4013, 4094, 4194 } | 44 | { 3450, 3692, 3740, 3772, 3798, 3828, 3876, 3943, 4013, 4094, 4194 } |
45 | }; | 45 | }; |
46 | 46 | ||
47 | #if CONFIG_CHARGING | 47 | #if CONFIG_CHARGING |
48 | /* voltages (millivolt) of 0%, 10%, ... 100% when charging enabled */ | 48 | /* voltages (millivolt) of 0%, 10%, ... 100% when charging enabled */ |
49 | const unsigned short percent_to_volt_charge[11] = | 49 | /* FIXME: This is guessed. Make proper curve using battery_bench */ |
50 | const unsigned short const percent_to_volt_charge[11] = | ||
50 | { | 51 | { |
51 | 3417, 3802, 3856, 3888, 3905, 3931, 3973, 4025, 4084, 4161, 4219 | 52 | 3600, 3802, 3856, 3888, 3905, 3931, 3973, 4025, 4084, 4161, 4219 |
52 | }; | 53 | }; |
53 | #endif /* CONFIG_CHARGING */ | ||
54 | #endif | ||
55 | |||
56 | #define BATT_MINMVOLT 3450 /* minimum millivolts of battery */ | ||
57 | #define BATT_MAXMVOLT 4150 /* maximum millivolts of battery */ | ||
58 | #define BATT_MAXRUNTIME (10 * 60) /* maximum runtime with full battery in | ||
59 | minutes */ | ||
60 | |||
61 | extern void send_battery_level_event(void); | ||
62 | extern int last_sent_battery_level; | ||
63 | extern int battery_percent; | ||
64 | |||
65 | static unsigned int battery_millivolts = BATT_MAXMVOLT; | ||
66 | /* estimated remaining time in minutes */ | ||
67 | static int powermgmt_est_runningtime_min = BATT_MAXRUNTIME; | ||
68 | 54 | ||
69 | static void battery_status_update(void) | 55 | unsigned int power_input_status(void) |
70 | { | 56 | { |
71 | static time_t last_change = 0; | 57 | unsigned status = POWER_INPUT_NONE; |
72 | time_t now; | 58 | int fd = open("/dev/minivet", O_RDONLY); |
73 | 59 | if (fd >= 0) | |
74 | time(&now); | 60 | { |
75 | 61 | if (ioctl(fd, IOCTL_MINIVET_DET_VBUS, NULL) > 0) | |
76 | if (last_change < now) { | 62 | status = POWER_INPUT_MAIN_CHARGER; |
77 | last_change = now; | 63 | close(fd); |
78 | |||
79 | battery_percent = 100 * (battery_millivolts - BATT_MINMVOLT) / | ||
80 | (BATT_MAXMVOLT - BATT_MINMVOLT); | ||
81 | |||
82 | powermgmt_est_runningtime_min = | ||
83 | battery_percent * BATT_MAXRUNTIME / 100; | ||
84 | } | 64 | } |
85 | 65 | return status; | |
86 | send_battery_level_event(); | ||
87 | } | 66 | } |
88 | 67 | ||
89 | void battery_read_info(int *voltage, int *level) | 68 | #endif /* CONFIG_CHARGING */ |
90 | { | ||
91 | battery_status_update(); | ||
92 | |||
93 | if (voltage) | ||
94 | *voltage = battery_millivolts; | ||
95 | |||
96 | if (level) | ||
97 | *level = battery_percent; | ||
98 | } | ||
99 | |||
100 | unsigned int battery_voltage(void) | ||
101 | { | ||
102 | battery_status_update(); | ||
103 | return battery_millivolts; | ||
104 | } | ||
105 | |||
106 | int battery_level(void) | ||
107 | { | ||
108 | battery_status_update(); | ||
109 | return battery_percent; | ||
110 | } | ||
111 | |||
112 | int battery_time(void) | ||
113 | { | ||
114 | battery_status_update(); | ||
115 | return powermgmt_est_runningtime_min; | ||
116 | } | ||
117 | 69 | ||
118 | bool battery_level_safe(void) | ||
119 | { | ||
120 | return battery_level() >= 10; | ||
121 | } | ||
122 | 70 | ||
123 | void set_battery_capacity(int capacity) | 71 | /* Returns battery voltage from ADC [millivolts], |
72 | * adc returns voltage in 5mV steps */ | ||
73 | unsigned int battery_adc_voltage(void) | ||
124 | { | 74 | { |
125 | (void)capacity; | 75 | return adc_read(3) * 5; |
126 | } | 76 | } |
127 | 77 | ||
128 | #if BATTERY_TYPES_COUNT > 1 | 78 | bool charging_state(void) |
129 | void set_battery_type(int type) | ||
130 | { | 79 | { |
131 | (void)type; | 80 | /* cannot make this static (initializer not constant error), but gcc |
81 | * seems to calculate at compile time anyway */ | ||
82 | const unsigned short charged_thres = | ||
83 | ((percent_to_volt_charge[9] + percent_to_volt_charge[10]) / 2); | ||
84 | |||
85 | bool ret = (power_input_status() == POWER_INPUT_MAIN_CHARGER); | ||
86 | /* dont indicate for > ~95% */ | ||
87 | return ret && (battery_adc_voltage() <= charged_thres); | ||
132 | } | 88 | } |
133 | #endif | ||