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) 2013 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.
*
* Test lid switch.
*/
#include "battery_smart.h"
#include "charge_state.h"
#include "chipset.h"
#include "common.h"
#include "gpio.h"
#include "hooks.h"
#include "host_command.h"
#include "task.h"
#include "test_util.h"
#include "util.h"
#define WAIT_CHARGER_TASK 500
#define BATTERY_DETACH_DELAY 35000
static int mock_chipset_state = CHIPSET_STATE_ON;
static int is_shutdown;
static int is_force_discharge;
static int is_hibernated;
void chipset_force_shutdown(void)
{
is_shutdown = 1;
}
int chipset_in_state(int state_mask)
{
return state_mask & mock_chipset_state;
}
int board_discharge_on_ac(int enabled)
{
is_force_discharge = enabled;
return EC_SUCCESS;
}
void system_hibernate(int sec, int usec)
{
is_hibernated = 1;
}
/* Setup init condition */
static void test_setup(void)
{
const struct battery_info *bat_info = battery_get_info();
/* 50% of charge */
sb_write(SB_RELATIVE_STATE_OF_CHARGE, 50);
sb_write(SB_ABSOLUTE_STATE_OF_CHARGE, 50);
/* 25 degree Celsius */
sb_write(SB_TEMPERATURE, 250 + 2731);
/* Normal voltage */
sb_write(SB_VOLTAGE, bat_info->voltage_normal);
sb_write(SB_CHARGING_VOLTAGE, bat_info->voltage_max);
sb_write(SB_CHARGING_CURRENT, 4000);
/* Discharging at 100mAh */
sb_write(SB_CURRENT, -100);
/* Unplug AC */
gpio_set_level(GPIO_AC_PRESENT, 0);
}
static int wait_charging_state(void)
{
enum power_state state;
task_wake(TASK_ID_CHARGER);
msleep(WAIT_CHARGER_TASK);
state = charge_get_state();
ccprintf("[CHARGING TEST] state = %d\n", state);
return state;
}
static int charge_control(enum ec_charge_control_mode mode)
{
struct ec_params_charge_control params;
params.mode = mode;
return test_send_host_command(EC_CMD_CHARGE_CONTROL, 1, ¶ms,
sizeof(params), NULL, 0);
}
static int test_charge_state(void)
{
enum power_state state;
state = wait_charging_state();
/* Plug AC, charging at 1000mAh */
ccprintf("[CHARGING TEST] AC on\n");
gpio_set_level(GPIO_AC_PRESENT, 1);
sb_write(SB_CURRENT, 1000);
state = wait_charging_state();
TEST_ASSERT(state == PWR_STATE_CHARGE);
/* Detach battery, charging error */
ccprintf("[CHARGING TEST] Detach battery\n");
TEST_ASSERT(test_detach_i2c(I2C_PORT_BATTERY, BATTERY_ADDR) ==
EC_SUCCESS);
msleep(BATTERY_DETACH_DELAY);
state = wait_charging_state();
TEST_ASSERT(state == PWR_STATE_ERROR);
/* Attach battery again, charging */
ccprintf("[CHARGING TEST] Attach battery\n");
test_attach_i2c(I2C_PORT_HOST, BATTERY_ADDR);
state = wait_charging_state();
TEST_ASSERT(state == PWR_STATE_CHARGE);
/* Unplug AC, discharging at 1000mAh */
ccprintf("[CHARGING TEST] AC off\n");
gpio_set_level(GPIO_AC_PRESENT, 0);
sb_write(SB_CURRENT, -1000);
state = wait_charging_state();
TEST_ASSERT(state == PWR_STATE_DISCHARGE);
/* Discharging overtemp */
ccprintf("[CHARGING TEST] AC off, batt temp = 90 C\n");
gpio_set_level(GPIO_AC_PRESENT, 0);
sb_write(SB_CURRENT, -1000);
state = wait_charging_state();
TEST_ASSERT(state == PWR_STATE_DISCHARGE);
sb_write(SB_TEMPERATURE, CELSIUS_TO_DECI_KELVIN(90));
state = wait_charging_state();
TEST_ASSERT(is_shutdown);
TEST_ASSERT(state == PWR_STATE_DISCHARGE);
sb_write(SB_TEMPERATURE, CELSIUS_TO_DECI_KELVIN(40));
/* Force idle */
ccprintf("[CHARGING TEST] AC on, force idle\n");
gpio_set_level(GPIO_AC_PRESENT, 1);
sb_write(SB_CURRENT, 1000);
state = wait_charging_state();
TEST_ASSERT(state == PWR_STATE_CHARGE);
charge_control(CHARGE_CONTROL_IDLE);
state = wait_charging_state();
TEST_ASSERT(state == PWR_STATE_IDLE);
charge_control(CHARGE_CONTROL_NORMAL);
state = wait_charging_state();
TEST_ASSERT(state == PWR_STATE_CHARGE);
/* Force discharge */
ccprintf("[CHARGING TEST] AC on, force discharge\n");
gpio_set_level(GPIO_AC_PRESENT, 1);
sb_write(SB_CURRENT, 1000);
charge_control(CHARGE_CONTROL_DISCHARGE);
state = wait_charging_state();
TEST_ASSERT(state == PWR_STATE_IDLE);
TEST_ASSERT(is_force_discharge);
charge_control(CHARGE_CONTROL_NORMAL);
state = wait_charging_state();
TEST_ASSERT(state == PWR_STATE_CHARGE);
TEST_ASSERT(!is_force_discharge);
return EC_SUCCESS;
}
static int test_low_battery(void)
{
ccprintf("[CHARGING TEST] Low battery with AC\n");
gpio_set_level(GPIO_AC_PRESENT, 1);
is_hibernated = 0;
sb_write(SB_CURRENT, 1000);
sb_write(SB_RELATIVE_STATE_OF_CHARGE, 2);
wait_charging_state();
mock_chipset_state = CHIPSET_STATE_SOFT_OFF;
hook_notify(HOOK_CHIPSET_SHUTDOWN);
TEST_ASSERT(!is_hibernated);
ccprintf("[CHARGING TEST] Low battery shutdown S0->S5\n");
mock_chipset_state = CHIPSET_STATE_ON;
hook_notify(HOOK_CHIPSET_PRE_INIT);
hook_notify(HOOK_CHIPSET_STARTUP);
gpio_set_level(GPIO_AC_PRESENT, 0);
is_hibernated = 0;
sb_write(SB_CURRENT, -1000);
sb_write(SB_RELATIVE_STATE_OF_CHARGE, 2);
wait_charging_state();
mock_chipset_state = CHIPSET_STATE_SOFT_OFF;
hook_notify(HOOK_CHIPSET_SHUTDOWN);
TEST_ASSERT(is_hibernated);
ccprintf("[CHARGING TEST] Low battery shutdown S5\n");
is_hibernated = 0;
sb_write(SB_RELATIVE_STATE_OF_CHARGE, 10);
wait_charging_state();
sb_write(SB_RELATIVE_STATE_OF_CHARGE, 2);
wait_charging_state();
TEST_ASSERT(is_hibernated);
ccprintf("[CHARGING TEST] Low battery AP shutdown\n");
is_shutdown = 0;
mock_chipset_state = CHIPSET_STATE_ON;
sb_write(SB_RELATIVE_STATE_OF_CHARGE, 10);
gpio_set_level(GPIO_AC_PRESENT, 1);
sb_write(SB_CURRENT, 1000);
wait_charging_state();
gpio_set_level(GPIO_AC_PRESENT, 0);
sb_write(SB_CURRENT, -1000);
sb_write(SB_RELATIVE_STATE_OF_CHARGE, 2);
wait_charging_state();
usleep(32 * SECOND);
wait_charging_state();
TEST_ASSERT(is_shutdown);
return EC_SUCCESS;
}
static int test_batt_fake(void)
{
ccprintf("[CHARGING TEST] Fake battery command\n");
mock_chipset_state = CHIPSET_STATE_ON;
hook_notify(HOOK_CHIPSET_PRE_INIT);
hook_notify(HOOK_CHIPSET_STARTUP);
gpio_set_level(GPIO_AC_PRESENT, 0);
is_hibernated = 0;
sb_write(SB_CURRENT, -1000);
sb_write(SB_RELATIVE_STATE_OF_CHARGE, 30);
wait_charging_state();
UART_INJECT("battfake 2\n");
msleep(50);
mock_chipset_state = CHIPSET_STATE_SOFT_OFF;
hook_notify(HOOK_CHIPSET_SHUTDOWN);
TEST_ASSERT(is_hibernated);
return EC_SUCCESS;
}
void run_test(void)
{
test_setup();
RUN_TEST(test_charge_state);
RUN_TEST(test_low_battery);
RUN_TEST(test_batt_fake);
test_print_result();
}
|