1919#include <zephyr/sys/util.h>
2020
2121#define NON_WAKEUP_RESET_REASON (RESET_PIN | RESET_SOFTWARE | RESET_POR | RESET_DEBUG)
22+ #include <zephyr/pm/device_runtime.h>
23+ #include <hal/nrf_gpio.h>
24+ #include <hal/nrf_memconf.h>
2225
2326#if defined(CONFIG_GRTC_WAKEUP_ENABLE )
2427#include <zephyr/drivers/timer/nrf_grtc_timer.h>
@@ -31,12 +34,15 @@ static const struct gpio_dt_spec sw0 = GPIO_DT_SPEC_GET(DT_ALIAS(sw0), gpios);
3134static const struct device * comp_dev = DEVICE_DT_GET (DT_NODELABEL (comp ));
3235#endif
3336
37+ static const struct gpio_dt_spec sw1 = GPIO_DT_SPEC_GET (DT_ALIAS (sw1 ), gpios );
38+ static const uint32_t port_sw1 = DT_PROP (DT_GPIO_CTLR_BY_IDX (DT_ALIAS (sw1 ), gpios , 0 ), port );
39+
3440int print_reset_cause (uint32_t reset_cause )
3541{
3642 int32_t ret ;
3743 uint32_t supported ;
3844
39- ret = hwinfo_get_supported_reset_cause ((uint32_t * ) & supported );
45+ ret = hwinfo_get_supported_reset_cause ((uint32_t * )& supported );
4046
4147 if (ret || !(reset_cause & supported )) {
4248 return - ENOTSUP ;
@@ -60,16 +66,33 @@ int main(void)
6066 int rc ;
6167 uint32_t reset_cause ;
6268 const struct device * const cons = DEVICE_DT_GET (DT_CHOSEN (zephyr_console ));
69+ uint32_t nrf_pin_sw1 = 32 * port_sw1 + sw1 .pin ;
70+ bool do_poweroff = true;
6371
6472 if (!device_is_ready (cons )) {
6573 printf ("%s: device not ready.\n" , cons -> name );
6674 return 0 ;
6775 }
6876
77+ /* TODO: this is always set and locks entering power off after gpio wakeup */
78+ // if (nrf_gpio_pin_latch_get(nrf_pin_sw1)) {
79+ // nrf_gpio_pin_latch_clear(nrf_pin_sw1);
80+ // do_poweroff = false;
81+ // }
82+
6983 printf ("\n%s system off demo\n" , CONFIG_BOARD );
7084 hwinfo_get_reset_cause (& reset_cause );
7185 rc = print_reset_cause (reset_cause );
7286
87+ #if defined(CONFIG_SOC_NRF54H20_CPUAPP )
88+ /* Temporary set gpio default if sense is set, prevent 300uA additional current after wakeup */
89+ for (int i = 0 ; i < 12 ; i ++ ) {
90+ if (nrf_gpio_pin_sense_get (i ) != GPIO_PIN_CNF_SENSE_Disabled ) {
91+ nrf_gpio_cfg_default (i );
92+ }
93+ }
94+ #endif
95+
7396 if (rc < 0 ) {
7497 printf ("Reset cause not supported.\n" );
7598 return 0 ;
@@ -97,13 +120,16 @@ int main(void)
97120 printf ("Retained data not supported\n" );
98121 }
99122
123+
124+ k_sleep (K_MSEC (4000 ));
125+
100126#if defined(CONFIG_GRTC_WAKEUP_ENABLE )
101127 int err = z_nrf_grtc_wakeup_prepare (DEEP_SLEEP_TIME_S * USEC_PER_SEC );
102128
103129 if (err < 0 ) {
104- printk ("Unable to prepare GRTC as a wake up source (err = %d).\n" , err );
130+ printf ("Unable to prepare GRTC as a wake up source (err = %d).\n" , err );
105131 } else {
106- printk ("Entering system off; wait %u seconds to restart\n" , DEEP_SLEEP_TIME_S );
132+ printf ("Entering system off; wait %u seconds to restart\n" , DEEP_SLEEP_TIME_S );
107133 }
108134#endif
109135#if defined(CONFIG_GPIO_WAKEUP_ENABLE )
@@ -119,14 +145,29 @@ int main(void)
119145 printf ("Could not configure sw0 GPIO interrupt (%d)\n" , rc );
120146 return 0 ;
121147 }
122-
123- printf ("Entering system off; press sw0 to restart\n" );
124148#endif
125149#if defined(CONFIG_LPCOMP_WAKEUP_ENABLE )
126150 comparator_set_trigger (comp_dev , COMPARATOR_TRIGGER_BOTH_EDGES );
127151 comparator_trigger_is_pending (comp_dev );
128152 printf ("Entering system off; change signal level at comparator input to restart\n" );
129153#endif
154+ rc = gpio_pin_configure_dt (& sw1 , GPIO_INPUT );
155+ if (rc < 0 ) {
156+ printf ("Could not configure sw1 GPIO (%d)\n" , rc );
157+ return 0 ;
158+ }
159+
160+ rc = gpio_pin_interrupt_configure_dt (& sw1 , GPIO_INT_LEVEL_ACTIVE );
161+ if (rc < 0 ) {
162+ printf ("Could not configure sw0 GPIO interrupt (%d)\n" , rc );
163+ return 0 ;
164+ }
165+
166+ if (do_poweroff ) {
167+ printf ("Entering system off; press sw0 or sw1 to restart\n" );
168+ } else {
169+ printf ("Button sw1 pressed, not entering system off\n" );
170+ }
130171
131172 rc = pm_device_action_run (cons , PM_DEVICE_ACTION_SUSPEND );
132173 if (rc < 0 ) {
@@ -140,6 +181,18 @@ int main(void)
140181 retained_update ();
141182 }
142183
184+
185+ if (do_poweroff ) {
186+ #if CONFIG_SOC_NRF54H20_CPUAPP
187+ /* Local RAM0 (TCM) is currently not used so retention can be disabled. */
188+ nrf_memconf_ramblock_ret_mask_enable_set (NRF_MEMCONF , 0 , RAMBLOCK_RET_MASK , false);
189+ nrf_memconf_ramblock_ret_mask_enable_set (NRF_MEMCONF , 1 , RAMBLOCK_RET_MASK , false);
190+ #endif
191+ sys_poweroff ();
192+ } else {
193+ k_sleep (K_FOREVER );
194+ }
195+
143196 hwinfo_clear_reset_cause ();
144197 sys_poweroff ();
145198
0 commit comments