This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Preparing Wakeup Buttons in BSP causes Automatic Reset After Shutdown

Hi, 

I've been implementing the BSP functions on a 15 button custom device based off of the nRF52832 (in a BL652 Laird Module). At a minimum I need to be able to disconnect the BLE and sleep the device (System OFF Mode). Right now I have the BSP_EVENT_DISCONNECT working fine. This works after a 1 second button press. 

I'm having a lot of difficulty with the BSP_EVENT_SLEEP though. The only way I have been able to get this to work is through commenting out in my sleep_mode_enter() the bsp_btn_ble_sleep_mode_prepare(); This however doesn't allow a pin to be properly defined to wake-up the device, but all of the buttons wake it up. 

/**@brief Function for putting the chip into sleep mode.
 *
 * @note This function will not return.
 */
static void sleep_mode_enter(void)
{
    ret_code_t err_code;

    err_code = bsp_indication_set(BSP_INDICATE_IDLE);
    APP_ERROR_CHECK(err_code);

    // Prepare wakeup buttons.
    //err_code = bsp_btn_ble_sleep_mode_prepare();
    //APP_ERROR_CHECK(err_code);

    // Go to system-off mode (this function will not return; wakeup will cause a reset).
    err_code = sd_power_system_off();
    APP_ERROR_CHECK(err_code);

}

Every time I try to define a button to wake the device up from System OFF sleep on pressing it; when pressing the button associated with sleeping the device it immediately returns to a CPU on state pulling around 4 mA. The indication LED that I set up for the advertising mode will be blinking. If I comment out the above wakeup pin configuration the current drops to nA levels and the indication LED shuts off (as expected for sleep mode). 

See the following code blocks for the main.c and bsp_btn_ble.c file contents and how the wakeup pin and sleep modes are enabled. 

/**
 * Copyright (c) 2014 - 2018, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#include "bsp_btn_ble.h"
#include "nrf_sdh_ble.h"
#include "bsp.h"

#include "nrf_ble_qwr.h"

#define BTN_ID_WAKEUP             10  /**< ID of button used to wake up the application. */
#define BTN_ID_SLEEP              14  /**< ID of button used to put the application into sleep mode. */
#define BTN_ID_DISCONNECT         12  /**< ID of button used to gracefully terminate a connection on long press. */
#define BTN_ID_ADVERTISE          12  /**< ID of button used to start advertising. */
//#define BTN_ID_WAKEUP_BOND_DELETE 14  /**< ID of button used to wake up the application and delete all bonding information. */
//#define BTN_ID_WHITELIST_OFF      14  /**< ID of button used to turn off usage of the whitelist. */

#define BTN_ACTION_SLEEP          BSP_BUTTON_ACTION_RELEASE    /**< Button action used to put the application into sleep mode. */
#define BTN_ACTION_DISCONNECT     BSP_BUTTON_ACTION_LONG_PUSH  /**< Button action used to gracefully terminate a connection on long press. */
#define BTN_ACTION_ADVERTISE      BSP_BUTTON_ACTION_RELEASE    /**< Button action used to start advertising. */

//#define BTN_ACTION_WHITELIST_OFF  BSP_BUTTON_ACTION_LONG_PUSH  /**< Button action used to turn off usage of the whitelist. */

/**@brief This macro will return from the current function if err_code
 *        is not NRF_SUCCESS or NRF_ERROR_INVALID_PARAM.
 */
#define RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code)                             \
do                                                                              \
{                                                                               \
    if (((err_code) != NRF_SUCCESS) && ((err_code) != NRF_ERROR_INVALID_PARAM)) \
    {                                                                           \
        return err_code;                                                        \
    }                                                                           \
}                                                                               \
while (0)


/**@brief This macro will return from the current function if err_code
 *        is not NRF_SUCCESS or NRF_ERROR_NOT_SUPPORTED.
 */
#define RETURN_ON_ERROR_NOT_NOT_SUPPORTED(err_code)                             \
do                                                                              \
{                                                                               \
    if (((err_code) != NRF_SUCCESS) && ((err_code) != NRF_ERROR_NOT_SUPPORTED)) \
    {                                                                           \
        return err_code;                                                        \
    }                                                                           \
}                                                                               \
while (0)


