1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*
* Smart battery driver.
*/
#include "console.h"
#include "smart_battery.h"
#include "timer.h"
#include "uart.h"
#include "util.h"
/* Read battery discharging current
* unit: mA
* negative value: charging
*/
int battery_current(int *current)
{
int rv, d;
rv = sb_read(SB_CURRENT, &d);
if (rv)
return rv;
*current = (int16_t)d;
return EC_SUCCESS;
}
int battery_average_current(int *current)
{
int rv, d;
rv = sb_read(SB_AVERAGE_CURRENT, &d);
if (rv)
return rv;
*current = (int16_t)d;
return EC_SUCCESS;
}
/* Calculate battery time in minutes, under a charging rate
* rate > 0: charging, negative time to full
* rate < 0: discharging, positive time to empty
* rate == 0: invalid input, time = 0
*/
int battery_time_at_rate(int rate, int *minutes)
{
int rv;
int ok, time;
int loop, cmd, output_sign;
if (rate == 0) {
*minutes = 0;
return EC_ERROR_INVAL;
}
rv = sb_write(SB_AT_RATE, rate);
if (rv)
return rv;
loop = 5;
while (loop--) {
rv = sb_read(SB_AT_RATE_OK, &ok);
if (rv)
return rv;
if (ok) {
if (rate > 0) {
cmd = SB_AT_RATE_TIME_TO_FULL;
output_sign = -1;
} else {
cmd = SB_AT_RATE_TIME_TO_EMPTY;
output_sign = 1;
}
rv = sb_read(cmd, &time);
if (rv)
return rv;
*minutes = (time == 0xffff) ? 0 : output_sign * time;
return EC_SUCCESS;
} else {
/* wait 10ms for AT_RATE_OK */
usleep(10000);
}
}
return EC_ERROR_TIMEOUT;
}
/* Read manufacturer date */
int battery_manufacturer_date(int *year, int *month, int *day)
{
int rv;
int ymd;
rv = sb_read(SB_SPECIFICATION_INFO, &ymd);
if (rv)
return rv;
/* battery date format:
* ymd = day + month * 32 + (year - 1980) * 256
*/
*year = (ymd >> 8) + 1980;
*month = (ymd & 0xff) / 32;
*day = (ymd & 0xff) % 32;
return EC_SUCCESS;
}
/* Console commands */
static int command_battery(int argc, char **argv)
{
int rv;
int d;
int hour, minute;
const char *unit;
uart_puts("Reading battery...\n");
rv = battery_temperature(&d);
if (rv)
return rv;
uart_printf(" Temperature: 0x%04x = %d x 0.1K (%d C)\n",
d, d, (d-2731)/10);
battery_serial_number(&d);
uart_printf(" Serial number: 0x%04x\n", d);
battery_voltage(&d);
uart_printf(" Voltage: 0x%04x = %d mV\n", d, d);
battery_desired_voltage(&d);
uart_printf(" Desired voltage 0x%04x = %d mV\n", d, d);
battery_design_voltage(&d);
uart_printf(" Design output voltage 0x%04x = %d mV\n", d, d);
battery_current(&d);
uart_printf(" Current: 0x%04x = %d mA",
d & 0xffff, d);
if (d > 0)
uart_puts("(CHG)");
else if (d < 0)
uart_puts("(DISCHG)");
uart_puts("\n");
battery_desired_current(&d);
uart_printf(" Desired current 0x%04x = %d mA\n", d, d);
battery_get_battery_mode(&d);
uart_printf(" Battery mode: 0x%04x\n", d);
unit = (d & MODE_CAPACITY) ? "0 mW" : " mAh";
battery_state_of_charge(&d);
uart_printf(" %% of charge: %d %%\n", d);
battery_state_of_charge_abs(&d);
uart_printf(" Abs %% of charge: %d %%\n", d);
battery_remaining_capacity(&d);
uart_printf(" Remaining capacity: %d%s\n", d, unit);
battery_full_charge_capacity(&d);
uart_printf(" Full charge capacity: %d%s\n", d, unit);
battery_design_capacity(&d);
uart_printf(" Design capacity: %d%s\n", d, unit);
battery_time_to_empty(&d);
if (d == 65535) {
hour = 0;
minute = 0;
} else {
hour = d / 60;
minute = d % 60;
}
uart_printf(" Time to empty: %dh:%d\n", hour, minute);
battery_time_to_full(&d);
if (d == 65535) {
hour = 0;
minute = 0;
} else {
hour = d / 60;
minute = d % 60;
}
uart_printf(" Time to full: %dh:%d\n", hour, minute);
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(battery, command_battery);
static int command_sb(int argc, char **argv)
{
int rv;
int cmd, d;
char *e;
if (argc < 3)
goto usage;
cmd = strtoi(argv[2], &e, 0);
if (*e) {
uart_puts("Invalid cmd.\n");
goto usage;
}
if (argv[1][0] == 'r') {
rv = i2c_read16(I2C_PORT_BATTERY, BATTERY_ADDR, cmd, &d);
if (rv) {
uart_puts("I2C read failed.\n");
return rv;
}
uart_printf("R SBCMD[%04x] 0x%04x (%d)\n", cmd, d, d);
return EC_SUCCESS;
} else if (argc >= 4 && argv[1][0] == 'w') {
d = strtoi(argv[3], &e, 0);
if (*e) {
uart_puts("Invalid w_word.\n");
goto usage;
}
uart_printf("W SBCMD[%04x] 0x%04x (%d)\n", cmd, d, d);
rv = i2c_write16(I2C_PORT_BATTERY, BATTERY_ADDR, cmd, d);
if (rv) {
uart_puts("I2C write failed.\n");
return rv;
}
return EC_SUCCESS;
}
usage:
uart_puts("Usage:sb <r/w> cmd [uint16_t w_word]\n");
uart_puts(" sb r 0x14 // desired charging current\n");
uart_puts(" sb r 0x15 // desired charging voltage\n");
uart_puts(" sb r 0x3 // battery mode\n");
uart_puts(" sb w 0x3 0xe001 // set battery mode\n");
return EC_SUCCESS;
}
DECLARE_CONSOLE_COMMAND(sb, command_sb);
|