diff --git a/common/inductive_charging.c b/common/inductive_charging.c index 3a2448933f..0021cc3ac6 100644 --- a/common/inductive_charging.c +++ b/common/inductive_charging.c @@ -9,24 +9,40 @@ #include "gpio.h" #include "hooks.h" #include "lid_switch.h" +#include "timer.h" void inductive_charging_interrupt(enum gpio_signal signal) { int charger_enabled = gpio_get_level(GPIO_BASE_CHG_VDD_EN); int charge_done = gpio_get_level(GPIO_CHARGE_DONE); + /* Always try to charge if the lid is just closed */ + if (signal == GPIO_LID_OPEN) + charge_done = 0; + if (!charger_enabled || charge_done) gpio_set_level(GPIO_CHARGE_EN, 0); else gpio_set_level(GPIO_CHARGE_EN, 1); } -static void inductive_charging_lid_update(void) +static void inductive_charging_deferred_update(void) { int lid_open = lid_is_open(); gpio_set_level(GPIO_BASE_CHG_VDD_EN, !lid_open); inductive_charging_interrupt(GPIO_LID_OPEN); } +DECLARE_DEFERRED(inductive_charging_deferred_update); + +static void inductive_charging_lid_update(void) +{ + /* + * When the lid close signal changes, the coils might still be + * unaligned. Delay here to give the coils time to align before + * we try to clear CHARGE_DONE. + */ + hook_call_deferred(inductive_charging_deferred_update, 5 * SECOND); +} DECLARE_HOOK(HOOK_LID_CHANGE, inductive_charging_lid_update, HOOK_PRIO_DEFAULT); static void inductive_charging_init(void) diff --git a/test/inductive_charging.c b/test/inductive_charging.c index 8a4a94df5f..9e9d1f8486 100644 --- a/test/inductive_charging.c +++ b/test/inductive_charging.c @@ -15,6 +15,9 @@ #include "timer.h" #include "util.h" +#define START_CHARGE_DELAY 5000 /* ms */ +#define TEST_CHECK_CHARGE_DELAY (START_CHARGE_DELAY + 500) /* ms */ + static void wait_for_lid_debounce(void) { while (lid_is_open() != gpio_get_level(GPIO_LID_OPEN)) @@ -34,13 +37,21 @@ static int test_lid(void) gpio_set_level(GPIO_CHARGE_DONE, 0); TEST_ASSERT(gpio_get_level(GPIO_BASE_CHG_VDD_EN) == 0); - /* Close the lid. Transmitter should be enabled. */ + /* + * Close the lid. The EC should wait for a second before + * enabling transmitter. + */ set_lid_open(0); + TEST_ASSERT(gpio_get_level(GPIO_BASE_CHG_VDD_EN) == 0); + msleep(TEST_CHECK_CHARGE_DELAY); + + /* Transmitter should now be enabled. */ TEST_ASSERT(gpio_get_level(GPIO_BASE_CHG_VDD_EN) == 1); TEST_ASSERT(gpio_get_level(GPIO_CHARGE_EN) == 1); /* Open the lid. Charging should stop. */ set_lid_open(1); + msleep(TEST_CHECK_CHARGE_DELAY); TEST_ASSERT(gpio_get_level(GPIO_BASE_CHG_VDD_EN) == 0); TEST_ASSERT(gpio_get_level(GPIO_CHARGE_EN) == 0); @@ -51,6 +62,7 @@ static int test_charge_done(void) { /* Close the lid to start charging */ set_lid_open(0); + msleep(TEST_CHECK_CHARGE_DELAY); TEST_ASSERT(gpio_get_level(GPIO_BASE_CHG_VDD_EN) == 1); TEST_ASSERT(gpio_get_level(GPIO_CHARGE_EN) == 1); @@ -71,12 +83,14 @@ static int test_lid_open_during_charging(void) { /* Close the lid. Start charging. */ set_lid_open(0); + msleep(TEST_CHECK_CHARGE_DELAY); gpio_set_level(GPIO_CHARGE_DONE, 0); TEST_ASSERT(gpio_get_level(GPIO_BASE_CHG_VDD_EN) == 1); TEST_ASSERT(gpio_get_level(GPIO_CHARGE_EN) == 1); /* Open the lid. Transmitter should be turned off. */ set_lid_open(1); + msleep(TEST_CHECK_CHARGE_DELAY); TEST_ASSERT(gpio_get_level(GPIO_BASE_CHG_VDD_EN) == 0); TEST_ASSERT(gpio_get_level(GPIO_CHARGE_EN) == 0); @@ -91,6 +105,30 @@ static int test_lid_open_during_charging(void) return EC_SUCCESS; } +static int test_clear_charge_done(void) +{ + /* Lid is open initially. CHARGE_DONE is set. */ + set_lid_open(1); + msleep(TEST_CHECK_CHARGE_DELAY); + gpio_set_level(GPIO_CHARGE_DONE, 1); + TEST_ASSERT(gpio_get_level(GPIO_BASE_CHG_VDD_EN) == 0); + TEST_ASSERT(gpio_get_level(GPIO_CHARGE_EN) == 0); + + /* Close the lid. Charging should start. */ + set_lid_open(0); + msleep(TEST_CHECK_CHARGE_DELAY); + TEST_ASSERT(gpio_get_level(GPIO_BASE_CHG_VDD_EN) == 1); + TEST_ASSERT(gpio_get_level(GPIO_CHARGE_EN) == 1); + gpio_set_level(GPIO_CHARGE_DONE, 0); + + /* Charge is done. */ + gpio_set_level(GPIO_CHARGE_DONE, 1); + TEST_ASSERT(gpio_get_level(GPIO_BASE_CHG_VDD_EN) == 1); + TEST_ASSERT(gpio_get_level(GPIO_CHARGE_EN) == 0); + + return EC_SUCCESS; +} + void run_test(void) { test_reset(); @@ -98,6 +136,7 @@ void run_test(void) RUN_TEST(test_lid); RUN_TEST(test_charge_done); RUN_TEST(test_lid_open_during_charging); + RUN_TEST(test_clear_charge_done); test_print_result(); }