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

Button initial push shows up as button hold while sleeping

Hello all,

I'm using the Nordic SDK 14.2 BSP button handler.  I have a push, release, and long push event on a single button.  These are working as expected -

Except when the device goes to sleep.  I have a push-hold that does the following:

   1.  Turns off LCD
   2.  Turns off BLE advertising
   3.  Turns off a monitoring device
Then, the main loop sends the processor back to sleep as usual via sd_app_evt_wait().

When it is sleeping in this condition, a button hold (2 seconds) wakes the processor back up and it starts BLE advertising again.

If it sleeps long enough (several minutes), the button hold does not work properly.  It triggers a long hold immediately upon pressing the button.

Any ideas?  I checked whether sd_app_evt_wait() is ever waking up in the "sleep" state and it is not.  Perhaps app_timer is elapsing or not restarting properly?

Thanks!

  John

  • Hi,

    So the problem is that the BLE advertising is not starting again, because the action is 'push and not 'long push'?

    Is the device restarting?

    Maybe you are entering system off mode? Are you calinig sleep_mode_enter() or sd_power_system_off() somewhere?

  • Hi Sigurd,

    Thanks for your reply.  I have not called sleep_mode_enter() or sd_power_system_off() at any point.

    I don't think the device is restarting but I will check.  It is in fact starting up; the problem is that it does not wait for a long push - it starts immediately after touching the button. This generates a button press event, which is erroneously marked as a long push.

    Also to clarify, it usually works properly.  Occasionally, however, after being in standby (sd_app_evt_wait()) long enough the event occurs.

  • Hi Sigurd,

    I just confirmed that the device is NOT restarting.

    Best regards,

     -   John

  • This generates a button press event, which is erroneously marked as a long push.

    How are you testing and verifying this? Could you post some code?

    How have you configured the BSP ?

    Did you do something like this?

        // Generate event BSP_EVENT_ADVERTISING_START on long push for button 2(id=1)
        err_code = bsp_event_to_button_action_assign(1,BSP_BUTTON_ACTION_LONG_PUSH,BSP_EVENT_ADVERTISING_START);
        APP_ERROR_CHECK(err_code);

    And added event check for it in your bsp_event_handler() ?

  • Hi Sigurd,

    Here's the psuedo-code.  Is this what you need to see for the bsp?

    I'm testing the button push type by toggling an output pin and observing on an oscilloscope (one pulse / two pulses / three pulses differentiates button press events).

    //Main loop
    main
    {
        //BSP_INIT_BUTTONS = 2
        bsp_init(BSP_INIT_BUTTONS, BUTTON_SetButtonType);
    
        for (;;)
        {
            HandleButtons();
            //Do other things
            uint32_t err_code = sd_app_evt_wait();
            APP_ERROR_CHECK(err_code);
        }
    }
    
    //Button action callback
    void BUTTON_SetButtonType(bsp_event_t event)
    {
        switch (event)
        {
            case BSP_EVENT_KEY_0:       // button short push
            {
              BUTTON_ButtonType = BUTTON_MOMENTARY;
              BUTTON_SetButtonActive(); //flag that button event occurred
              //TOGGLE DEBUG PIN HERE
              break;
            }
            case BSP_EVENT_KEY_1:       // button long push
            {
              BUTTON_ButtonType = BUTTON_HOLD;
              BUTTON_SetButtonActive(); //flag that button event occurred
              //TOGGLE DEBUG PIN HERE
              break;
            }
            default:       // Release, other
            {
              BUTTON_ButtonType = BUTTON_NONE;
              BUTTON_SetButtonActive(); //flag that button event occurred
              //TOGGLE DEBUG PIN HERE
              break;
            }
        }
    
    }
    
    static const app_button_cfg_t app_buttons[1] =
    {
        {BSP_BUTTON_0, false, BSP_BTP0, bsp_button_event_handler}
    }
    
    uint32_t bsp_init(uint32_t type, bsp_event_callback_t callback)
    {
        uint32_t err_code = NRF_SUCCESS;
    
        err_code = bsp_event_to_button_action_assign(0, BSP_BUTTON_ACTION_PUSH, BSP_EVENT_KEY_2);
        APP_ERROR_CHECK(err_code);
    
        err_code = bsp_event_to_button_action_assign(0, BSP_BUTTON_ACTION_RELEASE, BSP_EVENT_KEY_0);
        APP_ERROR_CHECK(err_code);
    
        err_code = bsp_event_to_button_action_assign(0, BSP_BUTTON_ACTION_LONG_PUSH, BSP_EVENT_KEY_1);
        APP_ERROR_CHECK(err_code);
        
        if (err_code == NRF_SUCCESS)
        {
            bsp_board_buttons_init();
            err_code = app_button_init((app_button_cfg_t *)app_buttons,
                                       1, APP_TIMER_TICKS(50));
        }
        if (err_code == NRF_SUCCESS)
        {
            err_code = app_button_enable();
        }
        if (err_code == NRF_SUCCESS)
        {
            err_code = app_timer_create(&m_button_timer_id,
                                        APP_TIMER_MODE_SINGLE_SHOT,
                                        button_timer_handler);
        }
        return err_code;
    }
    

    Also, within the HandleButtons() function, the BLE advertising is enabled/disabled upon a button hold event.

    ble_advertising_start(&m_advertising, BLE_ADV_MODE_FAST);  //Start advertising
    
    sd_ble_gap_adv_stop();  //Stop advertising

Related