Skip to content

Commit 8ce7266

Browse files
nordic-krchlstnl
authored andcommitted
samples: boards: nrf: system_off: Extend sample
Add option to not go to system off but instead go to sleep. Extending sample to use second button (Button 1). Sample goes to system off as before and when Button 0 is pressed it wakes up and goes to system off again. If Button 1 is pressed then it wakes up but goes to sleep again. Signed-off-by: Krzysztof Chruściński <[email protected]>
1 parent cd1717f commit 8ce7266

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

samples/boards/nordic/system_off/README.rst

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ Overview
88
********
99

1010
This sample can be used for basic power measurement and as an example of
11-
deep sleep on Nordic platforms.
11+
deep sleep on Nordic platforms. After pin reset application goes to system off.
12+
Pressing Button 0 wakes up the system and after printing messages goes again to
13+
system off. Pressing Button 1 wakes up the system but after printing messages
14+
system goes to sleep instead of system off.
1215

1316
RAM Retention
1417
=============

samples/boards/nordic/system_off/src/main.c

+32-2
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,34 @@
1515
#include <zephyr/pm/device.h>
1616
#include <zephyr/sys/poweroff.h>
1717
#include <zephyr/sys/util.h>
18+
#include <hal/nrf_gpio.h>
1819

1920
#if IS_ENABLED(CONFIG_GRTC_WAKEUP_ENABLE)
2021
#include <zephyr/drivers/timer/nrf_grtc_timer.h>
2122
#define DEEP_SLEEP_TIME_S 2
2223
#else
2324
static const struct gpio_dt_spec sw0 = GPIO_DT_SPEC_GET(DT_ALIAS(sw0), gpios);
25+
static const struct gpio_dt_spec sw1 = GPIO_DT_SPEC_GET(DT_ALIAS(sw1), gpios);
26+
static const uint32_t port_sw1 = DT_PROP(DT_GPIO_CTLR_BY_IDX(DT_ALIAS(sw1), gpios, 0), port);
2427
#endif
2528

2629
int main(void)
2730
{
2831
int rc;
2932
const struct device *const cons = DEVICE_DT_GET(DT_CHOSEN(zephyr_console));
33+
uint32_t nrf_pin_sw1 = 32 * port_sw1 + sw1.pin;
34+
bool do_poweroff = true;
3035

3136
if (!device_is_ready(cons)) {
3237
printf("%s: device not ready.\n", cons->name);
3338
return 0;
3439
}
3540

41+
if (nrf_gpio_pin_latch_get(nrf_pin_sw1)) {
42+
nrf_gpio_pin_latch_clear(nrf_pin_sw1);
43+
do_poweroff = false;
44+
}
45+
3646
printf("\n%s system off demo\n", CONFIG_BOARD);
3747

3848
if (IS_ENABLED(CONFIG_APP_USE_RETAINED_MEM)) {
@@ -66,13 +76,29 @@ int main(void)
6676
return 0;
6777
}
6878

79+
rc = gpio_pin_configure_dt(&sw1, GPIO_INPUT);
80+
if (rc < 0) {
81+
printf("Could not configure sw1 GPIO (%d)\n", rc);
82+
return 0;
83+
}
84+
6985
rc = gpio_pin_interrupt_configure_dt(&sw0, GPIO_INT_LEVEL_ACTIVE);
7086
if (rc < 0) {
7187
printf("Could not configure sw0 GPIO interrupt (%d)\n", rc);
7288
return 0;
7389
}
7490

75-
printf("Entering system off; press sw0 to restart\n");
91+
rc = gpio_pin_interrupt_configure_dt(&sw1, GPIO_INT_LEVEL_ACTIVE);
92+
if (rc < 0) {
93+
printf("Could not configure sw0 GPIO interrupt (%d)\n", rc);
94+
return 0;
95+
}
96+
97+
if (do_poweroff) {
98+
printf("Entering system off; press sw0 or sw1 to restart\n");
99+
} else {
100+
printf("Button sw1 pressed, not entering system off\n");
101+
}
76102
#endif
77103

78104
rc = pm_device_action_run(cons, PM_DEVICE_ACTION_SUSPEND);
@@ -87,7 +113,11 @@ int main(void)
87113
retained_update();
88114
}
89115

90-
sys_poweroff();
116+
if (do_poweroff) {
117+
sys_poweroff();
118+
} else {
119+
k_sleep(K_FOREVER);
120+
}
91121

92122
return 0;
93123
}

0 commit comments

Comments
 (0)