/**@brief This macro will call the registered error handler if err_code
 *        is not NRF_SUCCESS and the error handler is not NULL.
 */
#define CALL_HANDLER_ON_ERROR(err_code)                           \
do                                                                \
{                                                                 \
    if (((err_code) != NRF_SUCCESS) && (m_error_handler != NULL)) \
    {                                                             \
        m_error_handler(err_code);                                \
    }                                                             \
}                                                                 \
while (0)


static bsp_btn_ble_error_handler_t m_error_handler = NULL; /**< Error handler registered by the user. */
static uint32_t                    m_num_connections = 0;  /**< Number of connections the device is currently in. */


/**@brief Function for configuring the buttons for connection.
 *
 * @retval NRF_SUCCESS  Configured successfully.
 * @return A propagated error code.
 */

uint32_t connection_buttons_configure()
{
    uint32_t err_code;

    err_code = bsp_event_to_button_action_assign(BTN_ID_SLEEP,
                                                 BTN_ACTION_SLEEP,
                                                 BSP_EVENT_SLEEP);
    RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);

    /*
    err_code = bsp_event_to_button_action_assign(BTN_ID_WHITELIST_OFF,
                                                 BTN_ACTION_WHITELIST_OFF,
                                                 BSP_EVENT_WHITELIST_OFF);
    RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);
    */

    err_code = bsp_event_to_button_action_assign(BTN_ID_DISCONNECT,
                                                 BTN_ACTION_DISCONNECT,
                                                 BSP_EVENT_DISCONNECT);
    RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);

    return NRF_SUCCESS;
}


/**@brief Function for configuring the buttons for advertisement.
 *
 * @retval NRF_SUCCESS  Configured successfully.
 * @return A propagated error code.
 */

uint32_t advertising_buttons_configure()
{
    uint32_t err_code;

    err_code = bsp_event_to_button_action_assign(BTN_ID_DISCONNECT,
                                                 BTN_ACTION_DISCONNECT,
                                                 BSP_EVENT_DEFAULT);
    RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);

    /*
    err_code = bsp_event_to_button_action_assign(BTN_ID_WHITELIST_OFF,
                                                 BTN_ACTION_WHITELIST_OFF,
                                                 BSP_EVENT_WHITELIST_OFF);
    RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);

    */

    err_code = bsp_event_to_button_action_assign(BTN_ID_ADVERTISE, 
                                                 BTN_ACTION_ADVERTISE,
                                                 BSP_EVENT_ADVERTISING_START);
    RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);

    err_code = bsp_event_to_button_action_assign(BTN_ID_SLEEP,
                                                 BTN_ACTION_SLEEP,
                                                 BSP_EVENT_SLEEP);
    RETURN_ON_ERROR_NOT_INVALID_PARAM(err_code);

    return NRF_SUCCESS;
}


/**@brief Function for extracting the BSP event valid at startup.
 *
 * @details When a button was used to wake up the device, the button press will not generate an
 *          interrupt. This function reads which button was pressed at startup, and returns the
 *          appropriate BSP event.
 *
 * @param[out] p_startup_event  Where to put the extracted BSP event.
 */

static void startup_event_extract(bsp_event_t * p_startup_event)
{
    // React to button states
    /*
    if (bsp_button_is_pressed(BTN_ID_WAKEUP_BOND_DELETE))
    {
        *p_startup_event = BSP_EVENT_CLEAR_BONDING_DATA;
    }
    */
    
    //else if (bsp_button_is_pressed(BTN_ID_WAKEUP))
    if (bsp_button_is_pressed(BTN_ID_WAKEUP))
    {
        *p_startup_event = BSP_EVENT_WAKEUP;
    }
    else
    {
        *p_startup_event = BSP_EVENT_NOTHING;
    }
}




