summaryrefslogtreecommitdiff
path: root/test/charge_ramp.c
blob: 84cac57b8e97e8f4fadf6f22517a11452b5106a8 (plain)
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* Copyright 2015 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 AC input current ramp.
 */

#include "charge_manager.h"
#include "charge_ramp.h"
#include "common.h"
#include "console.h"
#include "gpio.h"
#include "hooks.h"
#include "task.h"
#include "test_util.h"
#include "timer.h"
#include "usb_charge.h"
#include "util.h"

#define TASK_EVENT_OVERCURRENT (1 << 0)

#define RAMP_STABLE_DELAY (120*SECOND)

/*
 * Time to delay for detecting the charger type. This value follows
 * the value in common/charge_ramp.c, but must be less than the real
 * CHARGE_DETECT_DELAY so we guarantee we wake up before the ramp
 * has started.
 */
#define CHARGE_DETECT_DELAY_TEST (CHARGE_DETECT_DELAY - 100*MSEC)

static int system_load_current_ma;
static int vbus_low_current_ma = 500;
static int overcurrent_current_ma = 3000;

static int charge_limit_ma;

/* Mock functions */

__override uint8_t board_get_usb_pd_port_count(void)
{
	return CONFIG_USB_PD_PORT_MAX_COUNT;
}

/* Override test_mockable implementations in charge_ramp module */
int chg_ramp_allowed(int port, int supplier)
{
	/* Ramp for TEST4-TEST8 */
	return supplier > CHARGE_SUPPLIER_TEST3;
}

int chg_ramp_max(int port, int supplier, int sup_curr)
{
	if (supplier == CHARGE_SUPPLIER_TEST7)
		return 1600;
	else if (supplier == CHARGE_SUPPLIER_TEST8)
		return 2400;
	else
		return 3000;
}

/* Mock bc12_ports[] array to make linker happy */
struct bc12_config bc12_ports[0];

int charge_is_consuming_full_input_current(void)
{
	return charge_limit_ma <= system_load_current_ma;
}

int board_is_vbus_too_low(int port, enum chg_ramp_vbus_state ramp_state)
{
	return MIN(system_load_current_ma, charge_limit_ma) >
	       vbus_low_current_ma;
}

void board_set_charge_limit(int port, int supplier, int limit_ma,
			    int max_ma, int max_mv)
{
	charge_limit_ma = limit_ma;
	if (charge_limit_ma > overcurrent_current_ma)
		task_set_event(TASK_ID_TEST_RUNNER, TASK_EVENT_OVERCURRENT);
}

/* Test utilities */

static void plug_charger_with_ts(int supplier_type, int port, int min_current,
				 int vbus_low_current, int overcurrent_current,
				 timestamp_t reg_time)
{
	vbus_low_current_ma = vbus_low_current;
	overcurrent_current_ma = overcurrent_current;
	chg_ramp_charge_supplier_change(port, supplier_type, min_current,
					reg_time, 0);
}

static void plug_charger(int supplier_type, int port, int min_current,
			 int vbus_low_current, int overcurrent_current)
{
	plug_charger_with_ts(supplier_type, port, min_current,
			     vbus_low_current, overcurrent_current,
			     get_time());
}

static void unplug_charger(void)
{
	chg_ramp_charge_supplier_change(CHARGE_PORT_NONE, CHARGE_SUPPLIER_NONE,
					0, get_time(), 0);
}

static int unplug_charger_and_check(void)
{
	unplug_charger();
	usleep(CHARGE_DETECT_DELAY_TEST);
	return charge_limit_ma == 0;
}

static int wait_stable_no_overcurrent(void)
{
	return task_wait_event(RAMP_STABLE_DELAY) != TASK_EVENT_OVERCURRENT;
}

static int is_in_range(int x, int min, int max)
{
	return x >= min && x <= max;
}

/* Tests */

