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 <stm32u5xx_ll_pwr.h>
13 #include <gpio/gpio_stm32.h>
14 
15 #define SLEEP_TIME_MS   2000
16 
17 static const struct gpio_dt_spec led =
18 	GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios);
19 
main(void)20 int main(void)
21 {
22 	bool led_is_on = true;
23 
24 	/* Compute GPIO port to be passed to LL_PWR_EnableGPIOPullUp etc. */
25 	const struct gpio_stm32_config *cfg = led.port->config;
26 	uint32_t pwr_port = ((uint32_t)LL_PWR_GPIO_PORTA) + (cfg->port * 8);
27 
28 	__ASSERT_NO_MSG(gpio_is_ready_dt(&led));
29 
30 	printk("Device ready\n");
31 	gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE);
32 
33 	/* Enable the pull-up/pull-down feature globally.
34 	 * User can decide to not use this feature to further reduce power consumption
35 	 *
36 	 * This configuration is active only in STOP3 mode, so no need to disable it
37 	 * during wake up
38 	 */
39 	LL_PWR_EnablePUPDConfig();
40 
41 	while (true) {
42 		gpio_pin_set(led.port, led.pin, (int)led_is_on);
43 		/* In STOP3, GPIOs are disabled. Only pull-up/pull-down can be enabled.
44 		 * So we enable pull-up/pull-down based on LED status
45 		 */
46 		if (led_is_on) {
47 			LL_PWR_DisableGPIOPullDown(pwr_port, (1 << led.pin));
48 			LL_PWR_EnableGPIOPullUp(pwr_port, (1 << led.pin));
49 		} else {
50 			LL_PWR_DisableGPIOPullUp(pwr_port, (1 << led.pin));
51 			LL_PWR_EnableGPIOPullDown(pwr_port, (1 << led.pin));
52 		}
53 
54 		k_msleep(SLEEP_TIME_MS);
55 		led_is_on = !led_is_on;
56 	}
57 	return 0;
58 }
59