<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Cannot wake up from System OFF</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/104093/cannot-wake-up-from-system-off</link><description>I want to control the device status System ON and OFF. 
 I&amp;#39;m working on nRF52 DK, nRF Connect SDK v2.4.2. 
 
 The buttons are initialized with dk_buttons_init(), and works well. 
 Then 
 
 can make the device System OFF. 
 But after that, pushing the</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 27 Sep 2023 00:40:49 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/104093/cannot-wake-up-from-system-off" /><item><title>RE: Cannot wake up from System OFF</title><link>https://devzone.nordicsemi.com/thread/447839?ContentTypeID=1</link><pubDate>Wed, 27 Sep 2023 00:40:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:0164409b-ae14-46a9-a688-c76e74101106</guid><dc:creator>hiro.maezawa</dc:creator><description>&lt;p&gt;Hi, Einar&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;Thank you for your test and kind explanation.&lt;/p&gt;
&lt;p&gt;It works.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot wake up from System OFF</title><link>https://devzone.nordicsemi.com/thread/447637?ContentTypeID=1</link><pubDate>Tue, 26 Sep 2023 07:50:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ce369240-600e-4ebe-b8b1-3caac4c109b7</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;It seems the issue is that you configure the wakeup button too early so that the configuration register gets overwritten at a later point. You can see this by debugging and setting a breakpoint before going to system off. The CNF registe for P0.13 (button 0), should look like this with sense enabled:&lt;/p&gt;
&lt;p&gt;&lt;img style="max-height:240px;max-width:320px;" alt=" " src="https://devzone.nordicsemi.com/resized-image/__size/640x480/__key/communityserver-discussions-components-files/4/5466.button_5F00_0_5F00_sense.png" /&gt;&lt;/p&gt;
&lt;p&gt;But when I test your application, it is not. The reason turns out to be that in the implementation of the DK buttons and LEDs library, interrupts are disabled, and with that, sense is also disabled (this happens in the static button_pressed() function in&amp;nbsp;dk_buttons_and_leds.c). So you should enable sense as the last thing you do before entering system off mode, moving the two relevant lines to right before the call to&amp;nbsp;pm_state_force().&lt;/p&gt;
&lt;p&gt;Note that even with this change there is a theoretical chance for getting a button interrupt between enabling sense and entering system off mode, so it could be good to disable interrupt with&amp;nbsp;irq_lock() before enabling sense and entering system off. This way there is no risk of getting the wakeup configuration modified by the button library (or anything else) before entering system OFF.&lt;/p&gt;
&lt;p&gt;Therefore I suggest you change your code slightly and enter system OFF doing something like this:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;/* Lock interrupts to prevent anything else from happening when preparing for system OFF. */
int key = irq_lock();

/* Configure to generate PORT event (wakeup) on button 1 press. */

nrf_gpio_cfg_input(NRF_DT_GPIOS_TO_PSEL(DT_ALIAS(sw0), gpios), NRF_GPIO_PIN_PULLUP);
nrf_gpio_cfg_sense_set(NRF_DT_GPIOS_TO_PSEL(DT_ALIAS(sw0), gpios), NRF_GPIO_PIN_SENSE_LOW);

/* Enter system OFF. On success, pm_state_force will not return() */
bool rc = pm_state_force(0u, &amp;amp;(struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0});

/* Unlock IRQ (Though execution should never reach here unless there is an error) */
irq_unlock(key);

if (!rc) {
    printk(&amp;quot;Fail to enter system OFF.\n&amp;quot;);
}&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot wake up from System OFF</title><link>https://devzone.nordicsemi.com/thread/447589?ContentTypeID=1</link><pubDate>Mon, 25 Sep 2023 23:24:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:524c3791-40c6-4e72-bd35-a122b91278a2</guid><dc:creator>hiro.maezawa</dc:creator><description>&lt;p&gt;Hi, Einar&lt;/p&gt;
&lt;p&gt;Thanks for your reply.&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;This is my source code, main.c;&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;zephyr/kernel.h&amp;gt;
#include &amp;lt;dk_buttons_and_leds.h&amp;gt;        /* Include DK library for buttons and LEDs */
#include &amp;lt;zephyr/pm/pm.h&amp;gt;
#include &amp;lt;zephyr/pm/device.h&amp;gt;
#include &amp;lt;zephyr/pm/policy.h&amp;gt;
#include &amp;lt;hal/nrf_gpio.h&amp;gt;

#define SLEEP_TIME_MS 1000      // 1000 ms = 1 sec

// LED Control Flags
#define LED1    0
#define LED2    1
#define LED3    2
#define LED4    3
#define LED_CONTROL_STATUS_MASK         0x01
#define LED_CONTROL_BLINKY_MASK         0x04
#define LED_CONTROL_BLINK_STATUS_MASK   0x08
#define LED_CONTROL_LIGHT_ON_DEFAULT    850     // ms
#define LED_CONTROL_LIGHT_OFF_DEFAULT   650     // ms

typedef struct {
    uint8_t status;                 // Status flags
    uint16_t light_on_interval_ms;  // interval time at blinking light on (ms)
    uint16_t light_off_interval_ms; // interval time at blinking light off (ms)
} LED_control_information_t;

static LED_control_information_t led_ctl_info;

