How to make more than one GPIO interrupt to work in a single module?

I am trying to configure interrupts for two push button (namely upbutton and dnbutton) on a custom board. In the main it is just looping and the module I have is as follows

/* Includes ------------------------------------------------------------------*/
#include <errno.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
#include <zephyr/device.h>
#include <zephyr/drivers/gpio.h>
#include <zephyr/drivers/pinctrl.h>
#include <hal/nrf_gpio.h>
#include <zephyr/pm/pm.h>
#include <zephyr/pm/policy.h>
#include <nrfx.h>
#include <hal/nrf_power.h>
#include <zephyr/drivers/spi.h>



/* Private includes ----------------------------------------------------------*/
#include "Push_Buttons.h"
#include "IO_Toggle.h"

/* Private macros ------------------------------------------------------------*/

LOG_MODULE_REGISTER(Push_Buttons,LOG_LEVEL_DBG);

/* Private defines -----------------------------------------------------------*/
/*
//      trigger button
#define TRIGBTN_NODE DT_PATH(io_controls, trigbtn)
#define TRIGBTN DT_GPIO_CTLR(TRIGBTN_NODE, gpios)
#define TRIGBTN_PIN DT_GPIO_PIN(TRIGBTN_NODE, gpios)
#define TRIGBTN_FLAGS DT_GPIO_FLAGS(TRIGBTN_NODE, gpios)*/


//      up button
#define UPBTN_NODE DT_PATH(io_controls, upbtn)
#define UPBTN DT_GPIO_CTLR(UPBTN_NODE, gpios)
#define UPBTN_PIN DT_GPIO_PIN(UPBTN_NODE, gpios)
#define UPBTN_FLAGS DT_GPIO_FLAGS(UPBTN_NODE, gpios)


//      down button
#define DNBTN_NODE DT_PATH(io_controls, dnbtn)
#define DNBTN DT_GPIO_CTLR(DNBTN_NODE, gpios)
#define DNBTN_PIN DT_GPIO_PIN(DNBTN_NODE, gpios)
#define DNBTN_FLAGS DT_GPIO_FLAGS(DNBTN_NODE, gpios)


/* Private variables ---------------------------------------------------------*/
//const struct device *trig_btn;
const struct device *up_btn;
const struct device *dn_btn;


/* Call back functions */
/*
void triggerbutton_press(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
		IO_TOGGLE_init();
	
};*/

void upbutton_press(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
		IO_TOGGLE_init();
	
};

void dnbutton_press(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
{
		IO_TOGGLE_init();
	
};


/* Define a variable of type static struct gpio_callback */
//static struct gpio_callback triggerbutton_cb_data;
static struct gpio_callback upbutton_cb_data;
static struct gpio_callback dnbutton_cb_data;

/* Functions Definition ------------------------------------------------------*/

int push_buttons_monitor(void){
LOG_DBG("push_buttons_monitor \r");

int ret = 0;
    /*
    /////////////    trigger
	trig_btn = DEVICE_DT_GET(TRIGBTN);
	if (!device_is_ready(trig_btn))
	{
		return -ENODEV;
	}
	ret = gpio_pin_configure(trig_btn, TRIGBTN_PIN, GPIO_INPUT | GPIO_PULL_UP);
	if (ret < 0)
	{
		return ret;
	}

    // Configure the interrupt on the  button to level high 
    ret = gpio_pin_interrupt_configure(trig_btn,TRIGBTN_PIN, GPIO_INT_LEVEL_ACTIVE); // this triggers the interrupt when the push butto is actually pushed.
    if (ret<0){                                                            // this flag on the other hand, GPIO_INT_LEVEL_HIGH will trigger the interrupt 
	return -5;                                                         // when the state of the push button is HIGH.  
    }

    // Initialize the gpio callback variable pin_cb_data using gpio_init_callback().
    gpio_init_callback(&triggerbutton_cb_data, triggerbutton_press, BIT(TRIGBTN_PIN));


    // The final step is to add the callback function through the function
    gpio_add_callback(trig_btn, &triggerbutton_cb_data);*/
    
    
    ///////////    UP
    up_btn = DEVICE_DT_GET(UPBTN);
	if (!device_is_ready(up_btn))
	{
		return -ENODEV;
	}
	ret = gpio_pin_configure(up_btn, UPBTN_PIN, GPIO_INPUT | GPIO_PULL_UP);
	if (ret < 0)
	{
		return ret;
	}

    ///////////    DOWN
    dn_btn = DEVICE_DT_GET(DNBTN);
	if (!device_is_ready(dn_btn))
	{
		return -ENODEV;
	}
	ret = gpio_pin_configure(dn_btn, DNBTN_PIN, GPIO_INPUT | GPIO_PULL_UP);
	if (ret < 0)
	{
		return ret;
	}

    // Configure the interrupt on the  button to level high 
    ret = gpio_pin_interrupt_configure(up_btn,UPBTN_PIN, GPIO_INT_LEVEL_ACTIVE); // this triggers the interrupt when the push butto is actually pushed.
    if (ret<0){                                                            // this flag on the other hand, GPIO_INT_LEVEL_HIGH will trigger the interrupt 
	return -5;                                                         // when the state of the push button is HIGH.  
    }

     // Configure the interrupt on the  button to level high 
    ret = gpio_pin_interrupt_configure(dn_btn,DNBTN_PIN, GPIO_INT_LEVEL_ACTIVE); // this triggers the interrupt when the push butto is actually pushed.
    if (ret<0){                                                            // this flag on the other hand, GPIO_INT_LEVEL_HIGH will trigger the interrupt 
	return -5;                                                         // when the state of the push button is HIGH.  
    }

      // Initialize the gpio callback variable pin_cb_data using gpio_init_callback().
    gpio_init_callback(&upbutton_cb_data, upbutton_press, BIT(UPBTN_PIN));

         // Initialize the gpio callback variable pin_cb_data using gpio_init_callback().
    gpio_init_callback(&dnbutton_cb_data, dnbutton_press, BIT(DNBTN_PIN));

    // The final step is to add the callback function through the function
    gpio_add_callback(up_btn, &upbutton_cb_data); 

    // The final step is to add the callback function through the function
    gpio_add_callback(dn_btn, &dnbutton_cb_data); 

   


    return 1;

}

As soon as I add the second push button interrupt non of them work. What could be the probelm?

  • As soon as I add the second push button interrupt non of them work. What could be the probelm?

    Likely the first or second interrupt continuously trigger due to the pin is continuously active, which prevent the application to continue to run. You can try to use GPIO_INT_EDGE_BOTH or GPIO_INT_EDGE_TO_ACTIVE instead of GPIO_INT_LEVEL_ACTIVE, or ensure that the pin doesn't stay active all the time.

    Kenneth

Related