pm_state_force(0u, &(struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0}); Causing reboot

I have a callback for button and instead of going to sleep it reboots?

void wakeButtonPressed(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
	int ret;
	static int state = WAKE_STATE;		// Initial state is wake
	static uint32_t last_time = 0;		// Last time the button was pressed
	uint32_t now = k_uptime_get_32(); // Current time

	printk("%s", "Button pressed, going to sleep...\n");

	// If the button is pressed within the debounce time, ignore it
	if (now - last_time < 2000)
		return;

	last_time = now; // Update the last time

	if (state == WAKE_STATE)
	{
		k_timer_stop(&timer2);
		for (int i = 0; i < num_leds; i++)
		{
			if (&led[i] != NULL)
			{
				ret = gpio_pin_set_dt(&led[i], 0);
				if (ret < 0)
					return;
			}
		}

		k_busy_wait(1000000);
		k_sleep(K_MSEC(1500));
		// pm_system_suspend(0);
		// Check the validity of the GPIO pin
		int gpio_pin = NRF_DT_GPIOS_TO_PSEL(DT_ALIAS(sw1), gpios);
		if (gpio_pin >= 0)
		{
			nrf_gpio_cfg_input(gpio_pin, NRF_GPIO_PIN_PULLUP);
			nrf_gpio_cfg_sense_set(gpio_pin, NRF_GPIO_PIN_SENSE_LOW);
		}
		k_sleep(K_MSEC(1000));
		pm_state_force(0u, &(struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0});

		state = SLEEP_STATE;
		k_sleep(K_SECONDS(2U));

		printk("ERROR: System off failed\n");
		while (true)
		{
			/* spin to avoid fall-off behavior */
		}
	}
	else
	{
		printk("%s", "Button pressed, waking up...\n");
		state = WAKE_STATE;
	}
}

I used https://github.com/nrfconnect/sdk-zephyr/blob/main/samples/boards/nrf/system_off/src/main.c as an example 

Parents Reply Children
No Data
Related