Event handler of app_button isn't called

app_button_init()  and app_button_enable() returns NRF_SUCCESS. But event handler of app_button isn't called.

log messages of nRF52832 device is as follows.

00> <debug> app: app_button_init()       errCode=0x0
00> <debug> app: app_button_enable() errCode=0x0

Parents Reply Children
  • #include "app_button.h"
    #include "app_timer.h"
    #include "band_nctu.h"
    #include "button.h"
    #include "error.h"
    #include "nrf_drv_gpiote.h"
    #include "nrf_log.h"
    #include "timer.h"
    
    #define APP_BUTTON_UNKNOWN      2
    #define PIN_IN F1_BUTTON_1
    #define BUTTON_NUMBER           1
    #define BUTTON_DETECTION_DELAY  APP_TIMER_TICKS(50)    /**< Delay from a GPIOTE event until a button is reported as pushed (in number of timer ticks). */
    
    typedef enum {
        BUTTON_PRESS = 0,
        BUTTON_RELEASE
    } button_state_t;
    
    static bool _gButtonInitialized = false;
    static uint8_t _gButtonAction   = APP_BUTTON_UNKNOWN;
    
    void in_pin_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
        switch (action) {
            case NRF_GPIOTE_POLARITY_LOTOHI:
            {
                NRF_LOG_DEBUG("NRF_GPIOTE_POLARITY_LOTOHI\n");
            }
            break;
                
            case NRF_GPIOTE_POLARITY_HITOLO:
            {
                NRF_LOG_DEBUG("NRF_GPIOTE_POLARITY_HITOLO\n");
            }
            break;
            
            case NRF_GPIOTE_POLARITY_TOGGLE:
            {
                NRF_LOG_INFO("NRF_GPIOTE_POLARITY_TOGGLE\n");
                const uint32_t buttonState = nrf_gpio_pin_read(F1_BUTTON_1);
                if (buttonState == BUTTON_PRESS) {
                    NRF_LOG_INFO("button pressed\n");
                    advertisingStart();
                    timerButtonLongPressStart();
                    _gButtonAction = BUTTON_PRESS;
                } else {
                    NRF_LOG_INFO("button released\n");
                    _gButtonAction = BUTTON_RELEASE;
                }
                
            }
            break;
        }        
    }
    
    int buttonInit(void) {
        ret_code_t errCode;
        nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
        in_config.pull = NRF_GPIO_PIN_PULLUP;
    
        errCode = nrf_drv_gpiote_in_init(PIN_IN, &in_config, in_pin_handler);
        if (errCode != NRF_SUCCESS) {
            NRF_LOG_WARNING("nrf_drv_gpiote_in_init() failed, errCode=0x%x\n", errCode);
            return ERROR_BUTTON_INITIALIZE_FAIL;
        }    
    
        nrf_drv_gpiote_in_event_enable(PIN_IN, true);
    
        const uint32_t buttonState = nrf_gpio_pin_read(F1_BUTTON_1);
        if (buttonState == BUTTON_PRESS) {
            timerButtonLongPressStart();
            _gButtonAction = APP_BUTTON_PUSH;
        }   
    
        _gButtonInitialized = true;
        return 0;
    }
    
    #if 0
    void button_events_init(void) {
        nrf_gpio_cfg_input(BUTTON_1,NRF_GPIO_PIN_PULLUP);
        
        timerButtonScanStart();    
    }
    #endif
    
    

  • Hi 

    In your original question you mentioned that you use the app_button library, but your code seems to do everything through the GPIO and GPIOTE drivers?

    Is there any reason why you didn't stick with the app_button library?

    I will find some time later this week to test the code, but in the mean time I would strongly suggest you get a DK in order to facilitate the software development and debugging process. 

    Best regards
    Torbjørn

Related