-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
samples: boards: stm32: exit Poweroff using wkup pins & gpios
This sample demonstrates how gpio pins can be used to power on the system after a Poweroff, just like a reset pin. The user only needs to add the ST_STM32_GPIO_WKUP flag to the gpio pin in "gpios" property of a DT device such a button or a sensor. The samples Powers off the system after a delay then the user can power it on with the user button that has the ST_STM32_GPIO_WKUP flag in DT. Only works on STM32 boards that support Zephyr POWEROFF (U5, WL, WB, & L4 SoC series), & also have a user button whose GPIO pin is associated with a wake-up pin (see PWR node in SoC dtsi file). Signed-off-by: Abderrahmane Jarmouni <[email protected]>
- Loading branch information
1 parent
b6a57f2
commit e825ab6
Showing
9 changed files
with
205 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
cmake_minimum_required(VERSION 3.20.0) | ||
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) | ||
project(stm32_wkup_pins) | ||
|
||
target_sources(app PRIVATE src/main.c) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.. _gpio-as-a-wkup-pin-src-sample: | ||
|
||
GPIO As A Wake-up Pin Source | ||
############################ | ||
|
||
Overview | ||
******** | ||
|
||
This sample is a minimum application to demonstrate using a wake-up pin with a GPIO as | ||
a source to power on an STM32 SoC after Poweroff. | ||
|
||
The system will power off automatically ``WAIT_TIME_US`` us after boot. | ||
Press the user button designated in boards's devicetree overlay as "wkup-src" to power it on again. | ||
|
||
.. _gpio-as-a-wkup-pin-src-sample-requirements: | ||
|
||
Requirements | ||
************ | ||
|
||
The SoC should support POWEROFF functionality & have a wake-up pin that corresponds | ||
to the GPIO pin of a user button. | ||
To support another board, add an overlay in boards folder. | ||
Make sure that wake-up pins are configured in SoC dtsi file. | ||
|
||
Building and Running | ||
******************** | ||
|
||
Build and flash wkup_pins as follows, changing ``nucleo_u5a5zj_q`` for your board: | ||
|
||
.. zephyr-app-commands:: | ||
:zephyr-app: samples/boards/stm32/power_mgmt/wkup_pins | ||
:board: nucleo_u5a5zj_q | ||
:goals: build flash | ||
:compact: | ||
|
||
After flashing, the LED in ON. | ||
The LED will be turned off when the system is powered off. | ||
Press the user button to power on the system again. |
21 changes: 21 additions & 0 deletions
21
samples/boards/stm32/power_mgmt/wkup_pins/boards/b_u585i_iot02a.overlay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2024 STMicroelectronics | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/dt-bindings/gpio/st-stm32-gpio.h> | ||
|
||
/ { | ||
aliases { | ||
wkup-src = &user_button; | ||
}; | ||
}; | ||
|
||
&user_button { | ||
gpios = <&gpioc 13 (GPIO_ACTIVE_LOW | GPIO_PULL_UP | ST_STM32_GPIO_WKUP)>; | ||
}; | ||
|
||
&pwr { | ||
status = "okay"; | ||
}; |
21 changes: 21 additions & 0 deletions
21
samples/boards/stm32/power_mgmt/wkup_pins/boards/nucleo_l4r5zi.overlay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2024 STMicroelectronics | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/dt-bindings/gpio/st-stm32-gpio.h> | ||
|
||
/ { | ||
aliases { | ||
wkup-src = &user_button; | ||
}; | ||
}; | ||
|
||
&user_button { | ||
gpios = <&gpioc 13 (GPIO_ACTIVE_HIGH | ST_STM32_GPIO_WKUP)>; | ||
}; | ||
|
||
&pwr { | ||
status = "okay"; | ||
}; |
21 changes: 21 additions & 0 deletions
21
samples/boards/stm32/power_mgmt/wkup_pins/boards/nucleo_u5a5zj_q.overlay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2024 STMicroelectronics | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/dt-bindings/gpio/st-stm32-gpio.h> | ||
|
||
/ { | ||
aliases { | ||
wkup-src = &user_button; | ||
}; | ||
}; | ||
|
||
&user_button { | ||
gpios = <&gpioc 13 (GPIO_ACTIVE_HIGH | ST_STM32_GPIO_WKUP)>; | ||
}; | ||
|
||
&pwr { | ||
status = "okay"; | ||
}; |
21 changes: 21 additions & 0 deletions
21
samples/boards/stm32/power_mgmt/wkup_pins/boards/nucleo_wl55jc.overlay
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* | ||
* Copyright (c) 2024 STMicroelectronics | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/dt-bindings/gpio/st-stm32-gpio.h> | ||
|
||
/ { | ||
aliases { | ||
wkup-src = &user_button_1; | ||
}; | ||
}; | ||
|
||
&user_button_1 { | ||
gpios = <&gpioa 0 (GPIO_ACTIVE_LOW | GPIO_PULL_UP | ST_STM32_GPIO_WKUP)>; | ||
}; | ||
|
||
&pwr { | ||
status = "okay"; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CONFIG_PM=n | ||
CONFIG_PM_DEVICE=n | ||
CONFIG_PM_DEVICE_RUNTIME=n | ||
CONFIG_HWINFO=y | ||
CONFIG_POWEROFF=y | ||
CONFIG_STM32_WKUP_PINS=y | ||
CONFIG_INPUT=y | ||
CONFIG_INPUT_GPIO_KEYS=y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
sample: | ||
name: GPIO As A Wake-up Pin Source | ||
tests: | ||
sample.boards.stm32.power_mgmt.wkup_pins: | ||
build_only: true | ||
filter: dt_enabled_alias_with_parent_compat("wkup-src", | ||
"gpio-keys") and dt_compat_enabled("st,stm32-pwr") | ||
platform_allow: | ||
- nucleo_l4r5zi | ||
- nucleo_u5a5zj_q | ||
- nucleo_wl55jc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright (c) 2024 STMicroelectronics | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include <zephyr/drivers/hwinfo.h> | ||
#include <zephyr/kernel.h> | ||
#include <zephyr/device.h> | ||
#include <zephyr/devicetree.h> | ||
#include <zephyr/drivers/gpio.h> | ||
#include <zephyr/sys/printk.h> | ||
#include <zephyr/sys/poweroff.h> | ||
|
||
#define WAIT_TIME_US (1 << 22) | ||
|
||
#define WKUP_SRC_NODE DT_ALIAS(wkup_src) | ||
#if !DT_NODE_HAS_STATUS(WKUP_SRC_NODE, okay) | ||
#error "Unsupported board: wkup_src devicetree alias is not defined" | ||
#endif | ||
|
||
static const struct gpio_dt_spec button = GPIO_DT_SPEC_GET_OR(WKUP_SRC_NODE, gpios, {0}); | ||
|
||
static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios); | ||
|
||
int main(void) | ||
{ | ||
uint32_t cause; | ||
|
||
hwinfo_get_reset_cause(&cause); | ||
hwinfo_clear_reset_cause(); | ||
|
||
if (cause & RESET_BROWNOUT) { | ||
printk("\nReset cause: wake-up pin or reset pin\n\n"); | ||
} else if (cause & RESET_PIN) { | ||
printk("\nReset cause: Reset pin\n\n"); | ||
} | ||
|
||
printk("Wake-up button set up at %s pin %d\n", button.port->name, button.pin); | ||
|
||
__ASSERT_NO_MSG(gpio_is_ready_dt(&led)); | ||
gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); | ||
gpio_pin_set(led.port, led.pin, 1); | ||
|
||
printk("Device is ready\n"); | ||
|
||
printk("Will wait %d s before powering the system off\n", (WAIT_TIME_US >> 20)); | ||
k_busy_wait(WAIT_TIME_US); | ||
|
||
printk("Powering off\n"); | ||
printk("Press the user button to power the system on\n\n"); | ||
|
||
sys_poweroff(); | ||
/* Will remain powered off until wakeup pin is activated */ | ||
|
||
return 0; | ||
} |