From 47b2f71b6f1ba21b0eed26e3008db6c2fd52787c Mon Sep 17 00:00:00 2001 From: Arnaud Ferraris Date: Thu, 10 Dec 2020 17:05:44 +0100 Subject: [PATCH] improve poweroff sequence When powering off the modem, we must assert the RESET line only when the modem has shut down (otherwise it'll cause a hard reset and won't allow the modem to shut down properly). This commit also polls the STATUS pin during poweroff and quits immediately once this pin goes high. --- src/gpio.c | 18 ++++++++++++++---- src/gpio.h | 2 ++ src/manager.c | 15 +++++++++++---- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/src/gpio.c b/src/gpio.c index d424b6e..95973e5 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -64,11 +64,8 @@ int gpio_sequence_poweron(struct EG25Manager *manager) int gpio_sequence_shutdown(struct EG25Manager *manager) { - gpiod_line_set_value(manager->gpio_out[GPIO_OUT_RESET], 1); gpiod_line_set_value(manager->gpio_out[GPIO_OUT_DISABLE], 1); - gpiod_line_set_value(manager->gpio_out[GPIO_OUT_PWRKEY], 1); - sleep(1); - gpiod_line_set_value(manager->gpio_out[GPIO_OUT_PWRKEY], 0); + gpio_sequence_poweron(manager); g_message("Executed power-off sequence"); @@ -175,6 +172,19 @@ int gpio_init(struct EG25Manager *manager) return 0; } +gboolean gpio_check_poweroff(struct EG25Manager *manager) +{ + if (manager->gpio_in[GPIO_IN_STATUS] && + gpiod_line_get_value(manager->gpio_in[GPIO_IN_STATUS]) == 1) { + + // Asserting RESET line to prevent modem from rebooting + gpiod_line_set_value(manager->gpio_out[GPIO_OUT_RESET], 1); + return TRUE; + } + + return FALSE; +} + void gpio_destroy(struct EG25Manager *manager) { int i; diff --git a/src/gpio.h b/src/gpio.h index 52291af..a31c1ce 100644 --- a/src/gpio.h +++ b/src/gpio.h @@ -15,3 +15,5 @@ int gpio_sequence_poweron(struct EG25Manager *state); int gpio_sequence_shutdown(struct EG25Manager *state); int gpio_sequence_suspend(struct EG25Manager *state); int gpio_sequence_resume(struct EG25Manager *state); + +gboolean gpio_check_poweroff(struct EG25Manager *manager); diff --git a/src/manager.c b/src/manager.c index ac04cad..f07c0e8 100644 --- a/src/manager.c +++ b/src/manager.c @@ -25,6 +25,16 @@ static gboolean quit_timeout_cb(struct EG25Manager *manager) return FALSE; } +static gboolean gpio_poll_cb(struct EG25Manager *manager) +{ + if (gpio_check_poweroff(manager)) { + quit_timeout_cb(manager); + return FALSE; + } + + return TRUE; +} + static gboolean quit_app(struct EG25Manager *manager) { g_message("Request to quit..."); @@ -33,10 +43,7 @@ static gboolean quit_app(struct EG25Manager *manager) g_message("Powering down the modem..."); gpio_sequence_shutdown(manager); manager->modem_state = EG25_STATE_FINISHING; - /* - * TODO: add a polling function to check STATUS and RI pins state - * (that way we could reduce the poweroff delay) - */ + g_timeout_add(500, G_SOURCE_FUNC(gpio_poll_cb), manager); g_timeout_add_seconds(30, G_SOURCE_FUNC(quit_timeout_cb), manager); }