This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

gpio button interrupt is not triggering?

Hi all,

I am using pca10040 board. My requirement is when i press the button led should turn on after repress the button led should turn off.

For that i tried below code :

#include <stdbool.h>
#include <stdint.h>
#include "pca10040.h"
#include "bsp.h"
#include "nordic_common.h"
#include "nrf_gpio.h"
#include "nrf_drv_clock.h"
#include "sdk_errors.h"
#include "app_error.h"
#include "app_uart.h"
#include "app_fifo.h"
#include "app_button.h"
#include "app_timer.h"
#include "nrf_delay.h"
#include "nrf_error.h"

#define APP_TIMER_PRESCALER 0
#define  APP_TIMER_OP_QUEUE_SIZE 6
#define BUTTON_DETECTION_DELAY        APP_TIMER_TICKS(500u, APP_TIMER_PRESCALER)
#define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 1                           /**< UART RX buffer size. */

void button_handler(uint8_t pin_no, uint8_t button_action)
{
    UNUSED_VARIABLE(puts("Enter \r\n"));
    nrf_gpio_pin_toggle(BSP_LED_1);
    if (button_action == APP_BUTTON_PUSH)
    {
        switch (pin_no)
        {
            case BSP_BUTTON_0:
                    UNUSED_VARIABLE(puts(" Buttons 0 \r\n"));
                    nrf_gpio_pin_toggle(BSP_LED_0);
                break;
          }
     }
}

void leds_init(void)
{
        UNUSED_VARIABLE(puts("LEDS Initialized \r\n"));
// Configure LED-pins as outputs
    nrf_gpio_cfg_output(BSP_LED_0);
    nrf_gpio_cfg_output(BSP_LED_1);
    nrf_gpio_cfg_output(BSP_LED_2);
    nrf_gpio_cfg_output(BSP_LED_3);
    nrf_gpio_pin_set(BSP_LED_0);
    nrf_gpio_pin_set(BSP_LED_1);
    nrf_gpio_pin_set(BSP_LED_2);
    nrf_gpio_pin_set(BSP_LED_3);
}

void uart_error_handle(app_uart_evt_t * p_event)
{
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_code);
    }
}

void uart_init(void)
{
    ret_code_t err_code;
    const app_uart_comm_params_t comm_params =
    {
        RX_PIN_NUMBER,
        TX_PIN_NUMBER,
        RTS_PIN_NUMBER,
        CTS_PIN_NUMBER,
        APP_UART_FLOW_CONTROL_ENABLED,
        false,
        UART_BAUDRATE_BAUDRATE_Baud38400
    };

    APP_UART_FIFO_INIT(&comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_error_handle,
                       APP_IRQ_PRIORITY_LOW,
                       err_code);

    APP_ERROR_CHECK(err_code);

    UNUSED_VARIABLE(puts(" UART Initialized \r\n"));
}
int main(void)
{
        ret_code_t err_code;
        err_code = nrf_drv_clock_init();
        APP_ERROR_CHECK(err_code);
        uart_init();
        leds_init();
        UNUSED_VARIABLE(puts(" Buttons Initialized \r\n"));
        app_button_cfg_t p_button[] = {{BUTTON_1, false, BUTTON_PULL, button_handler}};
        APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_OP_QUEUE_SIZE, NULL);
        // Initializing the buttons.
        err_code = app_button_init(p_button,1, BUTTON_DETECTION_DELAY);
        APP_ERROR_CHECK(err_code);
        // Enabling the buttons.
        err_code = app_button_enable();
        APP_ERROR_CHECK(err_code);
        while(1) {
            __WFE();
            __SEV();
            __WFE();
     }
}

Buttons Initialization is done. But button handler is not calling. In my console i'm getting prints like this :

 UART Initialized                       
                                        
LEDS Initialized                        
                                        
 Buttons Initialized 

After that its hanging there itself.

Why button handler is not called?

Im a newbie on this. Can anyone help me to fix this issue?

Thank you,

Parents
  • You need to define app_button_cfg_t p_button[] = {{BUTTON_1, false, BUTTON_PULL, button_handler}} as static or move it outside of main(). Or else it will be placed on the stack and removed when it is not used anymore. You are also toggling the led twice inside button_handler, remove one of the toggle functions.

Reply
  • You need to define app_button_cfg_t p_button[] = {{BUTTON_1, false, BUTTON_PULL, button_handler}} as static or move it outside of main(). Or else it will be placed on the stack and removed when it is not used anymore. You are also toggling the led twice inside button_handler, remove one of the toggle functions.

Children
Related