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

Button Interrupt problem when booting

Dear,

I'm now face problem on button interrupt.

I use 3 buttons for my system.

button 2, 3 is used for boot diagnostics. assigned as below.

 bsp_event_to_button_action_assign(0,
                                             BSP_BUTTON_ACTION_PUSH,
                                             BSP_EVENT_KEY_0);

 bsp_event_to_button_action_assign(BTN_ID_U,
                                             BSP_BUTTON_ACTION_PUSH,
                                             BSP_EVENT_KEY_1);

 bsp_event_to_button_action_assign(BTN_ID_C,
                                             BSP_BUTTON_ACTION_PUSH,
                                             BSP_EVENT_KEY_2);


 bsp_event_to_button_action_assign(BTN_ID_U,
                                             BSP_BUTTON_ACTION_RELEASE,
                                             BSP_EVENT_KEY_1);
																							 
 bsp_event_to_button_action_assign(BTN_ID_C,
                                             BSP_BUTTON_ACTION_RELEASE,
                                             BSP_EVENT_KEY_2);

problem caused when boot up. I have to detect key_1 pressed status at boot up. It works fine.

But, key_1 release event I can't receive.

Please advised on this problem. I think it has some problem on interrupt handler when boot with button pressed status.

  • So BTN_ID_U is the number 2 or BUTTON_2 (which is actually the number 18)?

  • .

    #define BUTTON_2       7
    #define BUTTON_3       21
    

    so GPIO 7 & 21 used for button 2,3

  • The way the bsp module is made, button ID is not the same as the gpio number. Rather button ID 0 corresponds to BSP_BUTTON_0/BUTTON_1, ID 1 to BSP_BUTTON_1/BUTTON_2 and so on. So this should work:

    //BTN_ID_U corresponds to BUTTON_2 which equals BSP_BUTTON_1 which has ID 1
    bsp_event_to_button_action_assign(1,
                                                 BSP_BUTTON_ACTION_RELEASE,
                                                 BSP_EVENT_KEY_1);
    

    I would not recommend however to use the bsp module, use app_button instead.

    EDIT 11.12.2015: Here is some example code using app_button:

    #define NUM_OF_BUTTONS 2
    
    static void button_event_handler(uint8_t pin_no, uint8_t button_action)
    {
        //code run on button state change
    }
    
    //Configure 2 buttons with pullup and detection on low state
    static const app_button_cfg_t app_buttons[NUM_OF_BUTTONS] = 
    {
        {BUTTON_1, false, BUTTON_PULL, button_event_handler},
        {BUTTON_2, false, BUTTON_PULL, button_event_handler},
    };
    
    int main(void)
    {
        //start LFCLK, if SoftDevice is used this is started in sd_ble_enable
        NRF_CLOCK->LFCLKSRC            = (CLOCK_LFCLKSRC_SRC_Xtal << CLOCK_LFCLKSRC_SRC_Pos);
        NRF_CLOCK->EVENTS_LFCLKSTARTED = 0;
        NRF_CLOCK->TASKS_LFCLKSTART    = 1;
    
        while (NRF_CLOCK->EVENTS_LFCLKSTARTED == 0)
        {
            // Do nothing.
        }
       
        //app_button uses app_timer, if this is not initialize, then initialize it here
        APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, false);
    
        //init app_button module, 50ms detection delay (button debouncing)
        err_code = app_button_init((app_button_cfg_t *)app_buttons,
                                       NUM_OF_BUTTONS,
                                       APP_TIMER_TICKS(50, APP_TIMER_PRESCALER));
        APP_ERROR_CHECK(err_code);
    
        err_code = app_button_enable();
        APP_ERROR_CHECK(err_code);
    
        // Enter main loop.
        for (;;)
        {
    
        }
    }
    
Related