void LED_lighting(uint8_t led_idx, bool led_on)
{
    if (led_on) {
        // Turn a LED on.
        dk_set_led_on(led_idx);
    } else {
        // Turn a LED off.
        dk_set_led_off(led_idx);
    }
}

void button_changed(uint32_t button_state, uint32_t has_changed)
{
      if (has_changed &amp;amp; 0x01) {
        // Button 0
        if (button_state &amp;amp; has_changed) {

            if ( (led_ctl_info.status &amp;amp; LED_CONTROL_STATUS_MASK) == LED_CONTROL_STATUS_MASK ) {
                // LED off
                LED_lighting(LED1, false);

                // stop blinky
                led_ctl_info.status ^= LED_CONTROL_BLINKY_MASK;
                led_ctl_info.status ^= LED_CONTROL_BLINK_STATUS_MASK;

                led_ctl_info.status ^= LED_CONTROL_STATUS_MASK;

                // Syste OFF
                // Force usae of given power state.
                // This function overrides decision made by PM policy forcing usage of given power state upon next entry of the idle thread.
                // Parameters: CPU index = 0, Power state: substate_id = PM_STATE_SOFT_OFF, min_residency_us = 0, exit_latency_us = 0 
                bool rc = pm_state_force(0u, &amp;amp;(struct pm_state_info){PM_STATE_SOFT_OFF, 0, 0});
                if (!rc) {
                    printk(&amp;quot;Fail to system off \n&amp;quot;);
                }
                k_msleep(SLEEP_TIME_MS);
                LED_lighting(LED3, true);
                
            }
            else {
                // LED on
                LED_lighting(LED1, true);

                // start blinky
                led_ctl_info.status |= LED_CONTROL_BLINKY_MASK;
                led_ctl_info.status |= LED_CONTROL_BLINK_STATUS_MASK;

                led_ctl_info.status |= LED_CONTROL_STATUS_MASK;
            }
        }
        else {
            // button released
        }
      }
}


int main(void)
{
        int err = 0;

        // RTT console
        printk(&amp;quot;Hello World! %s\n&amp;quot;, CONFIG_BOARD);

        // Buttons
        err = dk_buttons_init(button_changed);
        if (err) {
            printk(&amp;quot;Cannot init buttons (err: %d)\n&amp;quot;, err);
        }

	/* Configure to generate PORT event (wakeup) on button 1 press. */
	nrf_gpio_cfg_input(NRF_DT_GPIOS_TO_PSEL(DT_ALIAS(sw0), gpios), NRF_GPIO_PIN_PULLUP);
	nrf_gpio_cfg_sense_set(NRF_DT_GPIOS_TO_PSEL(DT_ALIAS(sw0), gpios), NRF_GPIO_PIN_SENSE_LOW);

        // LEDs
        err = dk_leds_init(); 
        if (err) {
            printk(&amp;quot;Cannot init leds (err: %d)\n&amp;quot;, err);
        }
        led_ctl_info.light_on_interval_ms = LED_CONTROL_LIGHT_ON_DEFAULT;
        led_ctl_info.light_off_interval_ms = LED_CONTROL_LIGHT_OFF_DEFAULT;

        // Power on
        LED_lighting(LED2, true);
        LED_lighting(LED4, true);
        k_msleep(SLEEP_TIME_MS);
        LED_lighting(LED4, false);


        while(1) {
            if ( (led_ctl_info.status &amp;amp; LED_CONTROL_BLINKY_MASK) == LED_CONTROL_BLINKY_MASK ) {
                if ( (led_ctl_info.status &amp;amp; LED_CONTROL_BLINK_STATUS_MASK) == LED_CONTROL_BLINK_STATUS_MASK ) {
                    // blinking light off
                    led_ctl_info.status ^= LED_CONTROL_BLINK_STATUS_MASK;
                    LED_lighting(LED1, false);

                    k_msleep(led_ctl_info.light_off_interval_ms);
                } else {
                    // blinking light on
                    led_ctl_info.status |= LED_CONTROL_BLINK_STATUS_MASK;
                    LED_lighting(LED1, true);

                    k_msleep(led_ctl_info.light_on_interval_ms);
                }
            } else {
                k_msleep(SLEEP_TIME_MS);
            }
        }
        return 0;
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;and proj.conf&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;# Enable RTT console 
CONFIG_RTT_CONSOLE=y
CONFIG_UART_CONSOLE=n
# CONFIG_USE_SEGGER_RTT=y  // defined in nrf52dk_nrf52832_defconfig

# Enable DK Library (Buttons and LEDs)
CONFIG_DK_LIBRARY=y

# Enable power management
CONFIG_PM=y
CONFIG_PM_DEVICE=y
# CONFIG_POWEROFF=y&lt;/pre&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Cannot wake up from System OFF</title><link>https://devzone.nordicsemi.com/thread/447483?ContentTypeID=1</link><pubDate>Mon, 25 Sep 2023 12:33:26 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:723c203f-e0e9-40c4-bb4b-a03f19f506d2</guid><dc:creator>Einar Thorsrud</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;The two lines you have&amp;nbsp;there is what you need for configuring the wakeup-pin. I tested now, and this still works on my end when I add the DK library and initialize the buttons with&amp;nbsp;dk_buttons_init() beforehand. Can you share your project so that I can test on my end?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>