Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

applications: serial_lte_modem: Fix asserts when pressing power pin #21081

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 35 additions & 19 deletions applications/serial_lte_modem/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ BUILD_ASSERT(!IS_ENABLED(CONFIG_SLM_START_SLEEP),
static struct k_work_delayable indicate_work;

struct k_work_q slm_work_q;
static atomic_t callback_wakeup_running;

/* Forward declarations */
static void indicate_wk(struct k_work *work);
static void power_pin_callback_wakeup(const struct device *dev,
struct gpio_callback *gpio_callback, uint32_t);

NRF_MODEM_LIB_ON_INIT(lwm2m_init_hook, on_modem_lib_init, NULL);
NRF_MODEM_LIB_ON_DFU_RES(main_dfu_hook, on_modem_dfu_res, NULL);
Expand Down Expand Up @@ -149,19 +152,22 @@ static int configure_power_pin_interrupt(gpio_callback_handler_t handler, gpio_f
return err;
}

LOG_DBG("Configured interrupt (0x%x) on power pin (%u).", flags, pin);
LOG_DBG("Configured interrupt (0x%x) on power pin (%u) with handler (%p).",
flags, pin, handler);
return 0;
}

static void power_pin_callback_poweroff(const struct device *, struct gpio_callback *, uint32_t)
static void slm_enter_sleep_work_fn(struct k_work *)
{
LOG_INF("Power off triggered.");
slm_enter_sleep();
}

static void poweroff_interrupt_enabler(struct k_work *)
static void power_pin_callback_poweroff(const struct device *, struct gpio_callback *, uint32_t)
{
configure_power_pin_interrupt(power_pin_callback_poweroff, GPIO_INT_EDGE_RISING);
static K_WORK_DEFINE(work, slm_enter_sleep_work_fn);

LOG_INF("Power off triggered.");
k_work_submit(&work);
}

#endif /* POWER_PIN_IS_ENABLED */
Expand Down Expand Up @@ -237,25 +243,18 @@ static void indicate_stop(void)
static void power_pin_callback_enable_poweroff(const struct device *dev,
struct gpio_callback *gpio_callback, uint32_t)
{
static K_WORK_DELAYABLE_DEFINE(work, poweroff_interrupt_enabler);

LOG_DBG("Enabling the poweroff interrupt shortly...");
gpio_remove_callback(dev, gpio_callback);

/* Enable the poweroff interrupt after a small delay
* so that it doesn't fire right away (which it does if enabled here).
*/
k_work_schedule(&work, K_MSEC(1));
configure_power_pin_interrupt(power_pin_callback_poweroff, GPIO_INT_EDGE_RISING);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomi-font Do you remember what happened if GPIO was configured here? The comment says it would have fired immediately but I haven't experienced issues with this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this have been because we were level triggered at some point? Cannot recall exactly.

}

static void power_pin_callback_wakeup(const struct device *dev,
struct gpio_callback *gpio_callback, uint32_t)
static void power_pin_callback_wakeup_work_fn(struct k_work *)
{
static atomic_t callback_running;
int err;

/* Prevent level triggered interrupt running this multiple times. */
if (!atomic_cas(&callback_running, false, true)) {
if (!atomic_cas(&callback_wakeup_running, false, true)) {
return;
}

Expand All @@ -271,17 +270,34 @@ static void power_pin_callback_wakeup(const struct device *dev,
}
err = slm_at_host_power_on();
if (err) {
atomic_set(&callback_running, false);
/* We couldn't wake up SLM device. Re-enable wakeup functionality. */
configure_power_pin_interrupt(power_pin_callback_wakeup, GPIO_INT_LEVEL_LOW);
atomic_set(&callback_wakeup_running, false);
LOG_ERR("Failed to power on uart: %d", err);
return;
}

gpio_remove_callback(dev, gpio_callback);

/* Enable the poweroff interrupt only when the pin will be back to a nonactive state. */
configure_power_pin_interrupt(power_pin_callback_enable_poweroff, GPIO_INT_EDGE_RISING);

atomic_set(&callback_running, false);
atomic_set(&callback_wakeup_running, false);
}

static void power_pin_callback_wakeup(const struct device *dev,
struct gpio_callback *gpio_callback, uint32_t)
{
static K_WORK_DEFINE(work, power_pin_callback_wakeup_work_fn);

/* Prevent level triggered interrupt running this multiple times. */
if (!atomic_cas(&callback_wakeup_running, false, true)) {
LOG_WRN("power_pin_callback_wakeup_work_fn already running.");
return;
}

LOG_INF("Resuming from idle shortly...");
gpio_remove_callback(dev, gpio_callback);

k_work_submit(&work);
}

void slm_enter_idle(void)
Expand Down
24 changes: 22 additions & 2 deletions lib/modem_slm/modem_slm.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ static void gpio_wakeup_wk(struct k_work *work)
if (gpio_pin_set(gpio_dev, CONFIG_MODEM_SLM_WAKEUP_PIN, 0) != 0) {
LOG_WRN("GPIO set error");
}
LOG_DBG("Stop wake-up");
LOG_INF("Stop wake-up");
}

static void slm_data_wk(struct k_work *work)
Expand Down Expand Up @@ -525,6 +525,26 @@ int modem_slm_shell(const struct shell *shell, size_t argc, char **argv)
return modem_slm_send_cmd((char *)argv[1], 0);
}

SHELL_CMD_REGISTER(slm, NULL, "SLM Shell", modem_slm_shell);
int modem_slm_shell_slmsh_powerpin(const struct shell *shell, size_t argc, char **argv)
{
int err;

err = modem_slm_wake_up();
if (err) {
LOG_ERR("Failed to toggle power pin");
}
return 0;
}

SHELL_CMD_REGISTER(slm, NULL, "Send AT commands to SLM device", modem_slm_shell);

SHELL_STATIC_SUBCMD_SET_CREATE(
sub_slmsh,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've made slmsh as the main command for shell commands that are executed in the SLM shell device. Better naming for "SLM shell device" or for the command name are welcome.

SHELL_CMD(powerpin, NULL, "Toggle power pin configured with CONFIG_SLM_POWER_PIN",
modem_slm_shell_slmsh_powerpin),
SHELL_SUBCMD_SET_END
);

SHELL_CMD_REGISTER(slmsh, &sub_slmsh, "Commands handled in SLM shell device", NULL);

#endif /* CONFIG_MODEM_SLM_SHELL */