uint32_t bsp_btn_ble_sleep_mode_prepare(void)
{
    uint32_t err_code;

    err_code = bsp_wakeup_button_enable(BTN_ID_WAKEUP);
    RETURN_ON_ERROR_NOT_NOT_SUPPORTED(err_code);

    //err_code = bsp_wakeup_button_enable(BTN_ID_WAKEUP_BOND_DELETE);
    //RETURN_ON_ERROR_NOT_NOT_SUPPORTED(err_code);

    return NRF_SUCCESS;
}

/**
 * @brief Function for handling BLE events.
 *
 * @param[in]   p_ble_evt       Event received from the BLE stack.
 * @param[in]   p_context       Context.
 */


static void ble_evt_handler(ble_evt_t const * p_ble_evt, void * p_context)
{
    uint32_t err_code;

    switch (p_ble_evt->header.evt_id)
    {
        case BLE_GAP_EVT_CONNECTED:
            if (m_num_connections == 0)
            {
                err_code = connection_buttons_configure();
                CALL_HANDLER_ON_ERROR(err_code);
            }

            m_num_connections++;

            for(int pinnum = 0; pinnum < 32; pinnum++) {


                uint32_t pin_cnf_settings = NRF_GPIO -> PIN_CNF[pinnum];
                printf("Pin %d = 0x%08x\r\n", pinnum, pin_cnf_settings);           

            }

            break;

        case BLE_GAP_EVT_DISCONNECTED:
            m_num_connections--;

            if (m_num_connections == 0)
            {
                err_code = advertising_buttons_configure();
                CALL_HANDLER_ON_ERROR(err_code);
            }
            break;

        default:
            break;
    }
}


NRF_SDH_BLE_OBSERVER(m_ble_observer, BSP_BTN_BLE_OBSERVER_PRIO, ble_evt_handler, NULL);


uint32_t bsp_btn_ble_init(bsp_btn_ble_error_handler_t error_handler, bsp_event_t * p_startup_bsp_evt)
{
    uint32_t err_code = NRF_SUCCESS;

    m_error_handler = error_handler;

    if (p_startup_bsp_evt != NULL)
    {
        startup_event_extract(p_startup_bsp_evt);
    }

    if (m_num_connections == 0)
    {
        err_code = advertising_buttons_configure();
    }

    return err_code;
}

/**@brief Function for handling events from the BSP module.
 *
 * @param[in]   event   Event generated when button is pressed.
 */
static void bsp_event_handler(bsp_event_t event)
{
    ret_code_t err_code;

    switch (event)
    {
        case BSP_EVENT_SLEEP:
            
            sleep_mode_enter();
            break; // BSP_EVENT_SLEEP

        case BSP_EVENT_DISCONNECT:
            err_code = sd_ble_gap_disconnect(m_conn_handle,
                                             BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
            if (err_code != NRF_ERROR_INVALID_STATE)
            {
                APP_ERROR_CHECK(err_code);
            }

            break; // BSP_EVENT_DISCONNECT
            
        default:
            break;
    }
}

Can anyone help with this?

Thanks!

Parents
  • Hi,

    Just to make sure, have you verified that the BUTTONS_ACTIVE_STATE value corresponds with your button circuitry? I.e.,  BUTTONS_ACTIVE_STATE ==0 if the button is active low and BUTTONS_ACTIVE_STATE ==1 if active high?

    That said, you don't have to re-configure the input if it's already configured with sense enabled which seems to be the case here. But you should do it for all of the other button inputs to avoid unintentional wakeups. nrf_gpio_cfg_default() can be used for this. It will configure pins back to the reset state (input, disconnect). 

Reply
  • Hi,

    Just to make sure, have you verified that the BUTTONS_ACTIVE_STATE value corresponds with your button circuitry? I.e.,  BUTTONS_ACTIVE_STATE ==0 if the button is active low and BUTTONS_ACTIVE_STATE ==1 if active high?

    That said, you don't have to re-configure the input if it's already configured with sense enabled which seems to be the case here. But you should do it for all of the other button inputs to avoid unintentional wakeups. nrf_gpio_cfg_default() can be used for this. It will configure pins back to the reset state (input, disconnect). 

Children
Related