1 /*
2  * Copyright (c) 2024 STMicroelectronics
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/device.h>
9 #include <zephyr/devicetree.h>
10 #include <zephyr/drivers/gpio.h>
11 #include <zephyr/sys/printk.h>
12 #include <zephyr/sys/poweroff.h>
13 #include <zephyr/dt-bindings/gpio/stm32-gpio.h>
14 
15 #define WAIT_TIME_US 4000000
16 
17 #define WKUP_SRC_NODE DT_ALIAS(wkup_src)
18 #if !DT_NODE_HAS_STATUS_OKAY(WKUP_SRC_NODE)
19 #error "Unsupported board: wkup_src devicetree alias is not defined"
20 #endif
21 
22 static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET(WKUP_SRC_NODE, gpios);
23 
24 static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
25 
main(void)26 int main(void)
27 {
28 	printk("\nWake-up button is connected to %s pin %d\n", button.port->name, button.pin);
29 
30 	__ASSERT_NO_MSG(gpio_is_ready_dt(&led));
31 	gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
32 	gpio_pin_set(led.port, led.pin, 1);
33 
34 	/* Setup button GPIO pin as a source for exiting Poweroff */
35 	gpio_pin_configure_dt(&button, STM32_GPIO_WKUP);
36 
37 	printk("Will wait %ds before powering the system off\n", (WAIT_TIME_US / 1000000));
38 	k_busy_wait(WAIT_TIME_US);
39 
40 	printk("Powering off\n");
41 	printk("Press the user button to power the system on\n\n");
42 
43 	sys_poweroff();
44 	/* Will remain powered off until wake-up or reset button is pressed */
45 
46 	return 0;
47 }
48