summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorVic Yang <victoryang@chromium.org>2013-09-25 21:23:43 +0800
committerchrome-internal-fetch <chrome-internal-fetch@google.com>2013-09-25 19:18:20 +0000
commit9ce364fce0cfe2d547c672094e7b004750495d58 (patch)
tree02908fc30941d6c1d5998570cd3d3319206ce191 /test
parent45bcffd91a16d23340abd4a7c85e889c1e853573 (diff)
downloadchrome-ec-9ce364fce0cfe2d547c672094e7b004750495d58.tar.gz
Add test for LP5562 LED control
This adds a test for LP5562 LED control. The test mocks charging and battery status and check LED behavior is as expected. BUG=chrome-os-partner:19236 TEST=Pass the new test BRANCH=None Change-Id: Iac6b538b24c555bf48b6824f880091cd11a585d4 Signed-off-by: Vic Yang <victoryang@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/170597 Reviewed-by: Randall Spangler <rspangler@chromium.org>
Diffstat (limited to 'test')
-rw-r--r--test/build.mk3
-rw-r--r--test/led_lp5562.c278
-rw-r--r--test/led_lp5562.tasklist17
-rw-r--r--test/test_config.h20
4 files changed, 312 insertions, 6 deletions
diff --git a/test/build.mk b/test/build.mk
index 7dbb08336f..8c1ede35b6 100644
--- a/test/build.mk
+++ b/test/build.mk
@@ -29,7 +29,7 @@ test-list-$(BOARD_bolt)=
# Emulator tests
test-list-host=mutex pingpong utils kb_scan kb_mkbp lid_sw power_button hooks
test-list-host+=thermal flash queue kb_8042 extpwr_gpio console_edit system
-test-list-host+=sbs_charging adapter host_command thermal_falco
+test-list-host+=sbs_charging adapter host_command thermal_falco led_lp5562
adapter-y=adapter.o
console_edit-y=console_edit.o
@@ -40,6 +40,7 @@ host_command-y=host_command.o
kb_8042-y=kb_8042.o
kb_mkbp-y=kb_mkbp.o
kb_scan-y=kb_scan.o
+led_lp5562-y=led_lp5562.o
lid_sw-y=lid_sw.o
mutex-y=mutex.o
pingpong-y=pingpong.o
diff --git a/test/led_lp5562.c b/test/led_lp5562.c
new file mode 100644
index 0000000000..fe99d5eb44
--- /dev/null
+++ b/test/led_lp5562.c
@@ -0,0 +1,278 @@
+/* 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 "common.h"
+#include "console.h"
+#include "host_command.h"
+#include "lp5562.h"
+#include "pmu_tpschrome.h"
+#include "smart_battery.h"
+#include "test_util.h"
+#include "timer.h"
+#include "util.h"
+
+#define LP5562_I2C_ADDR (0x30 << 1)
+#define LP5562_NUM_WATCH_REG 0x71
+static uint8_t lp5562_reg[LP5562_NUM_WATCH_REG];
+
+#define LED_COLOR_NONE LP5562_COLOR_NONE
+#define LED_COLOR_GREEN LP5562_COLOR_GREEN(0x10)
+#define LED_COLOR_YELLOW LP5562_COLOR_BLUE(0x40)
+#define LED_COLOR_RED LP5562_COLOR_RED(0x80)
+
+static int mock_ac;
+static enum charging_state mock_charge_state = ST_IDLE;
+static int lp5562_failed_i2c_reg = -1;
+static const char * const state_names[] = POWER_STATE_NAME_TABLE;
+
+/*****************************************************************************/
+/* Mock functions */
+
+int gpio_get_level(enum gpio_signal signal)
+{
+ if (signal == GPIO_AC_PRESENT)
+ return mock_ac;
+ return 0;
+}
+
+static void set_ac(int ac)
+{
+ mock_ac = ac;
+ ccprintf("[%T TEST AC = %d]\n", ac);
+}
+
+enum charging_state charge_get_state(void)
+{
+ return mock_charge_state;
+}
+
+static void set_charge_state(enum charging_state s)
+{
+ mock_charge_state = s;
+ ccprintf("[%T TEST Charge state = %s]\n", state_names[s]);
+}
+
+static void set_battery_soc(int soc)
+{
+ sb_write(SB_RELATIVE_STATE_OF_CHARGE, soc);
+ sb_write(SB_ABSOLUTE_STATE_OF_CHARGE, soc);
+}
+
+/*****************************************************************************/
+/* Test utilities */
+
+static int lp5562_i2c_write8(int port, int slave_addr, int offset, int data)
+{
+ if (port != I2C_PORT_HOST || slave_addr != LP5562_I2C_ADDR)
+ return EC_ERROR_INVAL;
+ if (offset == lp5562_failed_i2c_reg)
+ return EC_ERROR_UNKNOWN;
+ if (offset < LP5562_NUM_WATCH_REG)
+ lp5562_reg[offset] = data;
+ return EC_SUCCESS;
+}
+DECLARE_TEST_I2C_WRITE8(lp5562_i2c_write8);
+
+static int lp5562_get_color(void)
+{
+ return lp5562_reg[LP5562_REG_B_PWM] |
+ (lp5562_reg[LP5562_REG_G_PWM] << 8) |
+ (lp5562_reg[LP5562_REG_R_PWM] << 16);
+}
+
+static int lp5562_powered(void)
+{
+ return lp5562_reg[LP5562_REG_ENABLE] & 0x40;
+}
+
+static int lp5562_in_pwm_mode(void)
+{
+ return lp5562_reg[LP5562_REG_LED_MAP] == 0;
+}
+
+static int verify_color(int expected_color)
+{
+ int actual = lp5562_get_color();
+
+ if (expected_color == LED_COLOR_NONE)
+ return !lp5562_powered();
+ if (!lp5562_powered())
+ return 0;
+ if (!lp5562_in_pwm_mode())
+ return 0;
+
+ ccprintf("[%T LED color = 0x%06x]\n", actual);
+
+ return actual == expected_color;
+}
+
+/*****************************************************************************/
+/* Tests */
+
+static int test_led_power(void)
+{
+ /* Check LED is off */
+ TEST_ASSERT(!lp5562_powered());
+
+ /* Plug in AC, and LED should turn on within a second */
+ set_ac(1);
+ msleep(1500);
+ TEST_ASSERT(lp5562_powered());
+
+ /* Change state while AC is on. LED should keep on */
+ set_charge_state(ST_CHARGING_ERROR);
+ msleep(1500);
+ TEST_ASSERT(lp5562_powered());
+
+ /* Unplug AC. LED should turn off */
+ set_ac(0);
+ msleep(1500);
+ TEST_ASSERT(!lp5562_powered());
+
+ /* Plug AC again. LED should turn on */
+ set_ac(1);
+ msleep(1500);
+ TEST_ASSERT(lp5562_powered());
+
+ return EC_SUCCESS;
+}
+
+static int test_led_color(void)
+{
+ /* IDLE0 */
+ set_ac(1);
+ set_charge_state(ST_IDLE0);
+ msleep(30000);
+ TEST_ASSERT(verify_color(LED_COLOR_YELLOW));
+
+ /* BAD_COND*/
+ set_charge_state(ST_BAD_COND);
+ msleep(30000);
+ TEST_ASSERT(verify_color(LED_COLOR_YELLOW));
+
+ /* PRE_CHARGING */
+ set_charge_state(ST_PRE_CHARGING);
+ msleep(30000);
+ TEST_ASSERT(verify_color(LED_COLOR_YELLOW));
+
+ /* IDLE */
+ set_charge_state(ST_IDLE);
+ set_battery_soc(50);
+ msleep(30000);
+ TEST_ASSERT(verify_color(LED_COLOR_YELLOW));
+ set_battery_soc(99);
+ msleep(30000);
+ TEST_ASSERT(verify_color(LED_COLOR_GREEN));
+
+ /* DISCHARGING */
+ set_charge_state(ST_DISCHARGING);
+ set_battery_soc(50);
+ msleep(30000);
+ TEST_ASSERT(verify_color(LED_COLOR_YELLOW));
+ set_battery_soc(99);
+ msleep(30000);
+ TEST_ASSERT(verify_color(LED_COLOR_GREEN));
+
+ /* CHARGING */
+ set_charge_state(ST_CHARGING);
+ set_battery_soc(50);
+ msleep(30000);
+ TEST_ASSERT(verify_color(LED_COLOR_YELLOW));
+ set_battery_soc(99);
+ msleep(30000);
+ TEST_ASSERT(verify_color(LED_COLOR_GREEN));
+
+ /* CHARGING_ERROR */
+ set_charge_state(ST_CHARGING_ERROR);
+ msleep(1500);
+ TEST_ASSERT(verify_color(LED_COLOR_RED));
+
+ return EC_SUCCESS;
+}
+
+static int test_green_yellow(void)
+{
+ /* Make LED green */
+ set_ac(1);
+ set_charge_state(ST_CHARGING);
+ set_battery_soc(95);
+ msleep(30000);
+ TEST_ASSERT(verify_color(LED_COLOR_GREEN));
+
+ /* Make it yellow now */
+ set_battery_soc(90);
+ msleep(1500);
+ TEST_ASSERT(verify_color(LED_COLOR_YELLOW));
+
+ /* Shouldn't change from yellow to green in 15 seconds */
+ set_battery_soc(95);
+ msleep(13000);
+ TEST_ASSERT(verify_color(LED_COLOR_YELLOW));
+
+ /* After 15 seconds, it should turn green */
+ msleep(3000);
+ TEST_ASSERT(verify_color(LED_COLOR_GREEN));
+
+ /* Shouldn't change from green to yellow in 15 seconds */
+ set_charge_state(ST_BAD_COND);
+ msleep(12000);
+ TEST_ASSERT(verify_color(LED_COLOR_GREEN));
+
+ /* After 15 seconds, it should turn yellow */
+ msleep(4000);
+ TEST_ASSERT(verify_color(LED_COLOR_YELLOW));
+
+ return EC_SUCCESS;
+}
+
+static int test_bad_i2c(void)
+{
+ /* Make LED green */
+ set_ac(1);
+ set_charge_state(ST_DISCHARGING);
+ set_battery_soc(95);
+ msleep(30000);
+ TEST_ASSERT(verify_color(LED_COLOR_GREEN));
+
+ /* Make it red, but fail the I2C write to green PWM register */
+ lp5562_failed_i2c_reg = LP5562_REG_G_PWM;
+ set_charge_state(ST_CHARGING_ERROR);
+ msleep(3000);
+ TEST_ASSERT(!verify_color(LED_COLOR_RED));
+
+ /* I2C works again. LED should turn red */
+ lp5562_failed_i2c_reg = -1;
+ msleep(1500);
+ TEST_ASSERT(verify_color(LED_COLOR_RED));
+
+ /* Make it green, but I2C fails again */
+ lp5562_failed_i2c_reg = LP5562_REG_R_PWM;
+ set_charge_state(ST_DISCHARGING);
+ msleep(1500);
+ TEST_ASSERT(!verify_color(LED_COLOR_GREEN));
+ TEST_ASSERT(!verify_color(LED_COLOR_RED));
+
+ /* I2C works now, but LED turns red at the same time */
+ lp5562_failed_i2c_reg = -1;
+ set_charge_state(ST_CHARGING_ERROR);
+ msleep(1500);
+ TEST_ASSERT(verify_color(LED_COLOR_RED));
+
+ return EC_SUCCESS;
+}
+
+void run_test(void)
+{
+ test_reset();
+
+ RUN_TEST(test_led_power);
+ RUN_TEST(test_led_color);
+ RUN_TEST(test_green_yellow);
+ RUN_TEST(test_bad_i2c);
+
+ test_print_result();
+}
diff --git a/test/led_lp5562.tasklist b/test/led_lp5562.tasklist
new file mode 100644
index 0000000000..26cfc53453
--- /dev/null
+++ b/test/led_lp5562.tasklist
@@ -0,0 +1,17 @@
+/* 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.
+ */
+
+/**
+ * List of enabled tasks in the priority order
+ *
+ * The first one has the lowest priority.
+ *
+ * For each task, use the macro TASK_TEST(n, r, d, s) where :
+ * 'n' in the name of the task
+ * 'r' in the main routine of the task
+ * 'd' in an opaque parameter passed to the routine at startup
+ * 's' is the stack size in bytes; must be a multiple of 8
+ */
+#define CONFIG_TEST_TASK_LIST /* No test task */
diff --git a/test/test_config.h b/test/test_config.h
index 28aedd1474..e848f94af0 100644
--- a/test/test_config.h
+++ b/test/test_config.h
@@ -8,11 +8,26 @@
#ifndef __CROS_EC_TEST_CONFIG_H
#define __CROS_EC_TEST_CONFIG_H
+#ifdef TEST_adapter
+#define CONFIG_CHIPSET_CAN_THROTTLE
+#define CONFIG_EXTPOWER_FALCO
+#endif
+
#ifdef TEST_kb_8042
#undef CONFIG_KEYBOARD_PROTOCOL_MKBP
#define CONFIG_KEYBOARD_PROTOCOL_8042
#endif
+#ifdef TEST_led_lp5562
+#define CONFIG_BATTERY_MOCK
+#define CONFIG_BATTERY_SMART
+#define CONFIG_CHARGER_INPUT_CURRENT 4032
+#define CONFIG_LED_DRIVER_LP5562
+#define I2C_PORT_HOST 1
+#define I2C_PORT_BATTERY 1
+#define I2C_PORT_CHARGER 1
+#endif
+
#ifdef TEST_sbs_charging
#define CONFIG_BATTERY_MOCK
#define CONFIG_BATTERY_SMART
@@ -25,11 +40,6 @@ int board_discharge_on_ac(int enabled);
#define I2C_PORT_CHARGER 1
#endif
-#ifdef TEST_adapter
-#define CONFIG_CHIPSET_CAN_THROTTLE
-#define CONFIG_EXTPOWER_FALCO
-#endif
-
#ifdef TEST_thermal
#define CONFIG_CHIPSET_CAN_THROTTLE
#define CONFIG_FAN