Interrupt handling example for nRF52833 with nRF Connect SDK 2.3.0

Dear Support Team,

I just started evaluating a nRF52833 DK board with the nRF Connect SDK 2.3.0. At this point I would like to try interrupt handling from a GPIO pin but unfortunately I can't find any working example that I could use.

Ultimately I would like achive that the nRF52833 wakes up from the lowest energy mode via pin interrupt. How could I achive this?

Thank you very much in advance and all the best,

Viktor

Parents
  • Hello again Viktor and thanks for your patience,

    I just started evaluating a nRF52833 DK board with the nRF Connect SDK 2.3.0

    Great! Then in case this is your first glimpse into nRF Connect SDK and nRF's, welcome! 

    I would recommend you to start off by looking into our DevAcademy. I see that Lesson 2 on NCS fundamentals actually happens explain the subject of pin interrupts, I think you'll find your questions answered there.

    Regards,

    Elfving

  • Hi Elfving,

    Thank you very much for the answer! I managed to make the interrupt handling work.

    This is the code for configuring an interrupt on pin4 on both rising and falling edge:

    Device tree:

    custom_gpios {
    	compatible = "gpio-keys";
    	acc_irq_pin1: acc_irq_pin_1 {
    	   gpios = <&gpio0 4 (GPIO_ACTIVE_HIGH)>;
    	   label = "Acc irq pin";
    	};
    
     };

    Main.c

    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/devicetree.h>
    #include <zephyr/drivers/gpio.h>
    
    
    #define IRQ_ACC_NODE	DT_ALIAS(accirq0)
    static const struct gpio_dt_spec irq_acc_pin = GPIO_DT_SPEC_GET_OR(IRQ_ACC_NODE, gpios, {0});
    static struct gpio_callback irq_acc_callback;
    
    void IRQ_ACC_handler(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
    {
    	if(gpio_pin_get_dt(&irq_acc_pin) == 0)
    	{
    		printk("Falling edge interrupt\n");
    	}
    	else
    	{
    		printk("Rising edge interrupt\n");
    	}
    }
    
    
    void main(void)
    {
    	if (!device_is_ready(irq_acc_pin.port)) 
    	{
    		printk("Error: gpio device %s is not ready\n", irq_acc_pin.port->name);
    		return;
    	}
    
    	if (gpio_pin_configure_dt(&irq_acc_pin, GPIO_INPUT) != 0) 
    	{
    		printk("Error: failed to configure %s pin %d\n", irq_acc_pin.port->name, irq_acc_pin.pin);
    		return;
    	}
    
    	if (gpio_pin_interrupt_configure_dt(&irq_acc_pin, GPIO_INT_EDGE_BOTH) != 0) 
    	{
    		printk("Error: failed to configure interrupt on %s pin %d\n", irq_acc_pin.port->name, irq_acc_pin.pin);
    		return;
    	}
    
    	gpio_init_callback(&irq_acc_callback, IRQ_ACC_handler, BIT(irq_acc_pin.pin));
    	gpio_add_callback(irq_acc_pin.port, &irq_acc_callback);
    
    }

Reply
  • Hi Elfving,

    Thank you very much for the answer! I managed to make the interrupt handling work.

    This is the code for configuring an interrupt on pin4 on both rising and falling edge:

    Device tree:

    custom_gpios {
    	compatible = "gpio-keys";
    	acc_irq_pin1: acc_irq_pin_1 {
    	   gpios = <&gpio0 4 (GPIO_ACTIVE_HIGH)>;
    	   label = "Acc irq pin";
    	};
    
     };

    Main.c

    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/devicetree.h>
    #include <zephyr/drivers/gpio.h>
    
    
    #define IRQ_ACC_NODE	DT_ALIAS(accirq0)
    static const struct gpio_dt_spec irq_acc_pin = GPIO_DT_SPEC_GET_OR(IRQ_ACC_NODE, gpios, {0});
    static struct gpio_callback irq_acc_callback;
    
    void IRQ_ACC_handler(const struct device *dev, struct gpio_callback *cb, uint32_t pins)
    {
    	if(gpio_pin_get_dt(&irq_acc_pin) == 0)
    	{
    		printk("Falling edge interrupt\n");
    	}
    	else
    	{
    		printk("Rising edge interrupt\n");
    	}
    }
    
    
    void main(void)
    {
    	if (!device_is_ready(irq_acc_pin.port)) 
    	{
    		printk("Error: gpio device %s is not ready\n", irq_acc_pin.port->name);
    		return;
    	}
    
    	if (gpio_pin_configure_dt(&irq_acc_pin, GPIO_INPUT) != 0) 
    	{
    		printk("Error: failed to configure %s pin %d\n", irq_acc_pin.port->name, irq_acc_pin.pin);
    		return;
    	}
    
    	if (gpio_pin_interrupt_configure_dt(&irq_acc_pin, GPIO_INT_EDGE_BOTH) != 0) 
    	{
    		printk("Error: failed to configure interrupt on %s pin %d\n", irq_acc_pin.port->name, irq_acc_pin.pin);
    		return;
    	}
    
    	gpio_init_callback(&irq_acc_callback, IRQ_ACC_handler, BIT(irq_acc_pin.pin));
    	gpio_add_callback(irq_acc_pin.port, &irq_acc_callback);
    
    }

Children
No Data
Related