2828static const struct gpio_dt_spec sw0 = GPIO_DT_SPEC_GET (DT_ALIAS (sw0 ), gpios );
2929#endif
3030#if defined(CONFIG_LPCOMP_WAKEUP_ENABLE )
31+ static const uint32_t port_sw1 = DT_PROP (DT_GPIO_CTLR_BY_IDX (DT_ALIAS (sw1 ), gpios , 0 ), port );
3132static const struct device * comp_dev = DEVICE_DT_GET (DT_NODELABEL (comp ));
3233#endif
3334
@@ -60,12 +61,19 @@ int main(void)
6061 int rc ;
6162 uint32_t reset_cause ;
6263 const struct device * const cons = DEVICE_DT_GET (DT_CHOSEN (zephyr_console ));
64+ uint32_t nrf_pin_sw1 = 32 * port_sw1 + sw1 .pin ;
65+ bool do_poweroff = true;
6366
6467 if (!device_is_ready (cons )) {
6568 printf ("%s: device not ready.\n" , cons -> name );
6669 return 0 ;
6770 }
6871
72+ if (nrf_gpio_pin_latch_get (nrf_pin_sw1 )) {
73+ nrf_gpio_pin_latch_clear (nrf_pin_sw1 );
74+ do_poweroff = false;
75+ }
76+
6977 printf ("\n%s system off demo\n" , CONFIG_BOARD );
7078 hwinfo_get_reset_cause (& reset_cause );
7179 rc = print_reset_cause (reset_cause );
@@ -114,20 +122,37 @@ int main(void)
114122 return 0 ;
115123 }
116124
125+ rc = gpio_pin_configure_dt (& sw1 , GPIO_INPUT );
126+ if (rc < 0 ) {
127+ printf ("Could not configure sw1 GPIO (%d)\n" , rc );
128+ return 0 ;
129+ }
130+
117131 rc = gpio_pin_interrupt_configure_dt (& sw0 , GPIO_INT_LEVEL_ACTIVE );
118132 if (rc < 0 ) {
119133 printf ("Could not configure sw0 GPIO interrupt (%d)\n" , rc );
120134 return 0 ;
121135 }
122-
123- printf ("Entering system off; press sw0 to restart\n" );
136+ //code reorder needed
124137#endif
125138#if defined(CONFIG_LPCOMP_WAKEUP_ENABLE )
126139 comparator_set_trigger (comp_dev , COMPARATOR_TRIGGER_BOTH_EDGES );
127140 comparator_trigger_is_pending (comp_dev );
128141 printf ("Entering system off; change signal level at comparator input to restart\n" );
129142#endif
130143
144+ rc = gpio_pin_interrupt_configure_dt (& sw1 , GPIO_INT_LEVEL_ACTIVE );
145+ if (rc < 0 ) {
146+ printf ("Could not configure sw0 GPIO interrupt (%d)\n" , rc );
147+ return 0 ;
148+ }
149+
150+ if (do_poweroff ) {
151+ printf ("Entering system off; press sw0 or sw1 to restart\n" );
152+ } else {
153+ printf ("Button sw1 pressed, not entering system off\n" );
154+ }
155+
131156 rc = pm_device_action_run (cons , PM_DEVICE_ACTION_SUSPEND );
132157 if (rc < 0 ) {
133158 printf ("Could not suspend console (%d)\n" , rc );
@@ -140,6 +165,17 @@ int main(void)
140165 retained_update ();
141166 }
142167
168+ if (do_poweroff ) {
169+ #if CONFIG_SOC_NRF54H20_CPUAPP
170+ /* Local RAM0 (TCM) is currently not used so retention can be disabled. */
171+ nrf_memconf_ramblock_ret_mask_enable_set (NRF_MEMCONF , 0 , RAMBLOCK_RET_MASK , false);
172+ nrf_memconf_ramblock_ret_mask_enable_set (NRF_MEMCONF , 1 , RAMBLOCK_RET_MASK , false);
173+ #endif
174+ sys_poweroff ();
175+ } else {
176+ k_sleep (K_FOREVER );
177+ }
178+
143179 hwinfo_clear_reset_cause ();
144180 sys_poweroff ();
145181
0 commit comments