Trouble shutting down device and rebooting

I am having an issue where ONLY in release mode.

When charging the device through the USB port, if it is unplugged I want to put the device into sleep mode.  Following this the device becomes unresponsive - the power button doesn't work and the only way to get it to wake is by a pin reset.

In debugging I notice that when I call the following function, the log message "Shouldn't get here..." is shown, suggesting the device doesn't shut down immediately on "sd_power_system_off".  Why is that?  Is the shutdown somehow scheduled through the softdevice?

// NOTE Shutdown handler - called from scheduler to allow shutdown from main context.
static void _shutdown_handler(void * p_event_data, uint16_t event_size)
{
// Flush messages:
NRF_LOG_FLUSH();
// Switch off:
sd_power_system_off();
NRF_LOG_INFO("Shouldnt get here..");
NRF_LOG_FLUSH();
}

NRF52840, SDK17.0.2, SD S340

Parents
  • Hi,

    System OFF mode does not work when debugging, so in that case it is expected that sd_power_system_off() returns. This is for instance why the power management module in nrf_pwr_mgmt.c has this special handling for debug builds:

            // Enter System OFF.
    #ifdef SOFTDEVICE_PRESENT
            if (nrf_sdh_is_enabled())
            {
                ret_code_t ret_code = sd_power_system_off();
                ASSERT((ret_code == NRF_SUCCESS) || (ret_code == NRF_ERROR_SOFTDEVICE_NOT_ENABLED));
                UNUSED_VARIABLE(ret_code);
    #ifdef DEBUG
                while (true)
                {
                    /* Since the CPU is kept on in an emulated System OFF mode, it is recommended
                     * to add an infinite loop directly after entering System OFF, to prevent
                     * the CPU from executing code that normally should not be executed. */
                    __WFE();
    
                }
    #endif

    But for release builds (where DEBUG is not defined), execution would continue if you try to enter system off while debugging.

    Also, note that when the device enters system off mode, there are only a few possible wake-up sources, and except for pin reset, these needs to be explicitly enabled. For GPIO, you need to configure the sense mechanism for it to be able to wake the device up from system off, which you can do with nrf_gpio_cfg_sense_input().

Reply
  • Hi,

    System OFF mode does not work when debugging, so in that case it is expected that sd_power_system_off() returns. This is for instance why the power management module in nrf_pwr_mgmt.c has this special handling for debug builds:

            // Enter System OFF.
    #ifdef SOFTDEVICE_PRESENT
            if (nrf_sdh_is_enabled())
            {
                ret_code_t ret_code = sd_power_system_off();
                ASSERT((ret_code == NRF_SUCCESS) || (ret_code == NRF_ERROR_SOFTDEVICE_NOT_ENABLED));
                UNUSED_VARIABLE(ret_code);
    #ifdef DEBUG
                while (true)
                {
                    /* Since the CPU is kept on in an emulated System OFF mode, it is recommended
                     * to add an infinite loop directly after entering System OFF, to prevent
                     * the CPU from executing code that normally should not be executed. */
                    __WFE();
    
                }
    #endif

    But for release builds (where DEBUG is not defined), execution would continue if you try to enter system off while debugging.

    Also, note that when the device enters system off mode, there are only a few possible wake-up sources, and except for pin reset, these needs to be explicitly enabled. For GPIO, you need to configure the sense mechanism for it to be able to wake the device up from system off, which you can do with nrf_gpio_cfg_sense_input().

Children
  • If I run the device with "Release" compilation settings, and just look at the output with the j-link rtt viewer, does that count as debug mode?  This is the output.  Interestingly the "Shoudln't get here" timestamp is zero..

    [13391386] <info> velo_power: Shut down all...
    [13391393] <info> velo_power: Resetting GPIOS
    [13391403] <info> velo_power: Set GPIOs for wake...
    [13391413] <info> velo_power: Shut down all done...
    [13391418] <info> velo_driver: _shutdown done
    [00000000] <info> velo_power: Shouldnt get here..

    Fundamental question though, is should "sd_power_system_off()" immediately cause the system to power off?  

  • Hi,

    Aerosensor said:
    If I run the device with "Release" compilation settings, and just look at the output with the j-link rtt viewer, does that count as debug mode?

    You are using RTT logging and ahve the RTT viewer connected via SWD the debug interface is in use, and this prevents system off mode. So it is expected that sd_power_system_off() will return immediately. The device is not in a normal state though, but rather in emulated system off (see System OFF mode for more details).

    Aerosensor said:
    Fundamental question though, is should "sd_power_system_off()" immediately cause the system to power off?  

    If you are not debugging, then yes, it should make the system enter system off mode immediately. And wake-up from system off mode is always in form of a reset, so execution will never reach whatever is after the call to sd_power_system_off() (unless debugging, that is).

Related