diff options
-rw-r--r-- | common/extpower_gpio.c | 19 | ||||
-rw-r--r-- | test/adapter.c | 16 |
2 files changed, 24 insertions, 11 deletions
diff --git a/common/extpower_gpio.c b/common/extpower_gpio.c index dccaed4958..7a5cb0f843 100644 --- a/common/extpower_gpio.c +++ b/common/extpower_gpio.c @@ -10,10 +10,15 @@ #include "gpio.h" #include "hooks.h" #include "host_command.h" +#include "timer.h" + +#define EXTPOWER_DEBOUNCE_US (30 * MSEC) + +static int debounced_extpower_presence; int extpower_is_present(void) { - return gpio_get_level(GPIO_AC_PRESENT); + return debounced_extpower_presence; } /** @@ -21,10 +26,16 @@ int extpower_is_present(void) */ static void extpower_deferred(void) { + int extpower_presence = gpio_get_level(GPIO_AC_PRESENT); + + if (extpower_presence == debounced_extpower_presence) + return; + + debounced_extpower_presence = extpower_presence; hook_notify(HOOK_AC_CHANGE); /* Forward notification to host */ - if (extpower_is_present()) + if (extpower_presence) host_set_single_event(EC_HOST_EVENT_AC_CONNECTED); else host_set_single_event(EC_HOST_EVENT_AC_DISCONNECTED); @@ -34,11 +45,13 @@ DECLARE_DEFERRED(extpower_deferred); void extpower_interrupt(enum gpio_signal signal) { /* Trigger deferred notification of external power change */ - hook_call_deferred(extpower_deferred, 0); + hook_call_deferred(extpower_deferred, EXTPOWER_DEBOUNCE_US); } static void extpower_init(void) { + debounced_extpower_presence = gpio_get_level(GPIO_AC_PRESENT); + /* Enable interrupts, now that we've initialized */ gpio_enable_interrupt(GPIO_AC_PRESENT); } diff --git a/test/adapter.c b/test/adapter.c index c1b9190d93..483dcf82ce 100644 --- a/test/adapter.c +++ b/test/adapter.c @@ -26,14 +26,6 @@ static int mock_id; static int mock_current; static struct charge_state_context ctx; -static void test_reset_mocks(void) -{ - gpio_set_level(GPIO_AC_PRESENT, 0); - mock_id = 0; - mock_current = 0; - memset(&ctx, 0, sizeof(ctx)); -} - /* Mocked functions from the rest of the EC */ int adc_read_channel(enum adc_channel ch) @@ -84,6 +76,14 @@ static void set_id(int val) mock_id = val; } +static void test_reset_mocks(void) +{ + change_ac(0); + set_id(0); + mock_current = 0; + memset(&ctx, 0, sizeof(ctx)); +} + /* Specify as discharge current */ static void mock_batt(int cur) { |