static int test_no_ramp(void)
{
	system_load_current_ma = 3000;
	/* A powerful charger, but hey, you're not allowed to ramp! */
	plug_charger(CHARGE_SUPPLIER_TEST1, 0, 500, 3000, 3000);
	/*
	 * NOTE: Since this is currently the first test being run, give the
	 * charge ramp task enough time to actually transition states and set
	 * the charge limit.  This just needs at least transition to the
	 * CHG_RAMP_OVERCURRENT_DETECT state.
	 */
	usleep(CHARGE_DETECT_DELAY_TEST + 200*MSEC);
	/* That's right. Start at 500 mA */
	TEST_ASSERT(charge_limit_ma == 500);
	TEST_ASSERT(wait_stable_no_overcurrent());
	/* ... and stays at 500 mA */
	TEST_ASSERT(charge_limit_ma == 500);

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_full_ramp(void)
{
	system_load_current_ma = 3000;
	/* Now you get to ramp with this 3A charger */
	plug_charger(CHARGE_SUPPLIER_TEST4, 0, 500, 3000, 3000);
	usleep(CHARGE_DETECT_DELAY_TEST);
	/* Start with something around 500 mA */
	TEST_ASSERT(is_in_range(charge_limit_ma, 500, 800));
	TEST_ASSERT(wait_stable_no_overcurrent());
	/* And ramp up to 3A */
	TEST_ASSERT(charge_limit_ma == 3000);

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_vbus_dip(void)
{
	system_load_current_ma = 3000;
	/* VBUS dips too low right before the charger shuts down */
	plug_charger(CHARGE_SUPPLIER_TEST5, 0, 1000, 1500, 1600);

	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(is_in_range(charge_limit_ma, 1300, 1500));

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_overcurrent(void)
{
	system_load_current_ma = 3000;
	/* Huh...VBUS doesn't dip before the charger shuts down */
	plug_charger(CHARGE_SUPPLIER_TEST6, 0, 500, 3000, 1500);
	usleep(CHARGE_DETECT_DELAY_TEST);
	/* Ramp starts at 500 mA */
	TEST_ASSERT(is_in_range(charge_limit_ma, 500, 700));

	while (task_wait_event(RAMP_STABLE_DELAY) == TASK_EVENT_OVERCURRENT) {
		/* Charger goes away but comes back after 0.6 seconds */
		unplug_charger();
		usleep(MSEC * 600);
		plug_charger(CHARGE_SUPPLIER_TEST6, 0, 500, 3000, 1500);
		usleep(CHARGE_DETECT_DELAY_TEST);
		/* Ramp restarts at 500 mA */
		TEST_ASSERT(is_in_range(charge_limit_ma, 500, 700));
	}

	TEST_ASSERT(is_in_range(charge_limit_ma, 1300, 1500));

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_switch_outlet(void)
{
	int i;

	system_load_current_ma = 3000;
	/* Here's a nice powerful charger */
	plug_charger(CHARGE_SUPPLIER_TEST6, 0, 500, 3000, 3000);

	/*
	 * Now the user decides to move it to a nearby outlet...actually
	 * they decide to move it 5 times!
	 */
	for (i = 0; i < 5; ++i) {
		usleep(SECOND * 20);
		unplug_charger();
		usleep(SECOND * 1.5);
		plug_charger(CHARGE_SUPPLIER_TEST6, 0, 500, 3000, 3000);
		usleep(CHARGE_DETECT_DELAY_TEST);
		/* Ramp restarts at 500 mA */
		TEST_ASSERT(is_in_range(charge_limit_ma, 500, 700));
	}

	/* Should still ramp up to 3000 mA */
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(charge_limit_ma == 3000);

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_fast_switch(void)
{
	int i;

	system_load_current_ma = 3000;
	plug_charger(CHARGE_SUPPLIER_TEST4, 0, 500, 3000, 3000);

	/*
	 * Here comes that naughty user again, and this time they are switching
	 * outlet really quickly. Fortunately this time they only do it twice.
	 */
	for (i = 0; i < 2; ++i) {
		usleep(SECOND * 20);
		unplug_charger();
		usleep(600 * MSEC);
		plug_charger(CHARGE_SUPPLIER_TEST4, 0, 500, 3000, 3000);
		usleep(CHARGE_DETECT_DELAY_TEST);
		/* Ramp restarts at 500 mA */
		TEST_ASSERT(is_in_range(charge_limit_ma, 500, 700));
	}

	/* Should still ramp up to 3000 mA */
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(charge_limit_ma == 3000);

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_overcurrent_after_switch_outlet(void)
{
	system_load_current_ma = 3000;
	/* Here's a less powerful charger */
	plug_charger(CHARGE_SUPPLIER_TEST5, 0, 500, 3000, 1500);
	usleep(SECOND * 5);

	/* Now the user decides to move it to a nearby outlet */
	unplug_charger();
	usleep(SECOND * 1.5);
	plug_charger(CHARGE_SUPPLIER_TEST5, 0, 500, 3000, 1500);

	/* Okay the user is satisified */
	while (task_wait_event(RAMP_STABLE_DELAY) == TASK_EVENT_OVERCURRENT) {
		/* Charger goes away but comes back after 0.6 seconds */
		unplug_charger();
		usleep(MSEC * 600);
		plug_charger(CHARGE_SUPPLIER_TEST5, 0, 500, 3000, 1500);
		usleep(CHARGE_DETECT_DELAY_TEST);
		/* Ramp restarts at 500 mA */
		TEST_ASSERT(is_in_range(charge_limit_ma, 500, 700));
	}

	TEST_ASSERT(is_in_range(charge_limit_ma, 1300, 1500));

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_partial_load(void)
{
	/* We have a 3A charger, but we just want 1.5A */
	system_load_current_ma = 1500;
	plug_charger(CHARGE_SUPPLIER_TEST4, 0, 500, 3000, 2500);

	/* We should end up with a little bit more than 1.5A */
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(is_in_range(charge_limit_ma, 1500, 1600));

	/* Ok someone just started watching YouTube */
	system_load_current_ma = 2000;
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(is_in_range(charge_limit_ma, 2000, 2100));

	/* Somehow the system load increases again */
	system_load_current_ma = 2600;
	while (task_wait_event(RAMP_STABLE_DELAY) == TASK_EVENT_OVERCURRENT) {
		/* Charger goes away but comes back after 0.6 seconds */
		unplug_charger();
		usleep(MSEC * 600);
		plug_charger(CHARGE_SUPPLIER_TEST4, 0, 500, 3000, 2500);
		usleep(CHARGE_DETECT_DELAY_TEST);
		/* Ramp restarts at 500 mA */
		TEST_ASSERT(is_in_range(charge_limit_ma, 500, 700));
	}

	/* Alright the charger isn't powerful enough, so we'll stop at 2.5A */
	TEST_ASSERT(is_in_range(charge_limit_ma, 2300, 2500));

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_charge_supplier_stable(void)
{
	system_load_current_ma = 3000;
	/* The charger says it's of type TEST4 initially */
	plug_charger(CHARGE_SUPPLIER_TEST4, 0, 500, 1500, 1600);
	/*
	 * And then it decides it's actually TEST2 after 0.5 seconds,
	 * why? Well, this charger is just evil.
	 */
	usleep(500 * MSEC);
	plug_charger(CHARGE_SUPPLIER_TEST2, 0, 3000, 3000, 3000);
	/* We should get 3A right away. */
	usleep(SECOND);
	TEST_ASSERT(charge_limit_ma == 3000);

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_charge_supplier_stable_ramp(void)
{
	system_load_current_ma = 3000;
	/* This time we start with a non-ramp charge supplier */
	plug_charger(CHARGE_SUPPLIER_TEST3, 0, 500, 3000, 3000);
	/*
	 * After 0.5 seconds, it's decided that the supplier is actually
	 * a 1.5A ramp supplier.
	 */
	usleep(500 * MSEC);
	plug_charger(CHARGE_SUPPLIER_TEST5, 0, 500, 1400, 1500);
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(is_in_range(charge_limit_ma, 1200, 1400));

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_charge_supplier_change(void)
{
	system_load_current_ma = 3000;
	/* Start with a 3A ramp charge supplier */
	plug_charger(CHARGE_SUPPLIER_TEST4, 0, 500, 3000, 3000);
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(charge_limit_ma == 3000);

	/* The charger decides to change type to a 1.5A non-ramp supplier */
	plug_charger(CHARGE_SUPPLIER_TEST1, 0, 1500, 3000, 3000);
	usleep(500 * MSEC);
	TEST_ASSERT(charge_limit_ma == 1500);
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(charge_limit_ma == 1500);

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_charge_port_change(void)
{
	system_load_current_ma = 3000;
	/* Start with a 1.5A ramp charge supplier on port 0 */
	plug_charger(CHARGE_SUPPLIER_TEST5, 0, 500, 1400, 1500);
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(is_in_range(charge_limit_ma, 1200, 1400));

	/* Here comes a 2.1A ramp charge supplier on port 1 */
	plug_charger(CHARGE_SUPPLIER_TEST6, 0, 500, 2000, 2100);
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(is_in_range(charge_limit_ma, 1800, 2000));

	/* Now we have a 2.5A non-ramp charge supplier on port 0 */
	plug_charger(CHARGE_SUPPLIER_TEST1, 0, 2500, 3000, 3000);
	usleep(SECOND);
	TEST_ASSERT(charge_limit_ma == 2500);
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(charge_limit_ma == 2500);

	/* Unplug on port 0 */
	plug_charger(CHARGE_SUPPLIER_TEST6, 0, 500, 2000, 2100);
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(is_in_range(charge_limit_ma, 1800, 2000));

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_vbus_shift(void)
{
	system_load_current_ma = 3000;
	/*
	 * At first, the charger is able to supply up to 1900 mA before
	 * the VBUS voltage starts to drop.
	 */
	plug_charger(CHARGE_SUPPLIER_TEST6, 0, 500, 1900, 2000);
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(is_in_range(charge_limit_ma, 1700, 1900));

	/* The charger heats up and VBUS voltage drops by 100mV */
	vbus_low_current_ma = 1800;
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(is_in_range(charge_limit_ma, 1600, 1800));

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_equal_priority_overcurrent(void)
{
	int overcurrent_count = 0;
	timestamp_t oc_time = get_time();

	system_load_current_ma = 3000;

	/*
	 * Now we have two charge suppliers of equal priorties plugged into
	 * port 0 and port 1. If the active one browns out, charge manager
	 * switches to the other one.
	 */
	while (1) {
		plug_charger_with_ts(CHARGE_SUPPLIER_TEST4, 0, 500, 3000,
				     2000, oc_time);
		oc_time = get_time();
		oc_time.val += 600 * MSEC;
		if (wait_stable_no_overcurrent())
			break;
		plug_charger_with_ts(CHARGE_SUPPLIER_TEST4, 1, 500, 3000,
				     2000, oc_time);
		oc_time = get_time();
		oc_time.val += 600 * MSEC;
		if (wait_stable_no_overcurrent())
			break;
		if (overcurrent_count++ >= 10) {
			/*
			 * Apparently we are in a loop and can never reach
			 * stable state.
			 */
			unplug_charger();
			return EC_ERROR_UNKNOWN;
		}
	}

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

static int test_ramp_limit(void)
{
	system_load_current_ma = 3000;

	/* Plug in supplier that is limited to 1.6A */
	plug_charger(CHARGE_SUPPLIER_TEST7, 0, 500, 3000, 3000);
	usleep(SECOND);
	TEST_ASSERT(is_in_range(charge_limit_ma, 500, 700));
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(charge_limit_ma == 1600);

	/* Switch to supplier that is limited to 2.4A */
	plug_charger(CHARGE_SUPPLIER_TEST8, 1, 500, 3000, 3000);
	usleep(SECOND);
	TEST_ASSERT(is_in_range(charge_limit_ma, 500, 700));
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(charge_limit_ma == 2400);

	/* Go back to 1.6A limited, but VBUS goes low before that point */
	plug_charger(CHARGE_SUPPLIER_TEST7, 0, 500, 1200, 1300);
	usleep(SECOND);
	TEST_ASSERT(is_in_range(charge_limit_ma, 500, 700));
	TEST_ASSERT(wait_stable_no_overcurrent());
	TEST_ASSERT(is_in_range(charge_limit_ma, 1000, 1200));

	TEST_ASSERT(unplug_charger_and_check());
	return EC_SUCCESS;
}

void run_test(int argc, char **argv)
{
	test_reset();

	/*
	 * If the following test order changes, make sure to add enough time for
	 * the charge ramp task to make its first transition after plugging in a
	 * charger.  See the comment in test_no_ramp().
	 */
	RUN_TEST(test_no_ramp);
	RUN_TEST(test_full_ramp);
	RUN_TEST(test_vbus_dip);
	RUN_TEST(test_overcurrent);
	RUN_TEST(test_switch_outlet);
	RUN_TEST(test_fast_switch);
	RUN_TEST(test_overcurrent_after_switch_outlet);
	RUN_TEST(test_partial_load);
	RUN_TEST(test_charge_supplier_stable);
	RUN_TEST(test_charge_supplier_stable_ramp);
	RUN_TEST(test_charge_supplier_change);
	RUN_TEST(test_charge_port_change);
	RUN_TEST(test_vbus_shift);
	RUN_TEST(test_equal_priority_overcurrent);
	RUN_TEST(test_ramp_limit);

	test_print_result();
}